138 lines
4.0 KiB
C#
138 lines
4.0 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class EffectResTools
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
private static Dictionary<string, int> Counter = new Dictionary<string, int>();
|
|||
|
|
|||
|
// 资源在这些路径下不做处理
|
|||
|
private static string[] _ExceptPath = new string[]{
|
|||
|
"Assets/ManagedResources/Atlas",
|
|||
|
"Assets/ManagedResources/BG",
|
|||
|
"Assets/ManagedResources/Effects",
|
|||
|
"Assets/ManagedResources/PublicArtRes",
|
|||
|
"Assets/ManagedResources/DynamicAtlas"
|
|||
|
};
|
|||
|
|
|||
|
// 判断资源是否不做处理
|
|||
|
private static bool CheckIsExcept(string path)
|
|||
|
{
|
|||
|
for (int i = 0; i < _ExceptPath.Length; i++)
|
|||
|
{
|
|||
|
if (path.StartsWith(_ExceptPath[i]))
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// 获取资源类型文件夹名称
|
|||
|
private static string GetSubFolder(string ext)
|
|||
|
{
|
|||
|
ext = ext.ToLower();
|
|||
|
if(ext.Equals(".png")||ext.Equals(".tga")||ext.Equals(".jpg"))
|
|||
|
{
|
|||
|
return "Texture";
|
|||
|
}
|
|||
|
else if (ext.Equals(".mat"))
|
|||
|
{
|
|||
|
return "Material";
|
|||
|
}
|
|||
|
else if (ext.Equals(".controller")|| ext.Equals(".anim"))
|
|||
|
{
|
|||
|
return "Animation";
|
|||
|
}
|
|||
|
else if (ext.Equals(".shader"))
|
|||
|
{
|
|||
|
return "Shader";
|
|||
|
}
|
|||
|
else if (ext.Equals(".fbx")||ext.Equals(".obj"))
|
|||
|
{
|
|||
|
return "Models";
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
[MenuItem("Tools/Effect/检测重复引用的特效资源")]
|
|||
|
private static void CheckEffectRes()
|
|||
|
{
|
|||
|
List<string> assetPaths = new List<string>();
|
|||
|
assetPaths.Add("Assets/ManagedResources/EffectResSkill");
|
|||
|
assetPaths.Add("Assets/ManagedResources/EffectResUI");
|
|||
|
|
|||
|
// 开始时清除计数器
|
|||
|
Counter.Clear();
|
|||
|
|
|||
|
string[] allPath = AssetDatabase.FindAssets("t:Prefab", assetPaths.ToArray());
|
|||
|
for (int i = 0; i < allPath.Length; i++)
|
|||
|
{
|
|||
|
string path = AssetDatabase.GUIDToAssetPath(allPath[i]);
|
|||
|
EditorUtility.DisplayProgressBar(string.Format("正在检查:({0}/{1})", i, allPath.Length), "路径:" + path, (float)i / allPath.Length);
|
|||
|
CheckSingleRes(path);
|
|||
|
}
|
|||
|
|
|||
|
// 结束时再次清除计数器
|
|||
|
Counter.Clear();
|
|||
|
}
|
|||
|
|
|||
|
// 单个资源检测
|
|||
|
private static void CheckSingleRes(string path)
|
|||
|
{
|
|||
|
var dependencies = AssetDatabase.GetDependencies(path);
|
|||
|
for (int j = 0; j < dependencies.Length; j++)
|
|||
|
{
|
|||
|
string ps = dependencies[j];
|
|||
|
if (CheckIsExcept(ps))
|
|||
|
continue;
|
|||
|
int outValue = 0;
|
|||
|
if (Counter.TryGetValue(ps, out outValue))
|
|||
|
{
|
|||
|
Counter[ps] = outValue + 1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Counter.Add(ps, 1);
|
|||
|
}
|
|||
|
|
|||
|
if (outValue > 0)
|
|||
|
{
|
|||
|
MoveToCommon(ps);
|
|||
|
// 动画要把引用的anim也一起移动
|
|||
|
string ext = Path.GetExtension(ps).ToLower();
|
|||
|
if (ext.Equals(".controller"))
|
|||
|
{
|
|||
|
CheckSingleRes(ps);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 移动到公用文件夹
|
|||
|
private static void MoveToCommon(string assetPath)
|
|||
|
{
|
|||
|
|
|||
|
if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/EffectResCommon"))
|
|||
|
{
|
|||
|
AssetDatabase.CreateFolder("Assets/ManagedResources", "EffectResCommon");
|
|||
|
}
|
|||
|
string subFolderName = GetSubFolder(Path.GetExtension(assetPath));
|
|||
|
if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/EffectResCommon/" + subFolderName))
|
|||
|
{
|
|||
|
AssetDatabase.CreateFolder("Assets/ManagedResources/EffectResCommon", subFolderName);
|
|||
|
}
|
|||
|
|
|||
|
string fileName = Path.GetFileName(assetPath);
|
|||
|
string tarPath = "Assets/ManagedResources/EffectResCommon/"+ subFolderName+ "/" + fileName;
|
|||
|
AssetDatabase.MoveAsset(assetPath, tarPath);
|
|||
|
Debug.LogWarning("MoveAsset " + assetPath + " to " + tarPath);
|
|||
|
}
|
|||
|
}
|