private static void GenUICode(GameObject root, GameObjectData data) { List <GameObject> controls = new List <GameObject>(); // 装所有控件 List <GameObject> groups = new List <GameObject>(); // Traverse(root, controls, groups, false); Dictionary <string, int> usedNames = new Dictionary <string, int>(); //如果原先有代码生成,就先将原来的添加到gameobjectList中 List <string> gameobjectList = getGameobjectStrListByFlag(ToolsConst.UI_CODE_PATH + root.name + ".cs"); //这个是代码原有的 Dictionary <string, GameObject> gameObjectDic = TransListToDic(controls); //将所有的控件建立名字和GameObject映射,这个是重新生成的 string flag = string.Empty; if (gameobjectList.Count > 0) { for (int i = 0; i < gameobjectList.Count; i++) { if (gameObjectDic.ContainsKey(gameobjectList[i])) //如果有这个控件包含了列表该元素 { data.GameObjects.Add(gameObjectDic[gameobjectList[i]]); //添加这个对象到data中 flag += gameobjectList[i] + ","; //添加标记 gameObjectDic.Remove(gameobjectList[i]); //移除字典里该元素 //UnityEditor.EditorUtility.DisplayProgressBar(gameobjectList[i], "", (float)i/(float)); continue; } } } //这是原来代码没有的控件 foreach (KeyValuePair <string, GameObject> go in gameObjectDic) { data.GameObjects.Add(go.Value); flag += go.Value.name + ","; } //Generator Generator gen = new Generator(); //将标记写入文本中 if (flag.Contains(",")) { flag = flag.Substring(0, flag.LastIndexOf(",")); } gen.Println("//-L-<" + flag + ">"); gen.Println("using UnityEngine;"); gen.Println(); gen.Println("public partial class " + root.name + " : View"); gen.Println("{"); gen.AddIndent(); foreach (GameObject controlObject in data.GameObjects) //遍历控件对象 声明变量 { int times = 1; string name = controlObject.name; if (usedNames.TryGetValue(name, out times)) { name = name + "_" + times; ++times; } usedNames[controlObject.name] = times; GroupName groupName = controlObject.GetComponent <GroupName>(); //如果有UIGameObjectList组件 if (controlObject.GetComponent <UIGameObjectList>() != null) { gen.Printfln("private GameObject[] {0};", name); //声明这个数组 gen.Printfln("private GameObject {0}Obj;", name); } else if (groupName == null) //如果没有组 { gen.Println("private GameObject " + name + ";"); } else { gen.Printfln("private {0} {1};", groupName.groupName, name); } } gen.Println(); gen.Println("protected override void Awake()"); gen.Println("{"); gen.AddIndent(); gen.Println("base.Awake();"); gen.Println(); gen.Println("GameObjectData data = gameObject.GetComponent<GameObjectData>();"); usedNames.Clear(); for (int i = 0, max = data.GameObjects.Count; i < max; i++) { int times = 1; string name = data.GameObjects[i].name; if (usedNames.TryGetValue(name, out times)) { name = name + "_" + times; ++times; } usedNames[data.GameObjects[i].name] = times; GroupName groupName = data.GameObjects[i].GetComponent <GroupName>(); //如果有UIGameObjectList if (data.GameObjects[i].GetComponent <UIGameObjectList>() != null) { gen.Printfln("{0} = data.GameObjects[{1}].gameObject.GetComponent<UIGameObjectList>().objects;", name, i.ToString()); gen.Printfln("{0}Obj = data.GameObjects[{1}].gameObject;", name, i.ToString()); //gen.Printfln("{0} = transform.Find(@\"{1}\").gameObject.GetComponent<UIGameObjectList>().objects;", name, GetPath(controls[i], root)); //gen.Printfln("{0}Obj = transform.Find(@\"{1}\").gameObject;", name, GetPath(controls[i], root)); } else if (groupName == null) { gen.Println(name + " = data.GameObjects[" + i.ToString() + "].gameObject;"); //gen.Println(name + " = transform.Find(@\"" + GetPath(controls[i], root) + "\").gameObject;"); } else { gen.Printfln("{0} = View.AddComponentIfNotExist<{1}>(data.GameObjects[" + i.ToString() + "].gameObject);", name, groupName.groupName); //gen.Printfln("{0} = View.AddComponentIfNotExist<{2}>(transform.Find(@\"{1}\").gameObject);", name, GetPath(controls[i], root), groupName.groupName); } } gen.Println("ViewMgr.Ins.addView(this);"); gen.ReduceIndent(); gen.Println("}"); gen.Println(); HashSet <string> groupNames = new HashSet <string>(); foreach (GameObject group in groups) { string groupName = group.GetComponent <GroupName>().groupName; if (groupNames.Contains(groupName)) { continue; } GenGroupCode(group); //生成Group 代码 groupNames.Add(groupName); gen.Println(); } gen.ReduceIndent(); gen.Println("}"); StreamWriter sw = new StreamWriter(ToolsConst.UI_CODE_PATH + root.name + ".cs", false); sw.Write(gen.GetContent()); sw.Flush(); sw.Close(); }
/// <summary> /// 生成GroupName代码,这里可以改进生成内部类 /// </summary> /// <param name="root"></param> private static void GenGroupCode(GameObject root) { Generator gen = new Generator(); gen.Println("using UnityEngine;"); gen.Println(); List <GameObject> controls = new List <GameObject>(); List <GameObject> groups = new List <GameObject>(); Traverse(root, controls, groups, false); controls.Sort(new GameObjectNameCMP()); string clazzName = root.GetComponent <GroupName>().groupName; gen.Println("public partial class " + clazzName + " : MonoBehaviour"); gen.Println("{"); gen.AddIndent(); Dictionary <string, int> usedNames = new Dictionary <string, int>(); foreach (GameObject controlObject in controls) { int times = 1; string name = controlObject.name; if (usedNames.TryGetValue(name, out times)) { name = name + "_" + times; ++times; } usedNames[controlObject.name] = times; GroupName groupName = controlObject.GetComponent <GroupName>(); if (controlObject.GetComponent <UIGameObjectList>() != null) { gen.Printfln("public GameObject[] {0};", name); gen.Printfln("public GameObject {0}Obj;", name); } else if (groupName == null) { gen.Println("public GameObject " + name + ";"); } else if (controlObject.GetComponent <UIGameObjectList>() != null) { gen.Printfln("public GameObject[] {0};", name); } else { gen.Printfln("public {0} {1};", groupName.groupName, name); } } gen.Println(); gen.Println("private void Awake()"); gen.Println("{"); gen.AddIndent(); usedNames.Clear(); foreach (GameObject controlObject in controls) { int times = 1; string name = controlObject.name; if (usedNames.TryGetValue(name, out times)) { name = name + "_" + times; ++times; } usedNames[controlObject.name] = times; GroupName groupName = controlObject.GetComponent <GroupName>(); if (controlObject.GetComponent <UIGameObjectList>() != null) { gen.Printfln("{0} = transform.Find(@\"{1}\").gameObject.GetComponent<UIGameObjectList>().objects;", name, EditorTools.GetPath(controlObject, root)); gen.Printfln("{0}Obj = transform.Find(@\"{1}\").gameObject;", name, EditorTools.GetPath(controlObject, root)); } else if (groupName == null) { gen.Println(name + " = transform.Find(@\"" + EditorTools.GetPath(controlObject, root) + "\").gameObject;"); } else { gen.Printfln("{0} = View.AddComponentIfNotExist<{2}>(transform.Find(@\"{1}\").gameObject);", name, EditorTools.GetPath(controlObject, root), groupName.groupName); } } gen.ReduceIndent(); gen.Println("}"); gen.ReduceIndent(); gen.Println("}"); //组不会再递归新的组 StreamWriter sw = new StreamWriter(ToolsConst.UI_CODE_PATH + clazzName + ".cs", false); sw.Write(gen.GetContent()); sw.Flush(); sw.Close(); }