AxibugEmuOnline/AxibugEmuOnline.Client/Assets/AxiProjectTools/Editors/AxiProjectTools.cs

379 lines
13 KiB
C#
Raw Normal View History

2024-12-19 14:34:18 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class AxiProjectTools : EditorWindow
{
static string cachecfgPath = "Assets/AxiComToolCache.asset";
static string toolSenceName = "AxiProjectTools";
static string outCsDir = Application.dataPath + "/AxiCom/";
static Dictionary<string, AxiPrefabCache_Com2GUID> ComType2GUID = new Dictionary<string, AxiPrefabCache_Com2GUID>();
static void GoTAxiProjectToolsSence()
{
string[] sceneGuids = AssetDatabase.FindAssets("t:scene");
foreach (string guid in sceneGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
if (path.Contains(toolSenceName))
{
EditorSceneManager.OpenScene(path);
return;
}
}
}
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[1]<EFBFBD>ɼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>UGUI<EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public static void Part1()
{
GoTAxiProjectToolsSence();
ComType2GUID.Clear();
string[] sceneGuids = AssetDatabase.FindAssets("t:scene");
foreach (string guid in sceneGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
if (path.Contains(toolSenceName))
continue;
EditorSceneManager.OpenScene(path);
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD>ڵ<EFBFBD>
List<GameObject> rootNodes = new List<GameObject>();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
GameObject[] allObjects = FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
if (obj.transform.parent == null)
{
// <20><><EFBFBD><EFBFBD>û<EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
rootNodes.Add(obj);
}
}
foreach (var node in rootNodes)
LoopPrefabNode(path, node, 0);
}
string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab");
foreach (string guid in prefabGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
GetPrefab(path);
}
AxiPrefabCache cache = ScriptableObject.CreateInstance<AxiPrefabCache>();
foreach (var data in ComType2GUID)
cache.caches.Add(data.Value);
AssetDatabase.CreateAsset(cache, cachecfgPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
GoTAxiProjectToolsSence();
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [1]<5D>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD><EFBFBD>µ<EFBFBD>UGUI<55><49><EFBFBD><EFBFBD></color>");
}
static void GetPrefab(string path)
{
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
LoopPrefabNode(path, prefab.gameObject, 0);
}
static void LoopPrefabNode(string rootPath, GameObject trans, int depth)
{
string nodename = $"{rootPath}>{trans.name}";
GameObject prefabRoot = trans.gameObject;
int comCount = prefabRoot.GetComponentCount();
for (int i = 0; i < comCount; i++)
{
var com = prefabRoot.GetComponentAtIndex(i);
2024-12-19 15:13:03 +08:00
if (com == null)
continue;
if (com.name.Contains("VideoPlayer"))
{
}
2024-12-19 14:34:18 +08:00
MonoBehaviour monoCom = com as MonoBehaviour;
if (monoCom == null)
continue;
Type monoType = monoCom.GetType();
2024-12-19 14:55:59 +08:00
if (!monoType.Assembly.FullName.Contains("UnityEngine.UI"))
2024-12-19 14:34:18 +08:00
continue;
// <20><>ȡMonoScript<70><74>Դ
MonoScript monoScript = MonoScript.FromMonoBehaviour(monoCom);
if (monoScript != null)
{
// <20><>ȡMonoScript<70><74>Դ<EFBFBD><D4B4>GUID
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript));
Debug.Log($"{nodename} | <color=#FFF333>[{monoType.Name}]</color> <color=#FF0000>{guid}</color><color=#00FF00>({monoType.FullName})</color>");
ComType2GUID[monoType.FullName] =
new AxiPrefabCache_Com2GUID()
{
SrcFullName = monoType.FullName,
SrcName = monoType.Name,
GUID = guid,
};
}
else
{
Debug.LogError("!!!! û<><C3BB>");
}
}
//<2F><><EFBFBD><EFBFBD>
foreach (Transform child in trans.transform)
LoopPrefabNode(nodename, child.gameObject, depth + 1);
}
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[2]<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public static void Part2()
{
2024-12-19 15:13:03 +08:00
if (UnityEngine.Windows.Directory.Exists(outCsDir))
UnityEngine.Windows.Directory.Delete(outCsDir);
2024-12-19 14:34:18 +08:00
Directory.CreateDirectory(outCsDir);
AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath);
foreach (var data in cache.caches)
{
string toName = "Axi" + data.SrcName;
string toPath = outCsDir + toName + ".cs";
2024-12-19 15:13:03 +08:00
string codeStr = "namespace AxibugCom { public class " + toName + " : " + data.SrcFullName + " {} }";
2024-12-19 14:34:18 +08:00
try
{
System.IO.File.WriteAllText(toPath, codeStr);
data.ToName = toName;
data.ToPATH = toPath;
}
catch (Exception ex)
{
Debug.LogError(<><D0B4>ʧ<EFBFBD><CAA7>" + ex.ToString());
}
}
Debug.Log(<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [2]<5D><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD></color>");
}
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[3]<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵĽű<EFBFBD>")]
public static void Part3()
{
AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath);
List<MonoScript> allMonoScripts = FindAllAssetsOfType<MonoScript>();
foreach (var data in cache.caches)
{
MonoScript monoScript = allMonoScripts.FirstOrDefault(w => w.name == data.ToName);
if (monoScript == null)
{
Debug.LogError(<>ҵ<EFBFBD>" + data.ToName);
continue;
}
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript));
data.ToGUID = guid;
data.monoScript = monoScript;
}
Debug.Log(<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [3]<5D>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD>ɵĽű<C4BD></color>");
}
static List<T> FindAllAssetsOfType<T>() where T : UnityEngine.Object
{
List<T> assets = new List<T>();
string[] allGuids = AssetDatabase.FindAssets("");
foreach (string guid in allGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
if (path.EndsWith(".cs") || path.EndsWith(".js") || path.EndsWith(".boo")) // Unity֧<79>ֶ<EFBFBD><D6B6>ֽű<D6BD><C5B1><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD>ִ<EFBFBD>Unity<74><79>Ҫʹ<D2AA><CAB9>C#
{
T asset = AssetDatabase.LoadAssetAtPath<T>(path);
if (asset != null)
{
assets.Add(asset);
}
}
}
return assets;
}
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[4]<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public static void Part4()
{
AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath);
Dictionary<string, string> tempReplaceDict = new Dictionary<string, string>();
foreach (var data in cache.caches)
{
tempReplaceDict[data.GUID] = data.ToGUID;
}
ProcessAllPrefabs("*.prefab", tempReplaceDict);
ProcessAllPrefabs("*.unity", tempReplaceDict);
2024-12-19 16:00:45 +08:00
ProcessAllPrefabs("*.anim", tempReplaceDict);
2024-12-19 14:34:18 +08:00
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [4]<5D><EFBFBD><E6BBBB><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD></color>");
}
static void ProcessAllPrefabs(string form, Dictionary<string, string> tempReplaceDict, bool reverse = false)
{
List<GameObject> prefabs = new List<GameObject>();
var resourcesPath = Application.dataPath;
var absolutePaths = Directory.GetFiles(resourcesPath, form, SearchOption.AllDirectories);
for (int i = 0; i < absolutePaths.Length; i++)
{
Debug.Log("prefab name: " + absolutePaths[i]);
foreach (var VARIABLE in tempReplaceDict)
{
string oldValue = reverse ? VARIABLE.Value : VARIABLE.Key;
string newValue = reverse ? VARIABLE.Key : VARIABLE.Value;
ReplaceValue(absolutePaths[i], oldValue, newValue);
}
EditorUtility.DisplayProgressBar("<22><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><E5A1AD>", "<22><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>", (float)i / absolutePaths.Length);
}
EditorUtility.ClearProgressBar();
}
/// <summary>
/// <20>滻ֵ
/// </summary>
/// <param name="strFilePath"><3E>ļ<EFBFBD>·<EFBFBD><C2B7></param>
static void ReplaceValue(string strFilePath, string oldLine, string newLine)
{
if (File.Exists(strFilePath))
{
string[] lines = File.ReadAllLines(strFilePath);
for (int i = 0; i < lines.Length; i++)
{
lines[i] = lines[i].Replace(oldLine, newLine);
}
File.WriteAllLines(strFilePath, lines);
}
}
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[5]UnPack<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public static void UnpackPrefabs()
{
string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
int prefabCount = 0;
foreach (string path in allAssetPaths)
{
if (Path.GetExtension(path).Equals(".prefab"))
{
Debug.Log($"Unpacking {path}");
UnpackPrefab(path);
prefabCount++;
}
}
Debug.Log($"Unpacked {prefabCount} prefabs.");
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [5]UnPack<63><6B><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD></color>");
}
static void UnpackPrefab(string prefabPath)
{
GameObject prefabInstance = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (prefabInstance == null)
{
Debug.LogError($"Failed to load prefab at path: {prefabPath}");
return;
}
var obj = GameObject.Instantiate(prefabInstance, null);
TraverseHierarchy(obj);
PrefabUtility.SaveAsPrefabAsset(obj, prefabPath);
GameObject.DestroyImmediate(obj);
}
static void TraverseHierarchy(GameObject obj)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
if (PrefabUtility.IsPartOfPrefabInstance(obj))
{
// <20><>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>ͨ<EFBFBD><CDA8>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Debug.Log("Prefab instance converted to game object: " + obj.name);
}
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
for (int i = 0; i < obj.transform.childCount; i++)
{
TraverseHierarchy(obj.transform.GetChild(i).gameObject);
}
}
2024-12-19 16:00:45 +08:00
[MenuItem("Axibug<75><67>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>/[6]<EFBFBD>޸<EFBFBD>Sprite")]
public static void FixMultipleMaterialSprites()
{
string[] guids = AssetDatabase.FindAssets("t:sprite");
List<Sprite> spritesToFix = new List<Sprite>();
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (IsUsingMultipleMaterials(sprite))
{
spritesToFix.Add(sprite);
Debug.Log("Found sprite with multiple materials: " + path);
}
}
// <20>޸<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Sprite
foreach (var sprite in spritesToFix)
{
FixSprite(sprite);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [6]<5D>޸<EFBFBD>Sprite</color>");
}
private static bool IsUsingMultipleMaterials(Sprite sprite)
{
if (sprite == null) return false;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
var textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite)) as TextureImporter;
return textureImporter != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple;
}
private static void FixSprite(Sprite sprite)
{
// <20><>ȡSprite<74><65>·<EFBFBD><C2B7>
string path = AssetDatabase.GetAssetPath(sprite);
var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter != null)
{
// <20><><EFBFBD>浱ǰ<E6B5B1>и<EFBFBD><D0B8><EFBFBD>Ϣ
SpriteMetaData[] originalMetaData = textureImporter.spritesheet;
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Sprite<74><65><EFBFBD><EFBFBD>
textureImporter.spriteImportMode = SpriteImportMode.None;
textureImporter.SaveAndReimport();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Sprite<74><65><EFBFBD><EFBFBD><EBB2A2><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
textureImporter.spriteImportMode = SpriteImportMode.Multiple;
textureImporter.spritesheet = originalMetaData; // <20>ָ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD>Ϣ
// <20><><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ø<EFBFBD><C3B8><EFBFBD>
textureImporter.SaveAndReimport();
}
}
2024-12-19 14:34:18 +08:00
}