unity自动引用生成
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;/// <summary>
/// 模板脚本生成
/// </summary>
public class ScriptCreater : EditorWindow
{#region 生成CS脚本引用//按照前缀命名的就会自动生成引用private static Dictionary<string, string> compDic = new Dictionary<string, string>{{"rect_", "RectTransform"},{"btn_", "Button"},{ "text_","Text"},{"wImg_", "Image"}, };private static Dictionary<string, string[]> nameOfPath = new Dictionary<string, string[]>();private static string GenerateClassName;//类名private static void GenerateMainClass(){string mainPath = $"{Application.dataPath}/Scripts/Example/{GenerateClassName}.cs";if (!File.Exists(mainPath)){File.Create(mainPath).Close();}
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();
#endifFile.WriteAllText(mainPath,@"using UnityEngine;
using System;
using UnityEngine.UI;
public partial class _D_CLASSNAME
{protected override void OnCreate(){}}
".Replace("_D_CLASSNAME", GenerateClassName));
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();
#endifGeneratePartClass();}private static void GeneratePartClass(){string fielddata = "";string referdata = "";foreach (var item in nameOfPath){fielddata += $"\tprivate {item.Value[1]} {item.Key};\n";referdata += $"\t\t{item.Key} = transform.Find(\"{ item.Value[0] }\").GetComponent<{item.Value[1]}>();\n";}referdata += "\t\tAfterInitRef();";string baseTemp = @"using System;
using UnityEngine;
using UnityEngine.UI;public partial class _D_CLASSNAME
{
FILEDLISTprotected override void InitComp(){
REFERENCE}
}
";string info = "";info = baseTemp.Replace("FILEDLIST", fielddata);info = info.Replace("_D_CLASSNAME", GenerateClassName);info = info.Replace("REFERENCE", referdata);string aprtPath = $"{Application.dataPath}/Scripts/Game/Runtime/Logic/Dialog/Partial/{GenerateClassName}.cs";if (!File.Exists(aprtPath)){File.CreateText(aprtPath).Close();
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();
#endif}File.WriteAllText(aprtPath, info);
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();
#endif}[MenuItem("GameObject/UI组件引用生成", false, 1)][MenuItem("Assets/UI组件引用生成")]public static void UGUIPrefabGenerateCS(){nameOfPath.Clear();UnityEngine.Object obj = Selection.activeObject;if (obj == null){Debug.LogError("请选择预设");return;}GameObject prefab = obj as GameObject;List<string> types = new List<string>();string baseName = prefab.name;GenerateClassName = baseName;foreach (Transform item in prefab.GetComponentsInChildren<Transform>(true)){Debug.Log(item.name);foreach (var v in compDic){if (item.name.StartsWith(v.Key)){string fieldName = "comp_" + item.name;string path = "";var curgo = item;types.Clear();while (curgo != null){if (curgo.parent != null) types.Add(curgo.name);curgo = curgo.parent;}for (int i = types.Count - 1; i >= 0; i--){path += (types[i] + (i != 0 ? "/" : ""));}nameOfPath.Add(fieldName, new string[] { path, v.Value });}}}Debug.Log($"nameOfPath.Count = {nameOfPath.Count}");GenerateMainClass();}#endregion
}
思路:按照规范命名的才会生成引用