/// <summary> /// 这里将类名注册为地址类中的字典键值对,将预制件地址存储为值 /// JSON将需要保存已经注册过的UI、音频文件 /// JSON保存地址为StreamingAssets configs.dat /// </summary> private void RigisterUIPath(string uiScriptsName) { string localPath = "Assets/Resources/" + GlobalEditorSetting.UI_PREFAB_PATH + "Canvas_" + uiScriptsName.Substring(2); //预防重名 localPath = AssetDatabase.GenerateUniqueAssetPath(localPath); localPath = localPath.Substring(17); localPath = localPath.Split('.')[0]; localPath = localPath + "|" + uiSummary; ResPathTemplate rpt = null; //如果文件存在,则读取解析为存储类,写入相关数据条后写入JSON并保存 if (DocumentAccessor.IsExists(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME)) { var content = DocumentAccessor.ReadFile(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); rpt = JsonMapper.ToObject <ResPathTemplate>(content); if (!rpt.UI.ContainsKey(uiScriptsName)) { rpt.UI.Add(uiScriptsName, localPath); } } //如果文件不存在,则新建存储类,并保存相关的数据,然后写入JSON并保存 else { rpt = new ResPathTemplate(); rpt.UI.Add(uiScriptsName, localPath); } //写入已经注册的UI using (StreamWriter sw = _saveLocalFileInfo.CreateText()) { var result = JsonMapper.ToJson(rpt); sw.Write(result); } //更新并保存CS ResPathParse rpp = new ResPathParse(); if (!FrameworkConfig.Instance.UseHotFixMode) { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } else { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts/ILRuntime/HotFixLogic", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } AssetDatabase.Refresh(); }
void OnGUI() { //必选项:类名 GUILayout.Label("脚本类名(UI+类名) 例如:UIMain"); uiScriptsName = EditorGUILayout.TextField(uiScriptsName); //选填 GUILayout.Label("类说明,建议写UI界面类型,例如:主界面"); uiSummary = EditorGUILayout.TextField(uiSummary); uiNodeType = ( UINodeTypeEnum )EditorGUILayout.EnumPopup("挂载节点", uiNodeType); uiShowMode = ( UIShowModeEnum )EditorGUILayout.EnumPopup("窗体显示方式", uiShowMode); uiTransparent = ( UITransparentEnum )EditorGUILayout.EnumPopup("窗体背后的遮罩透明度", uiTransparent); EditorGUILayout.Space(); //勾选项 useOnEnable_OnDisable = EditorGUILayout.Toggle("OnEnable/OnDisable", useOnEnable_OnDisable); useDefaultExitBtn = EditorGUILayout.Toggle("退出按钮", useDefaultExitBtn); if (useDefaultExitBtn) { useOnEnable_OnDisable = useDefaultExitBtn ? true : EditorGUILayout.Toggle("启用OnEnable/OnDisable", useOnEnable_OnDisable); } useAnimRoot = EditorGUILayout.Toggle("动画控制器", useAnimRoot); if (useAnimRoot) { animStartID = EditorGUILayout.TextField(" 弹出动画ID", animStartID); animCloseID = EditorGUILayout.TextField(" 关闭动画ID", animCloseID); } EditorGUILayout.Space(); using (new BackgroundColorScope(Color.green)) { if (GUILayout.Button("创建脚本+UI预制件+注册绑定", GUILayout.Height(40))) { _saveLocalFileInfo = new FileInfo(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); if (CheckClassNameValid()) { isDirty = true; EditorUtility.DisplayProgressBar("生成UI模块", "", 1f); //CS 脚本 UICreateParse cs = new UICreateParse(); string csOutPath = Application.dataPath + "/Scripts/UI"; if (!FrameworkConfig.Instance.UseHotFixMode) { csOutPath = Application.dataPath + "/Scripts/UI"; } else { csOutPath = Application.dataPath + "/Scripts/ILRuntime/HotFixLogic/UI"; } EditorMenuExtention.CreateCSFile(csOutPath, uiScriptsName + ".cs", cs.CreateCS(this)); AssetDatabase.Refresh(); //预制件 newCanvas = new GameObject("Canvas_" + uiScriptsName.Substring(2), typeof(Canvas)).GetComponent <Canvas>(); newCanvas.renderMode = RenderMode.ScreenSpaceOverlay; var canvasScaler = newCanvas.gameObject.AddComponent <CanvasScaler>(); canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; var graphics = newCanvas.gameObject.AddComponent <GraphicRaycaster>(); graphics.ignoreReversedGraphics = true; graphics.blockingObjects = GraphicRaycaster.BlockingObjects.None; GameObject animTrans = new GameObject("Container_Anim", typeof(RectTransform)); animTrans.transform.SetParent(newCanvas.transform); var recTrans = animTrans.GetComponent <RectTransform>(); recTrans.sizeDelta = Vector2.zero; recTrans.anchorMin = Vector2.zero; recTrans.anchorMax = Vector2.one; recTrans.anchoredPosition = Vector2.zero; animTrans.transform.localPosition = Vector3.zero; animTrans.transform.localScale = Vector3.one; if (useAnimRoot) { //DOTEEN插件未集成在编辑器库中,引出到库外部使用 CreateAnimationComponentEvent?.Invoke(animTrans, animStartID, animCloseID); } if (useDefaultExitBtn) { GameObject btnExit = new GameObject("Btn_Exit", typeof(RectTransform)); btnExit.AddComponent <CanvasRenderer>(); btnExit.AddComponent <Image>().maskable = false; btnExit.AddComponent <Button>(); btnExit.transform.SetParent(animTrans.transform); btnExit.transform.localPosition = Vector3.zero; btnExit.transform.localScale = Vector3.one; } //Layer设定 ChangeUILayer(newCanvas.transform, "UI"); //预制件自注册 RigisterUIPath(uiScriptsName); AssetDatabase.Refresh(); } else { EditorUtility.DisplayDialog("类名错误", "类名应该不为空、空格,并且以UI开头", "哦"); } } } EditorGUILayout.Space(); using (new BackgroundColorScope(Color.green)) { if (GUILayout.Button("仅创建脚本", GUILayout.Height(40))) { _saveLocalFileInfo = new FileInfo(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); if (CheckClassNameValid()) { //CS 脚本 UICreateParse cs = new UICreateParse(); string csOutPath = Application.dataPath + "/Scripts/UI"; if (!FrameworkConfig.Instance.UseHotFixMode) { csOutPath = Application.dataPath + "/Scripts/UI"; } else { csOutPath = Application.dataPath + "/Scripts/ILRuntime/HotFixLogic/UI"; } EditorMenuExtention.CreateCSFile(csOutPath, uiScriptsName + ".cs", cs.CreateCS(this)); AssetDatabase.Refresh(); } else { EditorUtility.DisplayDialog("类名错误", "类名应该不为空、空格,并且以UI开头", "哦"); } } } EditorGUILayout.Space(); using (new BackgroundColorScope(Color.yellow)) { if (GUILayout.Button("更新UI配置", GUILayout.Height(40))) { _saveLocalFileInfo = new FileInfo(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); //============JSON文件取出============// ResPathTemplate rpt = null; //如果文件存在,则读取解析为存储类,写入相关数据条后写入JSON并保存 if (DocumentAccessor.IsExists(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME)) { var content = DocumentAccessor.ReadFile(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); rpt = JsonMapper.ToObject <ResPathTemplate>(content); } //如果文件不存在,则新建存储类,并保存相关的数据,然后写入JSON并保存 else { rpt = new ResPathTemplate(); } //=================================// //每次都重新写入ResPath Dictionary <string, string> resPathSumList = new Dictionary <string, string>(); foreach (var item in rpt.UI) { var sum = item.Value.Split('|'); if (sum.Length > 1 && !string.IsNullOrEmpty(sum[1])) { resPathSumList.Add(item.Key.ToUpper(), sum[1]); } } rpt.UI.Clear(); //============存入UI配置============// List <string> allResourcesPath = new List <string>(); RecursionAction("Assets", allResourcesPath); foreach (var childPath in allResourcesPath) { DirectoryInfo folder = new DirectoryInfo("Assets" + childPath + "/" + GlobalEditorSetting.UI_PREFAB_PATH); if (!folder.Exists) { continue; } foreach (FileInfo file in folder.GetFiles()) { string ss = file.Extension.ToUpper(); if (ss.Contains(".PREFAB") && file.FullName.Contains("Canvas")) { var result = file.Name.Split('.')[0]; var key = "UI" + result.Split('_')[1].ToUpper(); rpt.UI.Add(key, GlobalEditorSetting.UI_PREFAB_PATH + result); if (resPathSumList.ContainsKey(key)) { rpt.UI[key] += "|" + resPathSumList[key]; } } } } //=================================// //============JSON文件存入============// using (StreamWriter sw = _saveLocalFileInfo.CreateText()) { var result = JsonMapper.ToJson(rpt); sw.Write(result); } //=================================// //============更新并保存CS============// //更新并保存CS ResPathParse rpp = new ResPathParse(); if (!FrameworkConfig.Instance.UseHotFixMode) { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } else { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts/ILRuntime/HotFixLogic", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } AssetDatabase.Refresh(); } } //汇总编译 while (isDirty && !EditorApplication.isCompiling) { isDirty = false; EditorUtility.ClearProgressBar(); LDebug.Log(" 成功生成UI预制件! "); if (!FrameworkConfig.Instance.UseHotFixMode) { //反射生成脚本组件 var asmb = System.Reflection.Assembly.Load("Assembly-CSharp"); var t = asmb.GetType("Assets.Scripts.UI." + uiScriptsName); if (null != t) { newCanvas.gameObject.AddComponent(t); } else { LDebug.LogError("UI脚本绑定失败"); } } string localPath = "Assets/Resources/" + GlobalEditorSetting.UI_PREFAB_PATH + newCanvas.gameObject.name + ".prefab"; //预防重名 localPath = AssetDatabase.GenerateUniqueAssetPath(localPath); PrefabUtility.SaveAsPrefabAssetAndConnect(newCanvas.gameObject, localPath, InteractionMode.UserAction); AssetDatabase.Refresh(); } }
public static void RegisterAudios() { FileInfo _saveLocalFileInfo = new FileInfo(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); //============JSON文件取出============// ResPathTemplate rpt = null; //如果文件存在,则读取解析为存储类,写入相关数据条后写入JSON并保存 if (DocumentAccessor.IsExists(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME)) { var content = DocumentAccessor.ReadFile(Application.dataPath + "/Editor/" + GlobalEditorSetting.JSON_FILE_NAME); rpt = JsonMapper.ToObject <ResPathTemplate>(content); } //如果文件不存在,则新建存储类,并保存相关的数据,然后写入JSON并保存 else { rpt = new ResPathTemplate(); } //=================================// //每次都重新写入 rpt.Sound.Clear(); //============存入音频配置============// List <string> allResourcesPath = new List <string>(); RecursionAction("Assets", allResourcesPath); foreach (var childPath in allResourcesPath) { DirectoryInfo folder = new DirectoryInfo("Assets" + childPath + "/" + GlobalEditorSetting.AUDIO_PATH); if (!folder.Exists) { continue; } foreach (FileInfo file in folder.GetFiles()) { string ss = file.Extension.ToUpper(); if (ss.Contains(".AIFF") || ss.Contains(".WAV") || ss.Contains(".MP3") || ss.Contains(".OGG")) { var result = file.FullName.Substring(file.FullName.LastIndexOf('\\') + 1); result = result.Split('.')[0]; rpt.Sound.Add(result.ToUpper(), GlobalEditorSetting.AUDIO_PATH + "/" + result); } } } //=================================// //============JSON文件存入============// using (StreamWriter sw = _saveLocalFileInfo.CreateText()) { var result = JsonMapper.ToJson(rpt); sw.Write(result); } //=================================// //============更新并保存CS============// ResPathParse rpp = new ResPathParse(); if (!FrameworkConfig.Instance.UseHotFixMode) { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } else { EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts/ILRuntime/HotFixLogic", GlobalEditorSetting.OUTPUT_RESPATH, rpp.CreateCS(rpt)); } AssetDatabase.Refresh(); }