private static void GeneratorView(PanelCodeInfo panelCodeInfo) { Directory.CreateDirectory(_config.UIConfig.GenUIScriptsPath); var generateFilePath = Path.Combine(_config.UIConfig.GenUIScriptsPath, $"{panelCodeInfo.BehaviourName}.cs"); var strBuilder = new StringBuilder(); var template = File.Exists(generateFilePath) ? File.ReadAllText(generateFilePath) : Resources.Load <TextAsset>("ViewTemplate").text; string vmName = $"{panelCodeInfo.BehaviourName}VM"; template = template.Replace("#ClassName", panelCodeInfo.BehaviourName); template = template.Replace("#VMName", vmName); template = template.Replace("#PrefabPath", GetPanelPath(panelCodeInfo)); foreach (var uiMarks in panelCodeInfo.FieldFullPathToUIMark) { foreach (var uiMark in uiMarks.Value) { strBuilder.AppendLine($"\t[TransformPath(\"{uiMarks.Key}\")]"); strBuilder.AppendLine( $"\tprivate {uiMark.component.GetType().Name} {uiMark.fieldName};"); } } var markStr = strBuilder.ToString(); template = Regex.Replace(template, @"(#region Components\r*\n*)([\s\S]*?)(\s*?#endregion)", $"$1{markStr}$3"); File.WriteAllText(generateFilePath, template); }
private static void CreateCode(GameObject obj, string uiPrefabPath) { var prefabType = PrefabUtility.GetPrefabAssetType(obj); if (prefabType == PrefabAssetType.NotAPrefab) { return; } var clone = PrefabUtility.InstantiatePrefab(obj) as GameObject; if (null == clone) { return; } var panelCodeInfo = new PanelCodeInfo(); FillPanelInfo(clone.transform, uiPrefabPath, panelCodeInfo); Generate(panelCodeInfo); StartAddComponent2PrefabAfterCompile(obj); Object.DestroyImmediate(clone); }
private static void GeneratorVM(PanelCodeInfo panelCodeInfo) { string className = $"{panelCodeInfo.BehaviourName}VM"; //var generateFilePath = $"{BuildScript.GetSettings().uiScriptPath}{className}.cs"; var generateFilePath = ".cs"; if (File.Exists(generateFilePath)) { File.Delete(generateFilePath); } var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var strBuilder = new StringBuilder(); strBuilder.AppendLine("using Framework.UI.Core;"); strBuilder.AppendLine(); strBuilder.AppendLine($"public class {className} : ViewModel"); strBuilder.AppendLine("{"); strBuilder.AppendLine($"\tpublic {className}()"); strBuilder.AppendLine("\t{"); strBuilder.AppendLine(); strBuilder.AppendLine("\t}"); strBuilder.AppendLine(); strBuilder.AppendLine($"\tpublic override string ViewPath {{ get; }} = \"{GetPanelPath(panelCodeInfo)}\";"); strBuilder.AppendLine(); strBuilder.AppendLine($"\tpublic static {className} Create(VMCreator vmCreator)"); strBuilder.AppendLine("\t{"); strBuilder.AppendLine($"\t\t{className} vm = new {className}();"); strBuilder.AppendLine("\t\vmCreator?.BindView(vm);"); strBuilder.AppendLine("\t\treturn vm"); strBuilder.AppendLine("\t}"); strBuilder.AppendLine("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
private static void CreateCode(GameObject obj, string uiPrefabPath) { #pragma warning disable 618 var prefabType = PrefabUtility.GetPrefabType(obj); if (PrefabType.Prefab != prefabType) #pragma warning restore 618 { return; } var clone = PrefabUtility.InstantiatePrefab(obj) as GameObject; if (null == clone) { return; } var panelCodeInfo = new PanelCodeInfo(); FillPanelInfo(clone.transform, uiPrefabPath, panelCodeInfo); Generate(panelCodeInfo); StartAddComponent2PrefabAfterCompile(obj); //HotScriptBind(obj); Object.DestroyImmediate(clone); }
private static void GeneratorView(PanelCodeInfo panelCodeInfo) { //var dir = BuildScript.GetSettings().uiScriptPath; var dir = ""; Directory.CreateDirectory(dir); var generateFilePath = $"{dir}{panelCodeInfo.BehaviourName}.cs"; if (File.Exists(generateFilePath)) { File.Delete(generateFilePath); } var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var strBuilder = new StringBuilder(); strBuilder.AppendLine("using UnityEngine;"); strBuilder.AppendLine("using UnityEngine.UI;"); strBuilder.AppendLine("using Framework;"); strBuilder.AppendLine("using Framework.UI.Core;"); strBuilder.AppendLine(); strBuilder.AppendFormat("public class {0} : View", panelCodeInfo.BehaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("{"); strBuilder.AppendLine(); string vmName = $"{panelCodeInfo.BehaviourName}VM"; strBuilder.AppendLine($"\tprivate UIBindFactory<{panelCodeInfo.BehaviourName}, {vmName}> binding;"); strBuilder.AppendLine($"\tprivate {vmName} vm;"); foreach (var uiMarks in panelCodeInfo.FieldFullPathToUIMark.Values) { foreach (var uiMark in uiMarks) { strBuilder.Append( $"\t[SerializeField] private {uiMark.CurComponent.GetType().Name} {uiMark.FieldName};\n"); } } foreach (var element in panelCodeInfo.Elements) { strBuilder.Append( $"\t[SerializeField] private {element.BehaviourName} {element.BehaviourName};\n"); } strBuilder.AppendLine(); strBuilder.AppendLine(!panelCodeInfo.IsElement ? $"\tpublic override UILevel UILevel {{ get; }} = UILevel.Common;" : $"\tpublic override UILevel UILevel {{ get; }} = UILevel.None;"); strBuilder.AppendLine(); strBuilder.AppendLine("\tprotected override void OnVmChange()"); strBuilder.AppendLine("\t{"); strBuilder.AppendLine($"\t\tvm = ViewModel as {vmName};"); strBuilder.AppendLine("\t\tif (binding == null)"); strBuilder.AppendLine($"\t\t\tbinding = new UIBindFactory<{panelCodeInfo.BehaviourName}, {vmName}>(this, vm);"); strBuilder.AppendLine($"\t\tbinding.UpdateVm();"); strBuilder.AppendLine("\t}"); strBuilder.AppendLine("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
private static void GeneratorView(PanelCodeInfo panelCodeInfo) { //var dir = BuildScript.GetSettings().uiScriptPath; var dir = Application.dataPath; Directory.CreateDirectory(dir); var generateFilePath = $"{dir}/{panelCodeInfo.BehaviourName}.cs"; var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var strBuilder = new StringBuilder(); var template = File.ReadAllText(File.Exists(generateFilePath) ? generateFilePath : $"{Application.dataPath}/UI/Editor/ViewTemplate.txt"); string vmName = $"{panelCodeInfo.BehaviourName}VM"; template = template.Replace("#ClassName", panelCodeInfo.BehaviourName); template = template.Replace("#VMName", vmName); foreach (var uiMarks in panelCodeInfo.FieldFullPathToUIMark.Values) { foreach (var uiMark in uiMarks) { strBuilder.Append( $"\t[SerializeField] private {uiMark.CurComponent.GetType().Name} {uiMark.FieldName};\n"); } } foreach (var element in panelCodeInfo.Elements) { strBuilder.Append( $"\t[SerializeField] private {element.BehaviourName} {element.BehaviourName};\n"); } template = Regex.Replace(template, @"(?<=\t//#Components\n)([\n\s\w]*?)(?=\t//#Components)", strBuilder.ToString()); sw.Write(template); sw.Flush(); sw.Close(); }
private static string GetPanelPath(PanelCodeInfo panelCodeInfo) { if (CustomLoadPath != null) { return(CustomLoadPath(panelCodeInfo.PanelPath)); } return(panelCodeInfo.PanelPath); }
private static void Generate(PanelCodeInfo panelCodeInfo) { foreach (var element in panelCodeInfo.Elements) { Generate(element); } GeneratorView(panelCodeInfo); GeneratorVM(panelCodeInfo); }
private static string GetPanelPath(PanelCodeInfo panelCodeInfo) { var path = panelCodeInfo.PanelPath; //var rootPath = BuildScript.GetManifest().resRootPath; var rootPath = ""; if (path.Contains(rootPath)) { path = path.RemoveString(rootPath); } return(path); }
private static void FillPanelInfo(Transform transform, string prefabPath, PanelCodeInfo panelCodeInfo) { panelCodeInfo.BehaviourName = transform.name.Replace("(clone)", string.Empty); panelCodeInfo.PanelPath = prefabPath; var marks = transform.GetComponentsInChildren <UIMark>(); if (marks.Length <= 0) { return; } var elements = marks.Where(mark => mark._MarkType == UIMark.MarkType.Element); var elementsMark = new List <UIMark>(); foreach (var uiMark in elements) { elementsMark.Add(uiMark); if (uiMark.transform == transform) { continue; } PanelCodeInfo elementPanel = new PanelCodeInfo(); FillPanelInfo(uiMark.transform, prefabPath, elementPanel); elementPanel.IsElement = true; panelCodeInfo.Elements.Add(elementPanel); foreach (var elementMarks in elementPanel.FieldFullPathToUIMark.Values) { elementsMark.AddRange(elementMarks); } } foreach (var uiMark in marks) { if (uiMark.transform == transform && uiMark._MarkType == UIMark.MarkType.Element) { panelCodeInfo.BehaviourName = uiMark.FieldName; } if (elementsMark.Contains(uiMark)) { continue; } string fullPath = PathToParent(uiMark.transform, transform); if (!panelCodeInfo.FieldFullPathToUIMark.TryGetValue(fullPath, out var uiMarks)) { uiMarks = new List <UIMark>(); panelCodeInfo.FieldFullPathToUIMark.Add(fullPath, uiMarks); } uiMarks.Add(uiMark); } }
private static void GeneratorVM(PanelCodeInfo panelCodeInfo) { string className = $"{panelCodeInfo.BehaviourName}VM"; var generateFilePath = Path.Combine(_config.UIConfig.GenUIScriptsPath, $"{className}.cs"); if (File.Exists(generateFilePath)) { return; } var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var template = Resources.Load <TextAsset>("VMTemplate").text; template = template.Replace("#ClassName", className); sw.Write(template); sw.Flush(); sw.Close(); }
private static void GeneratorVM(PanelCodeInfo panelCodeInfo) { string className = $"{panelCodeInfo.BehaviourName}VM"; //var generateFilePath = $"{BuildScript.GetSettings().uiScriptPath}{className}.cs"; var generateFilePath = $"{Application.dataPath}/{panelCodeInfo.BehaviourName}VM.cs"; if (File.Exists(generateFilePath)) { return; } var sw = new StreamWriter(generateFilePath, false, Encoding.UTF8); var template = File.ReadAllText($"{Application.dataPath}/UI/Editor/VMTemplate.txt"); template = template.Replace("#ClassName", className); template = template.Replace("#PrefabPath", GetPanelPath(panelCodeInfo)); sw.Write(template); sw.Flush(); sw.Close(); }
private static void FillPanelInfo(Transform transform, string prefabPath, PanelCodeInfo panelCodeInfo) { panelCodeInfo.BehaviourName = transform.name.Replace("(clone)", string.Empty); panelCodeInfo.PanelPath = prefabPath; List <_uiMark> marks = new List <_uiMark>(); CollectMark(transform, ref marks); if (marks.Count <= 0) { return; } foreach (var uiMark in marks) { string fullPath = PathToParent(uiMark.transform, transform); if (!panelCodeInfo.FieldFullPathToUIMark.TryGetValue(fullPath, out var uiMarks)) { uiMarks = new List <_uiMark>(); panelCodeInfo.FieldFullPathToUIMark.Add(fullPath, uiMarks); } uiMarks.Add(uiMark); } }
private static void GeneratorView(PanelCodeInfo panelCodeInfo) { Directory.CreateDirectory(_config.UIConfig.GenUIScriptsPath); var fileName = $"{panelCodeInfo.BehaviourName}.cs"; var generateFilePath = Path.Combine(_config.UIConfig.GenUIScriptsPath, fileName); var strBuilder = new StringBuilder(); if (TryGetTemplate(fileName, Application.dataPath, out var tempPath)) { generateFilePath = tempPath; } var template = File.Exists(generateFilePath) ? File.ReadAllText(generateFilePath) : Resources.Load <TextAsset>("ViewTemplate").text; string vmName = $"{panelCodeInfo.BehaviourName}VM"; template = template.Replace("#ClassName", panelCodeInfo.BehaviourName); template = template.Replace("#VMName", vmName); template = template.Replace("#PrefabPath", GetPanelPath(panelCodeInfo)); foreach (var uiMarks in panelCodeInfo.FieldFullPathToUIMark) { foreach (var uiMark in uiMarks.Value) { var transformPath = uiMark.transform == panelCodeInfo.PanelGo.transform ? "" :uiMarks.Key; strBuilder.AppendLine($"\t[TransformPath(\"{transformPath}\")]"); var fieldName = uiMark.transform == panelCodeInfo.PanelGo.transform ? "self" : uiMark.fieldName; strBuilder.AppendLine( $"\tprotected {uiMark.component.GetType().FullName} {fieldName};"); } } var markStr = strBuilder.ToString() + "\t"; template = Regex.Replace(template, @"(#region Components\r*\n*)([\s\S]*?)(#endregion)", $"$1{markStr}$3"); File.WriteAllText(generateFilePath, template); }
private static string GetPanelPath(PanelCodeInfo panelCodeInfo) { var path = Path.GetFileNameWithoutExtension(panelCodeInfo.PanelPath); return(path); }
private static void Generate(PanelCodeInfo panelCodeInfo) { GeneratorView(panelCodeInfo); GeneratorVM(panelCodeInfo); }