public static bool CreateMonoLuaScript(string monoName, string desc) { string targetPath = Application.dataPath + "/" + CustomLuaMonoEditor.MonoScriptsPath; if (!string.IsNullOrEmpty(desc) && !desc.Equals("脚本描述")) { string monoPath = targetPath + monoName + "Mono.lua"; Dictionary <string, ScriptElementInfo> dictElements = new Dictionary <string, ScriptElementInfo>(); addFunToElement(dictElements, "Awake"); addFunToElement(dictElements, "Start"); addFunToElement(dictElements, "OnDestroy"); addFunToElement(dictElements, "OnEnable"); addFunToElement(dictElements, "OnDisable"); //addFunToElement(dictElements, "Update"); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } CustomLuaMenu.CreateMonoScriptFile(monoPath, desc, SystemInfo.deviceName, monoName, dictElements); string controllerPath = targetPath + monoName + "Controller.lua"; CustomLuaMenu.CreateControllerScriptFile(controllerPath, desc, SystemInfo.deviceName, monoName); return(true); } else { EditorUtility.DisplayDialog("创建失败", "请填写正确的脚本描述", "确认"); return(false); } }
//绘制窗口内容 void OnGUI() { EditorGUILayout.HelpBox(_strHelpMsg, _msgType, true); EditorGUILayout.Separator(); EditorGUILayout.LabelField("目录:" + _strDirectory); /* * EditorGUILayout.BeginHorizontal(); * EditorGUILayout.LabelField("*文件名"); * _strFileName = EditorGUILayout.TextField(_strFileName, GUILayout.Width(300), GUILayout.ExpandWidth(true)); * EditorGUILayout.EndHorizontal(); */ EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*脚本描述"); _strSummary = EditorGUILayout.TextField(_strSummary, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*作者"); _strAuthor = EditorGUILayout.TextField(_strAuthor, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*模块名"); _strModule = EditorGUILayout.TextField(_strModule, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("函数信息 "); _display = (ELuaMonoFunc)EditorGUILayout.EnumPopup(_display, GUILayout.Width(120), GUILayout.ExpandWidth(true)); if (GUILayout.Button("添加", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { string funcName = _display.ToString(); AddFuncToScript(funcName); } EditorGUILayout.EndHorizontal(); //菜单 EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("名称", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("属性", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("类型", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("模块", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); //函数列表信息 _v2ScrollPos = EditorGUILayout.BeginScrollView(_v2ScrollPos); foreach (var kvp in _dictElements) { var elementInfo = kvp.Value; string strAttr = elementInfo._bGlobal ? "global" : "local"; string strName = elementInfo._name; string strTypeName = elementInfo._type.ToString(); string strModule = _strModule; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(strName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strAttr, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strTypeName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strModule, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); //操作按钮 EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("生成", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { if (string.IsNullOrEmpty(_strFileName) || string.IsNullOrEmpty(_strSummary) || string.IsNullOrEmpty(_strAuthor) || string.IsNullOrEmpty(_strModule)) { _strHelpMsg = "信息不完整"; _msgType = MessageType.Error; return; } //还原提示信息 _strHelpMsg = "带有*标识的为必填项"; _msgType = MessageType.Info; //生成脚本 string file = _strDirectory + "/" + _strModule + "Mono.lua"; System.IO.FileInfo fi = new System.IO.FileInfo(file); if (fi.Exists) { EditorUtility.DisplayDialog("创建失败", "存在相同命名的脚本,请删除或者重命名后再创建。", "确认"); return; } if (!Directory.Exists(_strDirectory)) { Directory.CreateDirectory(_strDirectory); } CustomLuaMenu.CreateMonoScriptFile(file, _strSummary, _strAuthor, _strModule, _dictElements); if (_parentBehaviour != null) { _parentBehaviour.bindScript = CustomLuaMonoEditor.MonoScriptsPath + _strModule + "Mono.lua"; } string controllerPath = _strDirectory + "/" + _strModule + "Controller.lua"; CustomLuaMenu.CreateControllerScriptFile(controllerPath, _strSummary, _strAuthor, _strModule); this.Close(); } if (GUILayout.Button("取消", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { this.Close(); } EditorGUILayout.EndHorizontal(); }
//绘制窗口内容 void OnGUI() { EditorGUILayout.HelpBox(_strHelpMsg, _msgType, true); EditorGUILayout.Separator(); EditorGUILayout.LabelField("目录:" + _strDirectory); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*文件名"); _strFileName = EditorGUILayout.TextField(_strFileName, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*脚本描述"); _strSummary = EditorGUILayout.TextField(_strSummary, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*作者"); _strAuthor = EditorGUILayout.TextField(_strAuthor, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*模块名"); _strModule = EditorGUILayout.TextField(_strModule, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("函数信息 "); _display = (ELuaMonoFunc)EditorGUILayout.EnumPopup(_display, GUILayout.Width(200), GUILayout.ExpandWidth(true)); if (GUILayout.Button("添加", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { string funcName = _display.ToString(); AddFuncToScript(funcName); } EditorGUILayout.EndHorizontal(); //菜单 EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("名称", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("属性", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("类型", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("模块", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); //函数列表信息 _v2ScrollPos = EditorGUILayout.BeginScrollView(_v2ScrollPos); foreach (var kvp in _dictElements) { var elementInfo = kvp.Value; string strAttr = elementInfo._bGlobal ? "global" : "local"; string strName = elementInfo._name; string strTypeName = elementInfo._type.ToString(); string strModule = _strModule; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(strName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strAttr, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strTypeName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strModule, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); //操作按钮 EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("生成", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { if (string.IsNullOrEmpty(_strFileName) || string.IsNullOrEmpty(_strSummary) || string.IsNullOrEmpty(_strAuthor) || string.IsNullOrEmpty(_strModule)) { _strHelpMsg = "信息不完整"; _msgType = MessageType.Error; return; } //还原提示信息 _strHelpMsg = "带有*标识的为必填项"; _msgType = MessageType.Info; //生成脚本 string file = _strDirectory + "/" + _strFileName + ".lua"; if (!Directory.Exists(_strDirectory)) { Directory.CreateDirectory(_strDirectory); } CustomLuaMenu.generateScriptFile(file, _strSummary, _strAuthor, _strModule, _dictElements); } if (GUILayout.Button("取消", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { this.Close(); } EditorGUILayout.EndHorizontal(); }
//绘制窗口内容 void OnGUI() { EditorGUILayout.HelpBox(_strHelpMsg, _msgType, true); EditorGUILayout.Separator(); EditorGUILayout.LabelField("目录:" + _strDirectory); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*文件名"); _strFileName = EditorGUILayout.TextField(_strFileName, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*脚本描述"); _strSummary = EditorGUILayout.TextField(_strSummary, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("*作者"); _strAuthor = EditorGUILayout.TextField(_strAuthor, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("模块名"); _strModule = EditorGUILayout.TextField(_strModule, GUILayout.Width(300), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("脚本类 "); if (GUILayout.Button("添加", GUILayout.Width(100))) { var classEdtorWin = (ScriptClassEditorInfoWindow)EditorWindow.GetWindow(typeof(ScriptClassEditorInfoWindow)); classEdtorWin.name = "类信息"; classEdtorWin.initialize(_strModule, _dictElements, this); } if (GUILayout.Button("删除", GUILayout.Width(100))) { var keys = new List <string>(_elementsToggleInfo.Keys); foreach (var k in keys) { if (!_elementsToggleInfo[k]) { _dictElements.Remove(k); _elementsToggleInfo.Remove(k); } } } EditorGUILayout.EndHorizontal(); ////////生成类列表信息 //菜单 EditorGUILayout.BeginHorizontal(); _bSelectAll = EditorGUILayout.Toggle(_bSelectAll, GUILayout.Width(30)); //选中 EditorGUILayout.LabelField("名称", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("属性", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("类型", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField("模块", GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); //信息 _v2ScrollPos = EditorGUILayout.BeginScrollView(_v2ScrollPos); foreach (var kvp in _dictElements) { var elementInfo = kvp.Value; string strAttr = elementInfo._bGlobal ? "global" : "local"; string strName = elementInfo._name; string strTypeName = elementInfo._type.ToString(); string strModule = string.IsNullOrEmpty(elementInfo._module) ? "NULL" : elementInfo._module; //保存toggle信息 bool bSaved = false; if (_elementsToggleInfo.ContainsKey(strName)) { bSaved = _elementsToggleInfo[strName]; } else { _elementsToggleInfo.Add(strName, bSaved); } //是否全选 bool bToggle = _bSelectAll ? _bSelectAll : bSaved; EditorGUILayout.BeginHorizontal(); bToggle = EditorGUILayout.Toggle(bToggle, GUILayout.Width(30)); //选中 EditorGUILayout.LabelField(strName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strAttr, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strTypeName, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.LabelField(strModule, GUILayout.Width(30), GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); if (_bSelectAll && !bToggle) { _bSelectAll = false; } //全选状态选中一个,取消全选 _elementsToggleInfo[strName] = bToggle; } EditorGUILayout.EndScrollView(); //操作按钮 EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("生成", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { if (string.IsNullOrEmpty(_strFileName) || string.IsNullOrEmpty(_strSummary) || string.IsNullOrEmpty(_strAuthor)) { _strHelpMsg = "信息不完整"; _msgType = MessageType.Error; return; } //还原提示信息 _strHelpMsg = "带有*标识的为必填项"; _msgType = MessageType.Info; if (_dictElements.Count == 0) { ScriptElementInfo dft = new ScriptElementInfo(); dft._name = "DefaultTable"; dft._description = "默认生成的类"; dft._type = EScriptElementType.Table; dft._bGlobal = false; _dictElements.Add(dft._name, dft); } //生成脚本 string file = _strDirectory + "/" + _strFileName + ".lua"; CustomLuaMenu.generateScriptFile(file, _strSummary, _strAuthor, _strModule, _dictElements); } if (GUILayout.Button("取消", GUILayout.Width(100), GUILayout.ExpandWidth(true))) { this.Close(); } EditorGUILayout.EndHorizontal(); }
public override void OnInspectorGUI() { GUILayout.BeginHorizontal(); GUILayout.Label("LuaScript"); if (string.IsNullOrEmpty(info.bindScript)) { obj = EditorGUILayout.ObjectField(obj, typeof(Object), false, GUILayout.Width(170), GUILayout.ExpandWidth(true)); if (obj != null) { string assetPath = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(assetPath) && assetPath.Contains("/luaScripts/") && checkAsset(assetPath)) { int begin = assetPath.IndexOf("luaScripts"); int end = assetPath.LastIndexOf("/"); info.bindScript = assetPath.Substring(begin, end - begin + 1) + obj.name + ".lua"; } else { Debug.LogError("请选择正确的Lua脚本文件(*.txt、*.lua) GameObject: " + info.gameObject.name); obj = null; } } } else { string objPath = scriptFolderPath + info.bindScript; obj = AssetDatabase.LoadAssetAtPath(objPath + ".lua", typeof(Object)); if (obj == null) { obj = AssetDatabase.LoadAssetAtPath(objPath + ".txt", typeof(Object)); } EditorGUILayout.ObjectField(obj, typeof(Object), false, GUILayout.Width(170), GUILayout.ExpandWidth(true)); } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("BindedScript", string.IsNullOrEmpty(info.bindScript) ? "None" : info.bindScript); if (string.IsNullOrEmpty(info.bindScript) && checkLuaExsit(info.gameObject.name)) { if (GUILayout.Button("Reload")) { info.bindScript = MonoScriptsPath + info.gameObject.name + "Mono.lua"; } } GUILayout.EndHorizontal(); //按钮 if (string.IsNullOrEmpty(info.bindScript)) { GUILayout.BeginHorizontal(); if (GUILayout.Button("Create")) { if (checkLuaExsit(info.gameObject.name)) { EditorUtility.DisplayDialog("创建失败", "存在相同命名的脚本,请删除了再试。", "确认"); } else { try { CustomLuaMenu.CreateMonoLuaScript(info.gameObject.name, _cScriptDesc); if (checkLuaExsit(info.gameObject.name)) { info.bindScript = MonoScriptsPath + info.gameObject.name + "Mono.lua"; } } catch (System.Exception e) { Debug.LogError(e.Message); } } } /* 界面名有bug * if (GUILayout.Button("Edit")) * { * try * { * CustomLuaMenu.OpenMonoLuaScriptEditor(info); * } * catch (System.Exception e) * { * Debug.LogError(e.Message); * } * } * */ GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("-> " + info.gameObject.name, GUILayout.Width(150)); _cScriptDesc = EditorGUILayout.TextField(string.IsNullOrEmpty(_cScriptDesc) ? "脚本描述" : _cScriptDesc, GUILayout.Width(50), GUILayout.ExpandWidth(true)); GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); if (GUILayout.Button("Reset")) { try { info.bindScript = ""; obj = null; } catch (System.Exception e) { info.bindScript = ""; Debug.Log(e.Message); } } if (GUILayout.Button("Reload")) { try { if (EditorApplication.isPlaying && info != null) { info.loadLuaScript(); } } catch (System.Exception e) { Debug.Log(e.Message); } } GUILayout.EndHorizontal(); } SceneView.RepaintAll(); }