public void CreateScript(UIScriptCreateConfig tConfig) { if (tConfig == null) { return; } //类名 string tContent = File.ReadAllText(GetTemplatePath(tConfig.mScriptType)); string tReplaceName = tConfig.mScriptName + "Template"; string tClassName = GetClassNameByFilePath(tConfig.mPath); //特定的代码部分替换 Func <string, string> tRepaceFunc = null; switch (tConfig.mScriptType) { case UIScriptCreateConfig.ScriptType.View: tRepaceFunc = ReplaceViewTag; break; } tContent = tContent.Replace(tReplaceName, tClassName); tContent = tRepaceFunc == null ? tContent : tRepaceFunc(tContent); //生成脚本文件 string tDirectoryPath = tConfig.mPath.Substring(0, tConfig.mPath.LastIndexOf('\\')); if (Directory.Exists(tDirectoryPath) == false) { Directory.CreateDirectory(tDirectoryPath); } File.WriteAllText(tConfig.mPath, tContent); }
private void DrawTopInfo() { string tTriangleStr = mShowCreateToggle ? "▼" : "▶"; if (GUILayout.Button(string.Format("{0}脚本生成", tTriangleStr), "dragtabdropwindow")) { mShowCreateToggle = !mShowCreateToggle; } GUILayout.Space(3f); if (mShowCreateToggle == false) { GUILayout.Space(8f); return; } //绘制创建的代码 for (int i = 0; i < mManager.mScriptCreateConfigList.Count; ++i) { UIScriptCreateConfig tConfig = mManager.mScriptCreateConfigList[i]; GUILayout.BeginHorizontal("OL box NoExpand"); { tConfig.mCreate = EditorGUILayout.ToggleLeft(tConfig.mScriptName + " 脚本", tConfig.mCreate, GUILayout.Width(120f)); tConfig.mCover = EditorGUILayout.ToggleLeft("可覆盖", tConfig.mCover, GUILayout.Width(70f)); GUILayout.Label("路径:", GUILayout.Width(32)); Color tOriginColor = GUI.color; Color tColor = tConfig.IsNotAllowCover() ? Color.red : tOriginColor; GUI.color = tColor; tConfig.mPath = EditorGUILayout.TextField(tConfig.mPath); GUI.color = tOriginColor; if (GUILayout.Button("默认路径", GUILayout.Width(60f))) { tConfig.ResetPath(); } if (GUILayout.Button("创建", GUILayout.Width(40f))) { if (tConfig.IsNotAllowCover()) { ShowNotification(new GUIContent("操作失败:文件已存在且不允许覆盖,重新设置操作")); } else { mManager.CreateScript(tConfig); ShowNotification(new GUIContent("文件创建完成")); } } } GUILayout.EndHorizontal(); } if (GUILayout.Button("一 键 创 建", "LargeButtonMid")) { if (mManager.HasScripNotAllowToCoverInCreate()) { ShowNotification(new GUIContent("操作失败:有文件已存在且不允许覆盖,重新设置操作")); } else { mManager.CreateAllScript(); ShowNotification(new GUIContent("文件创建完成")); } } GUILayout.Space(5f); }