Ref映射工具
来源:互联网 发布:软件证书挂靠 编辑:程序博客网 时间:2024/06/06 16:29
第一次写博客。
using UnityEditor;using UnityEngine;using UnityEngine.UI;using System.Collections.Generic;using System.IO;using System.Reflection;using System;public class UIRefScriptCreator{ private static string SCRIPT_PATH { get { return Path.GetFullPath(Application.dataPath + "/Src/UI/Ref"); } } [MenuItem("MyTools/UIHelper/CreateRefScripts")] public static void CreateRefScripts() { string uiPath = Application.dataPath + "/Res/Bundles/Prefabs/UI/zh"; if (Directory.Exists(uiPath)) { DirectoryInfo di = new DirectoryInfo(uiPath); FileInfo[] fis = di.GetFiles(); for (int i = 0; i < fis.Length; i++) { if (fis[i].Extension != ".prefab") continue; UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath("Assets/Res/Bundles/Prefabs/UI/zh/" + fis[i].Name, typeof(UnityEngine.Object)); GameObject go = obj as GameObject; if (go) { CreateScript(go); } } AssetDatabase.Refresh(); } } private static bool CreateScript(GameObject obj) { string clzName = GetClassName(obj); if (!Directory.Exists(SCRIPT_PATH)) Directory.CreateDirectory(SCRIPT_PATH); string path = Path.Combine(SCRIPT_PATH, clzName + ".cs"); Dictionary<string, List<UIComRef>> objDic = new Dictionary<string, List<UIComRef>>(); FileStream fs = new FileStream(path, FileMode.Create); StreamWriter writer = new StreamWriter(fs); writer.Write("using UnityEngine;\nusing UnityEngine.UI;\nusing System.Collections.Generic;\n\nnamespace Game\n{\n\tpublic class " + clzName + " : MonoBehaviour\n\t{\n"); CreateUIComRef(obj, out objDic); foreach (string refName in objDic.Keys) { if (objDic[refName].Count > 1) { writer.Write("\t\tpublic List<UIComRef> " + refName + " = new List<UIComRef>();\n"); } else { writer.Write("\t\tpublic UIComRef " + refName + " = null;\n"); } } writer.Write("\t}\n}"); writer.Flush(); writer.Close(); fs.Close(); return true; } public static void CreateUIComRef(GameObject obj, out Dictionary<string, List<UIComRef>> objDic) { Dictionary<string, List<UIComRef>> oriDic = new Dictionary<string, List<UIComRef>>(); Dictionary<string, List<UIComRef>> tempDic = new Dictionary<string, List<UIComRef>>(); GameObject tempGo = null; UIComRef tempRef = null; string refName = ""; if (obj.transform.childCount == 0) { if (!obj.GetComponent<UINotRef>()) { if (obj.GetComponent<UIComRef>()) { tempRef = obj.GetComponent<UIComRef>(); refName = tempRef.name; if (!string.IsNullOrEmpty(tempRef.RefName)) refName = tempRef.RefName; if (!oriDic.ContainsKey(refName)) oriDic.Add(refName, new List<UIComRef>()); oriDic[refName].Add(tempRef); } } } else { for (int i = 0; i < obj.transform.childCount; i++) { tempGo = obj.transform.GetChild(i).gameObject; if (!tempGo.GetComponent<UINotRef>()) { if (tempGo.GetComponent<UIComRef>()) { tempRef = tempGo.GetComponent<UIComRef>(); refName = tempRef.name; if (!string.IsNullOrEmpty(tempRef.RefName)) refName = tempRef.RefName; if (!oriDic.ContainsKey(refName)) oriDic.Add(refName, new List<UIComRef>()); oriDic[refName].Add(tempRef); } CreateUIComRef(tempGo, out tempDic); foreach (var item in tempDic) { if (oriDic.ContainsKey(item.Key)) oriDic[item.Key].AddRange(item.Value); else oriDic.Add(item.Key, item.Value); } tempDic.Clear(); } } } objDic = oriDic; } [MenuItem("MyTools/UIHelper/AttachRefScripts")] public static void AttachRefScripts() { string uiPath = Application.dataPath + "/Res/Bundles/Prefabs/UI/zh"; if (Directory.Exists(uiPath)) { DirectoryInfo di = new DirectoryInfo(uiPath); FileInfo[] fis = di.GetFiles(); for (int i = 0; i < fis.Length; i++) { if (fis[i].Extension != ".prefab") continue; UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath("Assets/Res/Bundles/Prefabs/UI/zh/" + fis[i].Name, typeof(UnityEngine.Object)); GameObject go = obj as GameObject; if (go) AttachScript(go); } AssetDatabase.Refresh(); } } private static void AttachScript(GameObject obj) { string clzName = GetClassName(obj); Type t = Type.GetType("Game." + clzName); if (t == null) { var currentAssembly = Assembly.GetExecutingAssembly(); var referencedAssemblies = currentAssembly.GetReferencedAssemblies(); foreach (var assemblyName in referencedAssemblies) { // Load the referenced assembly var assembly = Assembly.Load(assemblyName); if (assembly != null) { // See if that assembly defines the named type t = assembly.GetType("Game." + clzName); if (t != null) break; } } } Component com = obj.GetComponent(t); if (com) UnityEngine.Object.DestroyImmediate(com, true); com = obj.AddComponent(t); foreach (UIComRef refInfo in obj.GetComponentsInChildren<UIComRef>(true)) { try { Attach(com, refInfo); } catch (Exception e) { Debug.LogException(e); } } } private static void Attach(Component com, UIComRef refInfo) { string refName = refInfo.name; if (!string.IsNullOrEmpty(refInfo.RefName)) refName = refInfo.RefName; FieldInfo fi = com.GetType().GetField(refName); if (fi != null) { if (typeof(List<UIComRef>).IsAssignableFrom(fi.FieldType)) { List<UIComRef> list = fi.GetValue(com) as List<UIComRef>; list.Add(refInfo); } else { fi.SetValue(com, refInfo); } } } private static string GetClassName(GameObject obj) { return "UI" + obj.name + "Ref"; }}
public class UIComRef : MonoBehaviour { public string RefName; public Image Image { get { return gameObject.GetComponent<Image>(); } } public Text Text { get { return gameObject.GetComponent<Text>(); } } public InputField Input { get { return gameObject.GetComponent<InputField>(); } } public Button Button { get { return gameObject.GetComponent<Button>(); } } public Toggle Toggle { get { return gameObject.GetComponent<Toggle>(); } } public Dropdown Dropdown { get { return gameObject.GetComponent<Dropdown>(); } } public ScrollRect ScrollRect { get { return gameObject.GetComponent<ScrollRect>(); } }}
public class UINotRef : MonoBehaviour{}
阅读全文
0 0
- Ref映射工具
- ref
- ref
- ref
- ref
- ref
- Ref
- ref
- ref
- Mybatis 高级结果映射Association Collection Cache-ref Discriminator Cache
- AutoMapper 自动映射工具
- 内外网映射工具
- 外网映射工具
- 端口二次映射工具
- 外网映射工具
- eclipse的hibernate映射工具
- 2.Component映射:工具类
- 2.集合映射:工具类
- java 代理模式
- 游戏开发中的人工智能(十四):神经网络
- 从程序员小白到大神的正确打开方式!
- Mac 终于有显示隐藏文件的快捷键了
- go闭包
- Ref映射工具
- Base64对字符串的加密解密
- 如何直接打开AutoCAD的DXF文件
- 链表面试题(1)----顺序表和链表的区别及其优缺点
- TcpListener(服务器)和TcpClient(客户端)
- App添加开发环境keystore文件,方便在开发环境下测试地图等功能
- 安卓提示控件(Sanckbar,alertdialog,Toast)用法
- solr入门教程
- 文件的拷贝