JJBB/Assets/Editor/Scripts/ResourceModify.cs
2024-08-23 15:49:34 +08:00

224 lines
8.1 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class ResourceModify : Editor
{
public static string ASSET_PATH = "/Project3D/BundleData";
[MenuItem("ProTool/ResTool/UIClass")]
public static void ClassifyUIPrefabBundles()
{
string sysPath = Application.dataPath + ASSET_PATH + "/UI/Prefab";
string[] filePaths = Directory.GetFiles(sysPath, "*.prefab", SearchOption.AllDirectories);
List<string> uiFileNames = new List<string>();
var uiPathInfos = typeof(UIInfo).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
foreach (var uiInfo in uiPathInfos)
{
if (uiInfo.FieldType != typeof(UIPathData))
continue;
var uipathInfo = uiInfo.GetValue(null) as UIPathData;
uiFileNames.Add(uipathInfo.name);
}
List<Object> prefabs = new List<Object>();
foreach (var file in filePaths)
{
string dataFile = file.Replace(Application.dataPath, "Assets");
string fileName = Path.GetFileNameWithoutExtension(file);
if (!uiFileNames.Contains(fileName))
continue;
fileName = fileName.ToLower();
Object assetData = AssetDatabase.LoadAssetAtPath<Object>(dataFile);
Object[] dependObjs = EditorUtility.CollectDependencies(new Object[1] { assetData });
List<string> containedDepend = new List<string>();
prefabs.Add(assetData);
foreach (var dependObj in dependObjs)
{
if (dependObj == null)
continue;
if (assetData == dependObj)
continue;
if (dependObj.name == assetData.name)
continue;
if (dependObj is GameObject)
{
var selectionPrefab = PrefabUtility.FindPrefabRoot(dependObj as GameObject);
if (!prefabs.Contains(selectionPrefab))
{
prefabs.Add(selectionPrefab);
}
//Debug.Log(selectionPrefab.name);
//return;
}
}
}
foreach (var prefabObj in prefabs)
{
Object[] dependObjs = EditorUtility.CollectDependencies(new Object[1] { prefabObj });
List<string> containedDepend = new List<string>();
foreach (var dependObj in dependObjs)
{
if (dependObj == null)
continue;
if (prefabObj == dependObj)
continue;
if (dependObj.name == prefabObj.name)
continue;
if (dependObj is Texture2D)
{
string dependObjPath = AssetDatabase.GetAssetPath(dependObj);
if (dependObjPath.Contains("第二版")
|| dependObjPath.Contains("第三版")
|| dependObjPath.Contains("第四版"))
{
Debug.Log("File:" + prefabObj.name + " contains old res:" + dependObjPath);
}
}
}
}
}
[MenuItem("ProTool/ResTool/UIClassSingle")]
public static void GetSingleUIOldRes()
{
var selectUI = Selection.GetFiltered(typeof(GameObject), SelectionMode.Deep);
for (int i = 0; i < selectUI.Length; ++i)
{
var uiGO = selectUI[i] as GameObject;
var images = uiGO.GetComponentsInChildren<Image>(true);
foreach (var image in images)
{
if (image.sprite == null)
continue;
string dependObjPath = AssetDatabase.GetAssetPath(image.sprite);
if (dependObjPath.Contains("第二版")
|| dependObjPath.Contains("第三版")
|| dependObjPath.Contains("第四版"))
{
Debug.Log("Img:" + image.name + ", res:" + dependObjPath);
}
}
}
}
[MenuItem("ProTool/ResTool/UICompress")]
public static void UICompress()
{
List<string> resFiles = new List<string>();
string sysPath = Application.dataPath + ASSET_PATH + "/UI/Prefab";
string[] filePaths = Directory.GetFiles(sysPath, "*.prefab", SearchOption.AllDirectories);
resFiles.AddRange(filePaths);
sysPath = Application.dataPath + ASSET_PATH + "/UI/Sprites";
string[] directPaths = Directory.GetDirectories(sysPath);
resFiles.AddRange(directPaths);
sysPath = Application.dataPath + ASSET_PATH + "/UI/Texture";
directPaths = Directory.GetDirectories(sysPath);
resFiles.AddRange(directPaths);
sysPath = Application.dataPath + "/Res_newMS/ChangJing/Sence";
filePaths = Directory.GetFiles(sysPath, "*.unity", SearchOption.TopDirectoryOnly);
resFiles.AddRange(filePaths);
sysPath = Application.dataPath + ASSET_PATH + "/Model";
filePaths = Directory.GetFiles(sysPath, "*.prefab", SearchOption.AllDirectories);
resFiles.AddRange(filePaths);
sysPath = Application.dataPath + ASSET_PATH + "/Weapon";
filePaths = Directory.GetFiles(sysPath, "*.prefab", SearchOption.AllDirectories);
resFiles.AddRange(filePaths);
sysPath = Application.dataPath + ASSET_PATH + "/Effect";
filePaths = Directory.GetFiles(sysPath, "*.prefab", SearchOption.AllDirectories);
resFiles.AddRange(filePaths);
foreach (var file in resFiles)
{
string dataFile = file.Replace(Application.dataPath, "Assets");
string fileName = Path.GetFileNameWithoutExtension(file);
fileName = fileName.ToLower();
Object assetData = AssetDatabase.LoadAssetAtPath<Object>(dataFile);
Object[] dependObjs = EditorUtility.CollectDependencies(new Object[1] { assetData });
List<string> containedDepend = new List<string>();
foreach (var dependObj in dependObjs)
{
if (dependObj == null)
continue;
if (assetData == dependObj)
continue;
if (dependObj.name == assetData.name)
continue;
if (dependObj is Texture2D)
{
string dependObjPath = AssetDatabase.GetAssetPath(dependObj);
if (dependObjPath == file)
continue;
var dependImporter = AssetImporter.GetAtPath(dependObjPath);
TextureImporter resImporter = dependImporter as TextureImporter;
if (resImporter != null)
{
//resImporter.spritePackingTag = dataFile;
//resImporter.mipmapEnabled = false;
resImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
}
}
}
}
AssetDatabase.Refresh();
}
[MenuItem("ProTool/ResTool/TextureCompress")]
public static void TextureCompress()
{
var selectTextures = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);
Debug.Log("selectTextures:" + selectTextures.Length);
foreach (var texture in selectTextures)
{
var texturePath = AssetDatabase.GetAssetPath(texture);
var dependImporter = AssetImporter.GetAtPath(texturePath);
TextureImporter resImporter = dependImporter as TextureImporter;
if (resImporter != null)
{
resImporter.textureType = TextureImporterType.Sprite;
//resImporter.spritePackingTag = dataFile;
resImporter.mipmapEnabled = false;
resImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
}
Debug.Log("set " + texturePath + " ok.");
dependImporter.SaveAndReimport();
}
AssetDatabase.Refresh();
}
}