private void CreateCode(GameObject obj, string uiPrefabPath) { if (obj.IsNotNull()) { var prefabType = PrefabUtility.GetPrefabType(obj); if (PrefabType.Prefab != prefabType) { return; } var clone = PrefabUtility.InstantiatePrefab(obj) as GameObject; if (null == clone) { return; } var panelCodeInfo = new PanelCodeInfo(); Debug.Log(clone.name); panelCodeInfo.GameObjectName = clone.name.Replace("(clone)", string.Empty); BindCollector.SearchBinds(clone.transform, "", panelCodeInfo); CreateUIPanelCode(obj, uiPrefabPath, panelCodeInfo); UISerializer.StartAddComponent2PrefabAfterCompile(obj); HotScriptBind(obj); Object.DestroyImmediate(clone); } }
public static void Write(string name, string scriptsFolder, PanelCodeInfo panelCodeInfo, UIKitSettingData uiKitSettingData) { var scriptFile = scriptsFolder + "/{0}.Designer.cs".FillFormat(name); var writer = File.CreateText(scriptFile); writer.WriteLine("// Generate Id:{0}".FillFormat(Guid.NewGuid().ToString())); writer.WriteLine("using UnityEngine;"); writer.WriteLine(); if (uiKitSettingData.IsDefaultNamespace) { writer.WriteLine("// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间"); writer.WriteLine("// 2.命名空间更改后,生成代码之后,需要把逻辑代码文件(非 Designer)的命名空间手动更改"); } writer.WriteLine("namespace {0}".FillFormat(uiKitSettingData.Namespace)); writer.WriteLine("{"); writer.WriteLine("\tpublic partial class {0}".FillFormat(name)); writer.WriteLine("\t{"); foreach (var bindInfo in panelCodeInfo.BindInfos) { writer.WriteLine("\t\tpublic {0} {1};".FillFormat(bindInfo.BindScript.ComponentName, bindInfo.Name)); } writer.WriteLine(); writer.WriteLine("\t}"); writer.WriteLine("}"); writer.Close(); }
private void CreateUIPanelCode(GameObject uiPrefab, string uiPrefabPath, PanelCodeInfo panelCodeInfo) { if (null == uiPrefab) { return; } var behaviourName = uiPrefab.name; var strFilePath = CodeGenUtil.GenSourceFilePathFromPrefabPath(uiPrefabPath, behaviourName); if (mScriptKitInfo.IsNotNull()) { if (File.Exists(strFilePath) == false) { if (mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[0].IsNotNull()) { mScriptKitInfo.Templates[0].Generate(strFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), null); } } } else { if (File.Exists(strFilePath) == false) { UIPanelTemplate.Write(behaviourName, strFilePath, UIKitSettingData.Load().Namespace, UIKitSettingData.Load()); } } CreateUIPanelDesignerCode(behaviourName, strFilePath, panelCodeInfo); Debug.Log(">>>>>>>Success Create UIPrefab Code: " + behaviourName); }
public static void DoCreateCodeFromScene(GameObject gameObject, bool genUIKit = false) { if (!gameObject) { Debug.LogWarning("需要选择 GameObject"); return; } if (gameObject.GetComponent <Bind>() && !gameObject.GetComponent <ILKitBehaviour>()) { var parentController = gameObject.GetComponentInParent <ILKitBehaviour>(); if (parentController) { gameObject = parentController.gameObject; } } Debug.Log("Create Code"); var generateInfo = gameObject.GetComponent <ILKitBehaviour>(); var scriptsFolder = Application.dataPath + "/Scripts"; if (generateInfo) { scriptsFolder = generateInfo.ScriptsFolder; } scriptsFolder.CreateDirIfNotExists(); var panelCodeInfo = new PanelCodeInfo { GameObjectName = generateInfo.name }; Debug.Log(gameObject.transform); Debug.Log(panelCodeInfo.GameObjectName); // 搜索所有绑定 BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo, null, typeof(ILKitBehaviour)); if (genUIKit == false) { ILBehaviourCodeTemplate.Write(generateInfo.ScriptName, scriptsFolder, generateInfo.Namespace); ILBehaviourCodeDesignerTemplate.Write(generateInfo.ScriptName, scriptsFolder, panelCodeInfo, generateInfo.Namespace); } else { ILUIPanelCodeTemplate.Write(generateInfo.ScriptName, scriptsFolder, generateInfo.Namespace); ILUIPanelCodeDesignerTemplate.Write(generateInfo.ScriptName, scriptsFolder, panelCodeInfo, generateInfo.Namespace); } AssetDatabase.Refresh(); }
public static void DoCreateCodeFromScene(GameObject gameObject) { if (!gameObject) { Log.W("需要选择 GameObject"); return; } if (gameObject.GetComponent <AbstractBind>() && !gameObject.GetComponent <ViewController>()) { var parentController = gameObject.GetComponentInParent <ViewController>(); if (parentController) { gameObject = parentController.gameObject; } } Log.I("Create Code"); var generateInfo = gameObject.GetComponent <ViewController>(); var scriptsFolder = Application.dataPath + "/Scripts"; if (generateInfo) { scriptsFolder = generateInfo.ScriptsFolder; } scriptsFolder.CreateDirIfNotExists(); var panelCodeInfo = new PanelCodeInfo { GameObjectName = generateInfo.name }; // 搜索所有绑定 BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo); var uikitSettingData = UIKitSettingData.Load(); ViewControllerTemplate.Write(generateInfo.ScriptName, scriptsFolder, generateInfo.Namespace, uikitSettingData); ViewControllerDesignerTemplate.Write(generateInfo.ScriptName, scriptsFolder, generateInfo.Namespace, panelCodeInfo, uikitSettingData); EditorPrefs.SetString("GENERATE_CLASS_NAME", generateInfo.ScriptName); EditorPrefs.SetString("GENERATE_NAMESPACE", generateInfo.Namespace.IsTrimNullOrEmpty() ? uikitSettingData.Namespace : generateInfo.Namespace); EditorPrefs.SetString("GAME_OBJECT_NAME", gameObject.name); AssetDatabase.Refresh(); }
static void CreateCode() { var gameObject = Selection.objects.First() as GameObject; if (!gameObject) { Debug.LogWarning("需要选择 GameObject"); return; } if (gameObject.GetComponent <Bind>() && !gameObject.GetComponent <ViewController>()) { var parentController = gameObject.GetComponentInParent <ViewController>(); if (parentController) { gameObject = parentController.gameObject; } } Debug.Log("Create Code"); var generateInfo = gameObject.GetComponent <ViewController>(); var scriptsFolder = Application.dataPath + "/Scripts"; if (generateInfo) { scriptsFolder = generateInfo.ScriptsFolder; } scriptsFolder.CreateDirIfNotExists(); var panelCodeInfo = new PanelCodeInfo(); panelCodeInfo.GameObjectName = generateInfo.name; // 搜索所有绑定 BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo); var uikitSettingData = UIKitSettingData.Load(); ViewControllerTemplate.Write(generateInfo.ScriptName, scriptsFolder, uikitSettingData); ViewControllerDesignerTemplate.Write(generateInfo.ScriptName, scriptsFolder, panelCodeInfo, uikitSettingData); EditorPrefs.SetString("GENERATE_CLASS_NAME", generateInfo.ScriptName); EditorPrefs.SetString("GAME_OBJECT_NAME", gameObject.name); AssetDatabase.Refresh(); }
private void CreateUIPanelCode(GameObject uiPrefab, string uiPrefabPath, PanelCodeInfo panelCodeInfo) { if (null == uiPrefab) { return; } var behaviourName = uiPrefab.name; var strFilePath = CodeGenUtil.GenSourceFilePathFromPrefabPath(uiPrefabPath, behaviourName); if (mScriptKitInfo.IsNotNull()) { if (File.Exists(strFilePath) == false) { if (mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[0].IsNotNull()) { mScriptKitInfo.Templates[0].Generate(strFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), null); } } } else { if (File.Exists(strFilePath) == false) { RegisteredTemplateGeneratorsFactory.RegisterTemplate <PanelCodeInfo, UIPanelDataTemplate>(); RegisteredTemplateGeneratorsFactory.RegisterTemplate <PanelCodeInfo, UIPanelTemplate>(); var factory = new RegisteredTemplateGeneratorsFactory(); var generators = factory.CreateGenerators(new UIGraph(), panelCodeInfo); CompilingSystem.GenerateFile(new FileInfo(strFilePath), new CodeFileGenerator(UIKitSettingData.GetProjectNamespace()) { Generators = generators.ToArray() }); RegisteredTemplateGeneratorsFactory.UnRegisterTemplate <PanelCodeInfo>(); } } CreateUIPanelDesignerCode(behaviourName, strFilePath, panelCodeInfo); Debug.Log(">>>>>>>Success Create UIPrefab Code: " + behaviourName); }
static void AddComponent2GameObject() { var generateClassName = EditorPrefs.GetString("GENERATE_CLASS_NAME"); var gameObjectName = EditorPrefs.GetString("GAME_OBJECT_NAME"); var generateNamespace = EditorPrefs.GetString("GENERATE_NAMESPACE"); if (string.IsNullOrEmpty(generateClassName)) { EditorPrefs.DeleteKey("GENERATE_CLASS_NAME"); EditorPrefs.DeleteKey("GAME_OBJECT_NAME"); } else { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp"); var typeName = generateNamespace + "." + generateClassName; var type = defaultAssembly.GetType(typeName); if (type == null) { Log.I("编译失败"); return; } Log.I(type); var gameObject = GameObject.Find(gameObjectName); if (!gameObject) { Log.I("上次的 View Controller 生成失败,找不到 GameObject:{0}".FillFormat(gameObjectName)); Clear(); return; } var scriptComponent = gameObject.GetComponent(type); if (!scriptComponent) { scriptComponent = gameObject.AddComponent(type); } var serialiedScript = new SerializedObject(scriptComponent); var panelCodeInfo = new PanelCodeInfo(); panelCodeInfo.GameObjectName = gameObjectName; // 搜索所有绑定 BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo); foreach (var bindInfo in panelCodeInfo.BindInfos) { var name = bindInfo.Name; var componentName = bindInfo.BindScript.ComponentName.Split('.').Last(); serialiedScript.FindProperty(name).objectReferenceValue = gameObject.transform.Find(bindInfo.PathToElement) .GetComponent(componentName); } var codeGenerateInfo = gameObject.GetComponent <ViewController>(); if (codeGenerateInfo) { serialiedScript.FindProperty("ScriptsFolder").stringValue = codeGenerateInfo.ScriptsFolder; serialiedScript.FindProperty("PrefabFolder").stringValue = codeGenerateInfo.PrefabFolder; serialiedScript.FindProperty("GeneratePrefab").boolValue = codeGenerateInfo.GeneratePrefab; serialiedScript.FindProperty("ScriptName").stringValue = codeGenerateInfo.ScriptName; serialiedScript.FindProperty("Namespace").stringValue = codeGenerateInfo.Namespace; var generatePrefab = codeGenerateInfo.GeneratePrefab; var prefabFolder = codeGenerateInfo.PrefabFolder; var fullPrefabFolder = prefabFolder.Replace("Assets", Application.dataPath); if (codeGenerateInfo.GetType() != type) { DestroyImmediate(codeGenerateInfo, false); } serialiedScript.ApplyModifiedPropertiesWithoutUndo(); if (generatePrefab) { fullPrefabFolder.CreateDirIfNotExists(); var genereateFolder = prefabFolder + "/" + gameObject.name + ".prefab"; PrefabUtils.SaveAndConnect(genereateFolder, gameObject); } } else { serialiedScript.FindProperty("ScriptsFolder").stringValue = "Assets/Scripts"; serialiedScript.ApplyModifiedPropertiesWithoutUndo(); } Clear(); EditorUtils.MarkCurrentSceneDirty(); } }
private void CreateUIPanelDesignerCode(string behaviourName, string uiUIPanelfilePath, PanelCodeInfo panelCodeInfo) { var dir = uiUIPanelfilePath.Replace(behaviourName + ".cs", ""); var generateFilePath = dir + behaviourName + ".Designer.cs"; if (mScriptKitInfo.IsNotNull()) { if (mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[1].IsNotNull()) { mScriptKitInfo.Templates[1].Generate(generateFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), panelCodeInfo); } mScriptKitInfo.HotScriptFilePath.CreateDirIfNotExists(); mScriptKitInfo.HotScriptFilePath = mScriptKitInfo.HotScriptFilePath + "/" + behaviourName + mScriptKitInfo.HotScriptSuffix; if (File.Exists(mScriptKitInfo.HotScriptFilePath) == false && mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[2].IsNotNull()) { mScriptKitInfo.Templates[2].Generate(mScriptKitInfo.HotScriptFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), panelCodeInfo); } } else { RegisteredTemplateGeneratorsFactory.RegisterTemplate <PanelCodeInfo, UIPanelDesignerTemplate>(); var factory = new RegisteredTemplateGeneratorsFactory(); var generators = factory.CreateGenerators(new UIGraph(), panelCodeInfo); CompilingSystem.GenerateFile(new FileInfo(generateFilePath), new CodeFileGenerator(UIKitSettingData.GetProjectNamespace()) { Generators = generators.ToArray() }); RegisteredTemplateGeneratorsFactory.UnRegisterTemplate <PanelCodeInfo>(); } foreach (var elementCodeData in panelCodeInfo.ElementCodeDatas) { var elementDir = string.Empty; elementDir = elementCodeData.BindInfo.BindScript.GetBindType() == BindType.Element ? (dir + behaviourName + "/").CreateDirIfNotExists() : (Application.dataPath + "/" + UIKitSettingData.GetScriptsPath() + "/Components/").CreateDirIfNotExists(); CreateUIElementCode(elementDir, elementCodeData); } }
public static void Generate(string generateFilePath, string behaviourName, string nameSpace, PanelCodeInfo panelCodeInfo) { var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var strBuilder = new StringBuilder(); strBuilder.AppendLine("using UnityEngine;"); strBuilder.AppendLine("using UnityEngine.UI;"); strBuilder.AppendLine("using QFramework;"); strBuilder.AppendLine(); strBuilder.AppendLine("namespace " + nameSpace); strBuilder.AppendLine("{"); strBuilder.AppendFormat("\tpublic partial class {0}", behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("\t{"); strBuilder.AppendFormat("\t\tpublic const string NAME = \"{0}\";\n", behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("\t\tpublic Transform transform;"); strBuilder.AppendLine(); strBuilder.AppendLine("\t\tpublic GameObject gameObject { get { return transform.gameObject; }}"); strBuilder.AppendLine(); strBuilder.AppendLine("\t\tpublic ILRuntimePanel Behaviour; // TODO:这里要改成继承的"); strBuilder.AppendLine(); strBuilder.AppendFormat("\t\tpublic {0}(Transform trans)\n", behaviourName); strBuilder.AppendLine("\t\t{"); strBuilder.AppendLine("\t\t\ttransform = trans;"); strBuilder.AppendLine("\t\t\tBehaviour = trans.GetComponent<ILRuntimePanel>();"); strBuilder.AppendLine("\t\t\tInternalInitView(transform);"); strBuilder.AppendLine("\t\t\tInitUI();"); strBuilder.AppendLine("\t\t\tRegisterUIEvent();"); strBuilder.AppendLine("\t\t}"); strBuilder.AppendLine(); strBuilder.AppendLine("\t\tpublic void CloseSelf()"); strBuilder.AppendLine("\t\t{"); strBuilder.AppendLine("\t\t\tUIMgr.ClosePanel(NAME);"); strBuilder.AppendLine("\t\t}"); strBuilder.AppendLine(); strBuilder.AppendLine("\t\tpublic void InternalClose()"); strBuilder.AppendLine("\t\t{"); strBuilder.AppendLine("\t\t\tInternalClearView();"); strBuilder.AppendLine("\t\t\tOnClose();"); strBuilder.AppendLine("\t\t}"); strBuilder.AppendLine(); foreach (var markInfo in panelCodeInfo.BindInfos) { var strUIType = markInfo.BindScript.ComponentName; strBuilder.AppendFormat("\t\tpublic {0} {1};\r\n", strUIType, markInfo.Name); } strBuilder.AppendLine(); strBuilder.Append("\t\t").AppendLine("private void InternalInitView(Transform transform)"); strBuilder.Append("\t\t").AppendLine("{"); foreach (var markInfo in panelCodeInfo.BindInfos) { var strUIType = markInfo.BindScript.ComponentName; strBuilder.AppendFormat("\t\t\t{0} = transform.Find(\"{1}\").GetComponent<{2}>();\r\n", markInfo.Name, markInfo.PathToElement, strUIType); } strBuilder.Append("\t\t").AppendLine("}"); strBuilder.AppendLine(); strBuilder.Append("\t\t").AppendLine("public void InternalClearView()"); strBuilder.Append("\t\t").AppendLine("{"); foreach (var markInfo in panelCodeInfo.BindInfos) { strBuilder.AppendFormat("\t\t\t{0} = null;\r\n", markInfo.Name); } strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.AppendLine("\t}"); strBuilder.AppendLine(); strBuilder.AppendFormat("\tpublic static class {0}Extention\n", behaviourName); strBuilder.AppendLine("\t{"); strBuilder.AppendFormat("\t\tpublic static {0} As{0}(this PTUIBehaviour selfRuntimePanel)\n", behaviourName); strBuilder.AppendLine("\t\t{"); strBuilder.AppendFormat("\t\t\treturn selfRuntimePanel.AsiIlRuntimePanel().ILObject as {0};\n", behaviourName); strBuilder.AppendLine("\t\t}"); strBuilder.AppendLine("\t}"); strBuilder.AppendLine("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
private void CreateUIPanelDesignerCode(string behaviourName, string uiUIPanelfilePath, PanelCodeInfo panelCodeInfo) { var dir = uiUIPanelfilePath.Replace(behaviourName + ".cs", ""); var generateFilePath = dir + behaviourName + ".Designer.cs"; if (mScriptKitInfo.IsNotNull()) { if (mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[1].IsNotNull()) { mScriptKitInfo.Templates[1].Generate(generateFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), panelCodeInfo); } mScriptKitInfo.HotScriptFilePath.CreateDirIfNotExists(); mScriptKitInfo.HotScriptFilePath = mScriptKitInfo.HotScriptFilePath + "/" + behaviourName + mScriptKitInfo.HotScriptSuffix; if (File.Exists(mScriptKitInfo.HotScriptFilePath) == false && mScriptKitInfo.Templates.IsNotNull() && mScriptKitInfo.Templates[2].IsNotNull()) { mScriptKitInfo.Templates[2].Generate(mScriptKitInfo.HotScriptFilePath, behaviourName, UIKitSettingData.GetProjectNamespace(), panelCodeInfo); } } else { UIPanelDesignerTemplate.Write(behaviourName, dir, UIKitSettingData.GetProjectNamespace(), panelCodeInfo, UIKitSettingData.Load()); } foreach (var elementCodeData in panelCodeInfo.ElementCodeDatas) { var elementDir = string.Empty; elementDir = elementCodeData.BindInfo.BindScript.GetBindType() == BindType.Element ? (dir + behaviourName + "/").CreateDirIfNotExists() : (Application.dataPath + "/" + UIKitSettingData.GetScriptsPath() + "/Components/").CreateDirIfNotExists(); CreateUIElementCode(elementDir, elementCodeData); } }
static void AddComponent2GameObject() { // Debug.Log("DidReloadScripts"); var generateClassName = EditorPrefs.GetString("GENERATE_CLASS_NAME"); var gameObjectName = EditorPrefs.GetString("GAME_OBJECT_NAME"); // Debug.Log(generateClassName); if (string.IsNullOrEmpty(generateClassName)) { // Debug.Log("不继续操作"); EditorPrefs.DeleteKey("GENERATE_CLASS_NAME"); EditorPrefs.DeleteKey("GAME_OBJECT_NAME"); } else { // Debug.Log("继续操作"); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp"); var typeName = UIKitSettingData.Load().Namespace + "." + generateClassName; var type = defaultAssembly.GetType(typeName); if (type == null) { Debug.Log("编译失败"); return; } Debug.Log(type); var gameObject = GameObject.Find(gameObjectName); var scriptComponent = gameObject.GetComponent(type); if (!scriptComponent) { scriptComponent = gameObject.AddComponent(type); } var serialiedScript = new SerializedObject(scriptComponent); var panelCodeInfo = new PanelCodeInfo(); panelCodeInfo.GameObjectName = gameObjectName; // 搜索所有绑定 BindCollector.SearchBinds(gameObject.transform, "", panelCodeInfo); foreach (var bindInfo in panelCodeInfo.BindInfos) { var name = bindInfo.Name; var componentName = bindInfo.BindScript.ComponentName.Split('.').Last(); serialiedScript.FindProperty(name).objectReferenceValue = gameObject.transform.Find(bindInfo.PathToElement) .GetComponent(componentName); } var codeGenerateInfo = gameObject.GetComponent <ViewController>(); if (codeGenerateInfo) { serialiedScript.FindProperty("ScriptsFolder").stringValue = codeGenerateInfo.ScriptsFolder; serialiedScript.FindProperty("PrefabFolder").stringValue = codeGenerateInfo.PrefabFolder; serialiedScript.FindProperty("GeneratePrefab").boolValue = codeGenerateInfo.GeneratePrefab; serialiedScript.FindProperty("ScriptName").stringValue = codeGenerateInfo.ScriptName; var generatePrefab = codeGenerateInfo.GeneratePrefab; var prefabFolder = codeGenerateInfo.PrefabFolder; var fullPrefabFolder = prefabFolder.Replace("Assets", Application.dataPath); if (codeGenerateInfo.GetType() != type) { DestroyImmediate(codeGenerateInfo, false); } serialiedScript.ApplyModifiedPropertiesWithoutUndo(); if (generatePrefab) { fullPrefabFolder.CreateDirIfNotExists(); var genereateFolder = fullPrefabFolder + "/" + gameObject.name + ".prefab"; #if UNITY_2018_3_OR_NEWER PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, fullPrefabFolder + "/" + gameObject.name + ".prefab", InteractionMode.AutomatedAction); #else genereateFolder = prefabFolder + "/" + gameObject.name + ".prefab"; PrefabUtility.CreatePrefab(genereateFolder, gameObject, ReplacePrefabOptions.ConnectToPrefab); #endif } } else { serialiedScript.FindProperty("ScriptsFolder").stringValue = "Assets/Scripts"; serialiedScript.ApplyModifiedPropertiesWithoutUndo(); } EditorPrefs.DeleteKey("GENERATE_CLASS_NAME"); EditorPrefs.DeleteKey("GAME_OBJECT_NAME"); EditorUtils.MarkCurrentSceneDirty(); } }
public static void Write(string name, string scriptsFolder, string scriptNamespace, PanelCodeInfo panelCodeInfo, UIKitSettingData uiKitSettingData) { var scriptFile = scriptsFolder + "/{0}.Designer.cs".FillFormat(name); var writer = File.CreateText(scriptFile); var root = new RootCode() .Using("System") .Using("UnityEngine") .Using("UnityEngine.UI") .Using("QFramework") .EmptyLine() .Namespace(scriptNamespace.IsTrimNullOrEmpty() ? uiKitSettingData.Namespace : scriptNamespace, ns => { ns.Custom("// Generate Id:{0}".FillFormat(Guid.NewGuid().ToString())); ns.Class(name, null, true, false, (classScope) => { classScope.Custom("public const string Name = \"" + name + "\";"); classScope.EmptyLine(); foreach (var bindInfo in panelCodeInfo.BindInfos) { if (bindInfo.BindScript.Comment.IsNotNullAndEmpty()) { classScope.Custom("/// <summary>"); classScope.Custom("/// " + bindInfo.BindScript.Comment); classScope.Custom("/// </summary>"); } classScope.Custom("[SerializeField]"); classScope.Custom("public " + bindInfo.BindScript.ComponentName + " " + bindInfo.Name + ";"); } classScope.EmptyLine(); classScope.Custom("private " + name + "Data mPrivateData = null;"); classScope.EmptyLine(); classScope.CustomScope("protected override void ClearUIComponents()", false, (function) => { foreach (var bindInfo in panelCodeInfo.BindInfos) { function.Custom(bindInfo.Name + " = null;"); } function.EmptyLine(); function.Custom("mData = null;"); }); classScope.EmptyLine(); classScope.CustomScope("public " + name + "Data Data", false, (property) => { property.CustomScope("get", false, (getter) => { getter.Custom("return mData;"); }); }); classScope.EmptyLine(); classScope.CustomScope(name + "Data mData", false, (property) => { property.CustomScope("get", false, (getter) => { getter.Custom("return mPrivateData ?? (mPrivateData = new " + name + "Data());"); }); property.CustomScope("set", false, (setter) => { setter.Custom("mUIData = value;"); setter.Custom("mPrivateData = value;"); }); }); }); }); var codeWriter = new FileCodeWriter(writer); root.Gen(codeWriter); codeWriter.Dispose(); }
/// <summary> /// /// </summary> /// <param name="rootTrans"></param> /// <param name="curTrans"></param> /// <param name="transFullName"></param> public static void SearchBinds(Transform curTrans, string transFullName, PanelCodeInfo panelCodeInfo = null, ElementCodeInfo parentElementCodeInfo = null) { foreach (Transform childTrans in curTrans) { var uiMark = childTrans.GetComponent <IBind>(); if (null != uiMark) { if (null == parentElementCodeInfo) { if (!panelCodeInfo.BindInfos.Any(markedObjInfo => markedObjInfo.Name.Equals(uiMark.Transform.name))) { panelCodeInfo.BindInfos.Add(new BindInfo { Name = uiMark.Transform.name, BindScript = uiMark, PathToElement = CodeGenUtil.PathToParent(childTrans, panelCodeInfo.GameObjectName) }); panelCodeInfo.DicNameToFullName.Add(uiMark.Transform.name, transFullName + childTrans.name); } else { Debug.LogError("Repeat key: " + childTrans.name); } } else { if (!parentElementCodeInfo.BindInfos.Any(markedObjInfo => markedObjInfo.Name.Equals(uiMark.Transform.name))) { parentElementCodeInfo.BindInfos.Add(new BindInfo() { Name = uiMark.Transform.name, BindScript = uiMark, PathToElement = CodeGenUtil.PathToParent(childTrans, parentElementCodeInfo.BehaviourName) }); parentElementCodeInfo.DicNameToFullName.Add(uiMark.Transform.name, transFullName + childTrans.name); } else { Debug.LogError("Repeat key: " + childTrans.name); } } if (uiMark.GetBindType() != BindType.DefaultUnityElement) { var elementCodeData = new ElementCodeInfo { BehaviourName = uiMark.ComponentName, BindInfo = new BindInfo { BindScript = uiMark } }; if (null == parentElementCodeInfo) { panelCodeInfo.ElementCodeDatas.Add(elementCodeData); } else { parentElementCodeInfo.ElementCodeDatas.Add(elementCodeData); } SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, elementCodeData); } else { SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, parentElementCodeInfo); } } else { SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, parentElementCodeInfo); } } }
public static void Write(string name, string scriptsFolder, PanelCodeInfo panelCodeInfo, string ns) { var scriptFile = scriptsFolder + "/{0}.Designer.cs".FillFormat(name); var writer = File.CreateText(scriptFile); writer.WriteLine("// Generate Id:{0}".FillFormat(Guid.NewGuid().ToString())); writer.WriteLine("using UnityEngine;"); writer.WriteLine("using QFramework;"); writer.WriteLine(); writer.WriteLine("namespace {0}".FillFormat(ns)); writer.WriteLine("{"); writer.WriteLine("\tpublic partial class {0}".FillFormat(name)); writer.WriteLine("\t{"); writer.WriteLine("\t\tpublic const string NAME = \"{0}\";\n", name); foreach (var bindInfo in panelCodeInfo.BindInfos) { writer.WriteLine("\t\tpublic {0} {1};".FillFormat(bindInfo.BindScript.ComponentName, bindInfo.Name)); } writer.WriteLine("\t\tpublic Transform transform { get; set; }"); writer.WriteLine("\t\tpublic GameObject gameObject { get; set; }"); writer.WriteLine("\t\tpublic ILKitBehaviour MonoBehaviour { get; set; }"); writer.WriteLine(); writer.WriteLine("\t\tpublic static void Start(ILKitBehaviour ilkitBehaviour)"); writer.WriteLine("\t\t{"); writer.WriteLine("\t\t\tvar ilBehaviour = new {0}".FillFormat(name)); writer.WriteLine("\t\t\t{"); writer.WriteLine("\t\t\t\ttransform = ilkitBehaviour.transform,"); writer.WriteLine("\t\t\t\tgameObject = ilkitBehaviour.gameObject,"); writer.WriteLine("\t\t\t\tMonoBehaviour = ilkitBehaviour"); writer.WriteLine("\t\t\t};"); writer.WriteLine(); writer.WriteLine("\t\t\tilkitBehaviour.Script = ilBehaviour;"); writer.WriteLine(); writer.WriteLine("\t\t\tilBehaviour.SetupBinds();"); writer.WriteLine("\t\t\tilBehaviour.OnStart();"); writer.WriteLine(); writer.WriteLine("\t\t\tilkitBehaviour.OnDestroyAction += ilBehaviour.DestroyScript;"); writer.WriteLine("\t\t}"); writer.WriteLine(); writer.WriteLine("\t\tvoid SetupBinds()"); writer.WriteLine("\t\t{"); foreach (var bindInfo in panelCodeInfo.BindInfos) { writer.WriteLine("\t\t\t{0} = transform.Find(\"{1}\").GetComponent<{2}>();" .FillFormat(bindInfo.Name, bindInfo.PathToElement, bindInfo.BindScript.ComponentName)); } writer.WriteLine("\t\t}"); writer.WriteLine(); writer.WriteLine("\t\tvoid ClearBinds()"); writer.WriteLine("\t\t{"); foreach (var bindInfo in panelCodeInfo.BindInfos) { writer.WriteLine("\t\t\t{0} = null;" .FillFormat(bindInfo.Name)); } writer.WriteLine("\t\t}"); writer.WriteLine(); writer.WriteLine("\t\tvoid DestroyScript()"); writer.WriteLine("\t\t{"); writer.WriteLine("\t\t\tOnDestroy();"); writer.WriteLine(); writer.WriteLine("\t\t\tClearBinds();"); writer.WriteLine(); writer.WriteLine("\t\t\ttransform = null;"); writer.WriteLine("\t\t\tgameObject = null;"); writer.WriteLine("\t\t\tMonoBehaviour = null;"); writer.WriteLine("\t\t}"); writer.WriteLine("\t}"); writer.WriteLine("}"); writer.Close(); }