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

417 lines
13 KiB
C#
Raw Normal View History

2024-12-26 10:40:59 +08:00
#if UNITY_EDITOR
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;
2024-12-26 10:40:59 +08:00
using UnityEngine.SceneManagement;
2024-12-19 14:34:18 +08:00
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;
}
}
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[1]UGUI组件")]
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);
2024-12-26 10:40:59 +08:00
// 创建一个列表来存储根节点
List<GameObject> rootNodes = new List<GameObject>();
2024-12-26 10:40:59 +08:00
// 遍历场景中的所有对象
GameObject[] allObjects = FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects)
{
2024-12-26 10:40:59 +08:00
// 检查对象是否有父对象
if (obj.transform.parent == null)
{
2024-12-26 10:40:59 +08:00
// 如果没有父对象,则它是一个根节点
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();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [1]采集所有预制体和场景下的UGUI组件</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}";
2024-12-25 22:38:55 +08:00
#if UNITY_2018_4_OR_NEWER
2024-12-19 14:34:18 +08:00
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;
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;
2024-12-26 10:40:59 +08:00
// 获取MonoScript资源
2024-12-19 14:34:18 +08:00
MonoScript monoScript = MonoScript.FromMonoBehaviour(monoCom);
if (monoScript != null)
{
2024-12-26 10:40:59 +08:00
// 获取MonoScript资源的GUID
2024-12-19 14:34:18 +08:00
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
{
2024-12-26 10:40:59 +08:00
Debug.LogError("!!!! 没得");
2024-12-19 14:34:18 +08:00
}
}
2024-12-26 10:40:59 +08:00
//遍历
2024-12-19 14:34:18 +08:00
foreach (Transform child in trans.transform)
LoopPrefabNode(nodename, child.gameObject, depth + 1);
#else
2024-12-26 10:40:59 +08:00
Debug.Log("低版本不要执行本函数");
#endif
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[2]")]
public static void Part2()
{
if (UnityEngine.Windows.Directory.Exists(outCsDir))
UnityEngine.Windows.Directory.Delete(outCsDir);
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";
string codeStr = "namespace AxibugCom { public class " + toName + " : " + data.SrcFullName + " {} }";
try
{
System.IO.File.WriteAllText(toPath, codeStr);
data.ToName = toName;
data.ToPATH = toPath;
}
catch (Exception ex)
{
2024-12-26 10:40:59 +08:00
Debug.LogError("写入失败" + ex.ToString());
}
}
2024-12-26 10:40:59 +08:00
Debug.Log("写入完毕");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [2]生成中间脚本代码</color>");
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[3]")]
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)
{
2024-12-26 10:40:59 +08:00
Debug.LogError("没找到" + data.ToName);
continue;
}
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript));
data.ToGUID = guid;
data.monoScript = monoScript;
}
2024-12-26 10:40:59 +08:00
Debug.Log("写入完毕");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [3]收集生成的脚本</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);
2024-12-26 10:40:59 +08:00
if (path.EndsWith(".cs") || path.EndsWith(".js") || path.EndsWith(".boo")) // Unity支持多种脚本语言但现代Unity主要使用C#
{
T asset = AssetDatabase.LoadAssetAtPath<T>(path);
if (asset != null)
{
assets.Add(asset);
}
}
}
return assets;
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[4]")]
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);
ProcessAllPrefabs("*.anim", tempReplaceDict);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [4]替换所有预制体和场景中的组件</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);
}
2024-12-26 10:40:59 +08:00
EditorUtility.DisplayProgressBar("处理预制体……", "处理预制体中……", (float)i / absolutePaths.Length);
}
EditorUtility.ClearProgressBar();
}
/// <summary>
2024-12-26 10:40:59 +08:00
/// 替换值
/// </summary>
2024-12-26 10:40:59 +08:00
/// <param name="strFilePath">文件路径</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);
}
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[5]UnPack所有嵌套预制体和场景中的预制体")]
public static void UnpackPrefabs()
{
2024-12-25 23:48:55 +08:00
#if UNITY_2018_4_OR_NEWER
GoTAxiProjectToolsSence();
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++;
}
}
2024-12-26 10:40:59 +08:00
Debug.Log($"{prefabCount}个预制体Unpack");
2024-12-25 23:48:55 +08:00
string[] sceneGuids = AssetDatabase.FindAssets("t:scene");
foreach (string guid in sceneGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
if (path.Contains(toolSenceName))
continue;
EditorSceneManager.OpenScene(path);
Scene currentScene = SceneManager.GetActiveScene();
GameObject[] rootObjects = currentScene.GetRootGameObjects();
foreach (GameObject rootObj in rootObjects)
{
2024-12-26 10:40:59 +08:00
// 遍历场景中的所有对象
2024-12-25 23:48:55 +08:00
TraverseHierarchy(rootObj);
}
2024-12-26 10:40:59 +08:00
// Save the scene // 获取当前打开的场景
2024-12-25 23:48:55 +08:00
currentScene = EditorSceneManager.GetActiveScene();
2024-12-26 10:40:59 +08:00
// 保存场景到文件(默认路径和名称)
2024-12-25 23:48:55 +08:00
bool success = EditorSceneManager.SaveScene(currentScene, currentScene.path);
2024-12-26 10:40:59 +08:00
Debug.Log($"{currentScene.name}场景中 所有物体Unpack");
2024-12-25 23:48:55 +08:00
}
2024-12-25 23:48:55 +08:00
GoTAxiProjectToolsSence();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [5]UnPack所有预制体</color>");
2024-12-25 23:48:55 +08:00
#else
2024-12-26 10:40:59 +08:00
Debug.Log("低版本不要执行本函数");
2024-12-25 23:48:55 +08:00
#endif
}
static void UnpackPrefab(string prefabPath)
{
2024-12-25 23:48:55 +08:00
#if UNITY_2018_4_OR_NEWER
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);
2024-12-25 23:48:55 +08:00
#else
2024-12-26 10:40:59 +08:00
Debug.Log("低版本不要执行本函数");
2024-12-25 23:48:55 +08:00
#endif
}
static void TraverseHierarchy(GameObject obj)
{
2024-12-25 23:48:55 +08:00
#if UNITY_2018_4_OR_NEWER
2024-12-26 10:40:59 +08:00
// 检查该对象是否是预制体的实例
if (PrefabUtility.IsPartOfPrefabInstance(obj))
{
2024-12-26 10:40:59 +08:00
// 将预制体实例转换为普通游戏对象
PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Debug.Log("Prefab instance converted to game object: " + obj.name);
}
2024-12-26 10:40:59 +08:00
// 递归遍历子对象
for (int i = 0; i < obj.transform.childCount; i++)
{
TraverseHierarchy(obj.transform.GetChild(i).gameObject);
}
2024-12-25 23:48:55 +08:00
#else
2024-12-26 10:40:59 +08:00
Debug.Log("低版本不要执行本函数");
2024-12-25 23:48:55 +08:00
#endif
}
2024-12-26 10:40:59 +08:00
[MenuItem("Axibug移植工具/[6]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);
2024-12-26 10:40:59 +08:00
// 检查是否有多个材质
if (IsUsingMultipleMaterials(sprite))
{
spritesToFix.Add(sprite);
Debug.Log("Found sprite with multiple materials: " + path);
}
}
2024-12-26 10:40:59 +08:00
// 修复每个找到的Sprite
foreach (var sprite in spritesToFix)
{
FixSprite(sprite);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2024-12-26 10:40:59 +08:00
Debug.Log("<Color=#FFF333>处理完毕 [6]修复Sprite</color>");
}
private static bool IsUsingMultipleMaterials(Sprite sprite)
{
if (sprite == null) return false;
2024-12-26 10:40:59 +08:00
// 获取精灵的材质
var textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite)) as TextureImporter;
return textureImporter != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple;
}
private static void FixSprite(Sprite sprite)
{
2024-12-26 10:40:59 +08:00
// 获取Sprite的路径
string path = AssetDatabase.GetAssetPath(sprite);
var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter != null)
{
2024-12-26 10:40:59 +08:00
// 保存当前切割信息
SpriteMetaData[] originalMetaData = textureImporter.spritesheet;
2024-12-26 10:40:59 +08:00
// 临时禁用Sprite导入
textureImporter.spriteImportMode = SpriteImportMode.None;
textureImporter.SaveAndReimport();
2024-12-26 10:40:59 +08:00
// 重新启用Sprite导入并保持原样切割参数
textureImporter.spriteImportMode = SpriteImportMode.Multiple;
2024-12-26 10:40:59 +08:00
textureImporter.spritesheet = originalMetaData; // 恢复原来的切割信息
2024-12-26 10:40:59 +08:00
// 重新导入以应用更改
textureImporter.SaveAndReimport();
}
}
2024-12-26 00:30:59 +08:00
}
#endif