//生成界面代码 public void GenViewScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string viewClassName = uiType.Name + "View"; if (File.Exists(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath, viewClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + viewClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIView.uiViewNameSpace, viewClassName); gener.AddBaseType(typeof(UIView).Name) .AddImport("ResetCore.UGUI"); rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIView.genableSign)) { return; } string goName = go.name.Replace(UIView.genableSign, string.Empty); if (goName.Contains(splitGoNameAndProperty[0])) { goName = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries)[0]; } gener.AddMemberProperty(typeof(GameObject), UIView.goName + goName); var coms = go.GetComponents <Component>(); foreach (var com in coms) { Type comType = com.GetType(); if (!UIView.uiCompTypeList.Contains(comType)) { continue; } gener.AddMemberProperty(comType, UIView.comNameDict[comType] + goName); } }); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath)); }
private void ShowInfo() { BaseUI ui = target as BaseUI; if (ui is BaseNormalUI) { GUILayout.Label("This is Normal UI", GUIHelper.MakeHeader(30)); } if (ui is BasePopupUI) { GUILayout.Label("This is Popup UI", GUIHelper.MakeHeader(30)); } if (ui is BaseTopUI) { GUILayout.Label("This is Top UI", GUIHelper.MakeHeader(30)); } EditorGUILayout.HelpBox("You can Edit UI Property Here\n" + "UI Path is \"" + UIConst.uiPrefabPath + "\" now", MessageType.Info); }
private void CheckPrefab() { BaseUI ui = target as BaseUI; string uiPrefabPath = UIConst.uiPrefabPath; if (!UIConst.UIPrefabNameDic.ContainsKey(ui.uiName)) { EditorGUILayout.HelpBox("There is no prefab name setted! Open the UIConst To Issue this problem.", MessageType.Error); if (GUILayout.Button("Open UIConst")) { Object obj = EditorResources.GetAsset <Object>("UIConst", "ResetCore", "UGUI") as Object; AssetDatabase.OpenAsset(obj); } return; } string prefabNameWithEx = UIConst.UIPrefabNameDic[ui.uiName]; string prefabName = Path.GetFileNameWithoutExtension(prefabNameWithEx); //检查是否是Prefab if (PrefabUtility.GetPrefabType(ui.gameObject) != PrefabType.PrefabInstance && PrefabUtility.GetPrefabType(ui.gameObject) != PrefabType.Prefab) { GameObject prefabAlreadyExist = EditorResources.GetAsset <GameObject>(prefabName, "Resources", uiPrefabPath) as GameObject; if (prefabAlreadyExist == null) { EditorGUILayout.HelpBox("Lose prefab! You should Create a Prefab", MessageType.Warning); if (GUILayout.Button("Create Prefab")) { string path = PathEx.Combine(PathConfig.assetResourcePath, uiPrefabPath, prefabNameWithEx).Replace("\\", "/"); string absolutePath = PathEx.Combine(PathConfig.resourcePath, uiPrefabPath, prefabNameWithEx); PathEx.MakeFileDirectoryExist(absolutePath); Debug.unityLogger.Log("Create a ui prefab : " + path); GameObject go = PrefabUtility.CreatePrefab(path, ui.gameObject); PrefabUtility.ConnectGameObjectToPrefab(ui.gameObject, go); AssetDatabase.Refresh(); } } else { EditorGUILayout.HelpBox("There is already a same name prefab there!", MessageType.Error); if (GUILayout.Button("Check Prefab")) { Debug.unityLogger.Log(EditorResources.GetFullPath(prefabName, "Resources", uiPrefabPath)); //Selection.activeObject = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Prefabs/Cube.prefab"); EditorGUIUtility.PingObject(prefabAlreadyExist); Selection.activeObject = prefabAlreadyExist; } } } else { Object prefabObj; string prefabAssetName; if (PrefabUtility.GetPrefabType(ui) == PrefabType.Prefab) { prefabObj = ui; prefabAssetName = ui.gameObject.name; Debug.Log(ui.gameObject.name); } else { prefabObj = PrefabUtility.GetPrefabParent(ui.gameObject); prefabAssetName = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(prefabObj.GetInstanceID())); } //检查Prefab名与UIName类型是否匹配 if (prefabName != prefabAssetName) { EditorGUILayout.HelpBox("Your prefab name is not fit your UITypeName, Please reselect!\n" + "UIName = " + prefabName + " but \nPrefabName = " + prefabAssetName, MessageType.Error); UIConst.UIName?fitUIName = null; foreach (KeyValuePair <UIConst.UIName, string> kvp in UIConst.UIPrefabNameDic) { if (kvp.Value == prefabAssetName + ".prefab") { fitUIName = kvp.Key; } } if (fitUIName != null) { if (GUILayout.Button("Fix Issue")) { ui.uiName = (UIConst.UIName)fitUIName; } } } } }
//生成数据代码 public void GenModelScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string modelClassName = uiType.Name + "Model"; if (File.Exists(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath, modelClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + modelClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIModel.uiModelNameSpace, modelClassName); gener.AddBaseType(typeof(UIModel).Name) .AddImport("ResetCore.UGUI") .AddImport("ResetCore.Event"); List <string> hasAddedProperty = new List <string>(); CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIModel.genableSign)) { return; } string goName = go.name.Replace(UIModel.genableSign, string.Empty); string propertyName; string typeName; if (!goName.Contains(splitGoNameAndProperty[0])) { return; } string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries); goName = tempstrs[0]; if (!tempstrs[1].Contains(splitComponentAndProperty[0])) { EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的"); return; } propertyName = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries)[1]; if (!propertyName.Contains(splitPropertyAndType.ToString())) { typeName = "EventProperty<string>"; } tempstrs = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries); propertyName = tempstrs[0]; typeName = "EventProperty<" + tempstrs[1] + ">"; if (!hasAddedProperty.Contains(propertyName)) { gener.AddMemberProperty(typeName, propertyName); hasAddedProperty.Add(propertyName); constructor.Statements.AddRange(new CodeStatement[] { new CodeCommentStatement("From " + go.name), new CodeSnippetStatement(propertyName + " = new " + typeName + "();") }); } }); gener.newClass.Members.Add(constructor); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath)); }
//生成自动绑定代码 public void GenBindScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string binderClassName = uiType.Name + "Binder"; if (File.Exists(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath, binderClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + binderClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIBinder.uiBinderNameSpace, binderClassName); gener.AddBaseType(typeof(UIBinder).Name) .AddImport("ResetCore.UGUI") .AddImport("ResetCore.Event") .AddImport(UIView.uiViewNameSpace) .AddImport(UIModel.uiModelNameSpace); CodeMemberMethod initMethod = new CodeMemberMethod(); initMethod.AddComment("Used to bind view and model", true); initMethod.Name = "Bind"; string viewClassName = uiType.Name + "View"; string modelClassName = uiType.Name + "Model"; initMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIView).Name), "view"), new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIModel).Name), "model"), }); initMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override; List <string> hasAddedProperty = new List <string>(); rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIBinder.genableSign)) { return; } string goName = go.name.Replace(UIBinder.genableSign, string.Empty); string comName; string propertyName; string typeName; if (!goName.Contains(splitGoNameAndProperty[0])) { return; } string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries); goName = tempstrs[0]; if (!tempstrs[1].Contains(splitComponentAndProperty[0])) { EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的"); return; } tempstrs = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries); comName = tempstrs[0]; propertyName = tempstrs[1]; if (!propertyName.Contains(splitPropertyAndType.ToString())) { typeName = "EventProperty<string>"; } tempstrs = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries); propertyName = tempstrs[0]; typeName = "EventProperty<" + tempstrs[1] + ">"; initMethod.Statements.AddRange(new CodeStatement[] { new CodeCommentStatement("From " + go.name), new CodeSnippetStatement("((" + viewClassName + ")view)." + comName + goName + ".Bind(((" + modelClassName + ")model)." + propertyName + ");"), }); }); gener.newClass.Members.Add(initMethod); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath)); }