/// <summary> /// 建立 Scriptable Asset /// </summary> /// <returns>是否成功建立</returns> public bool CreateScriptableAssets(string scriptableScriptName, string scriptableAssetName) { _config = ClientDataBaseManager.Instance.m_config; MonoScript script = AssetDatabase.LoadAssetAtPath <MonoScript>(_config.GetScriptableScriptsPath() + scriptableScriptName); if (script == null || script.GetClass() == null) { Debug.LogError(string.Format("Scriptable Script is Null. [Path:{0}]", _config.GetScriptableScriptsPath() + scriptableScriptName)); return(false); } string path = _config.GetScriptableAssetPath() + scriptableAssetName; UtilityEditor.CreateFolder(_config.GetScriptableAssetPath()); Object _Object = ScriptableObject.CreateInstance(script.GetClass()); AssetDatabase.CreateAsset(_Object, path); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); Debug.Log(string.Format("[Scriptable Asset] is Create.\nFile:[{0}] Path:[{1}]", scriptableAssetName, _config.GetScriptableAssetPath())); //資料讀取 ScriptableObjectBase scriptableObjectBase = AssetDatabase.LoadAssetAtPath <ScriptableObjectBase>(path); return(scriptableObjectBase.LoadGameTable(true)); }
/// <summary> /// 建立 Scriptable Script Editor /// </summary> /// <returns>是否成功建立</returns> bool CreateScriptableScriptEditor() { string templateScriptable = GetTemplate("ScriptableEditor"); if (string.IsNullOrEmpty(templateScriptable)) { return(false); } templateScriptable = templateScriptable.Replace("$ScriptableEditorName", _config.GetScriptableScriptEditorName(_tableName)); templateScriptable = templateScriptable.Replace("$ScriptableName", _config.GetScriptableScriptName(_tableName)); UtilityEditor.CreateFolder(_config.GetScriptableEditorPath()); using (var writer = new StreamWriter(_config.GetScriptableEditorPath() + _config.GetScriptableScriptEditorName(_tableName, true))) { writer.Write(templateScriptable); writer.Close(); } AssetDatabase.Refresh(); Debug.Log(string.Format("[Scriptable Script Editor] is Create.\nFile:[{0}] Path:[{1}]", _config.GetScriptableScriptEditorName(_tableName, true), _config.GetScriptableEditorPath())); return(true); }
public static void UpdateAll() { ClientDataBaseEditorWindow window = EditorWindow.GetWindow <ClientDataBaseEditorWindow>(); window._objList = UtilityEditor.LoadAllAssetsAtPath(ClientDataBaseManager.Instance.m_config.GetGameTablePath()).ToList(); if (window._objList.Count == 0) { Debug.Log("No GameTable file (.csv)"); window.Close(); return; } window._boolUpdateAll = true; window._boolStartCreate = true; window._boolExecuteButtonClick = true; }
void OnGUI() { _btnStyle = new GUIStyle(GUI.skin.button); _btnStyle.fontSize = 20; _btnStyle.alignment = TextAnchor.MiddleLeft; GUIStyle myStyle = GUI.skin.GetStyle("HelpBox"); myStyle.fontSize = 16; //遮罩 if (_boolStartCreate) { GUI.enabled = false; } _intTabIndex = UtilityEditor.Tabs(_types, _intTabIndex); GUILayout.Space(10); //還沒開始時才需要抓物件 if (_boolStartCreate == false && _boolUpdateAll == false) { _objList = Selection.objects.ToList(); } //排除 for (int i = _objList.Count - 1; i >= 0; i--) { if (GetFiltered(_objList[i])) { _objList.Remove(_objList[i]); } } if (GUILayout.Button("清除生成目录的只读属性")) { FileUtils.ClearReadOnly(ClientDataBaseManager.Instance.m_config.m_root + "/Generate"); FileUtils.ClearReadOnly(ClientDataBaseManager.Instance.m_config.GetScriptableAssetPath()); FileUtils.ClearReadOnly(Application.dataPath + "/../../Server/apps/config"); FileUtils.ClearReadOnly(Application.dataPath + "/LuaFramework/Lua/Table"); } createScriptableAssets = EditorGUILayout.ToggleLeft("是否生成Script Assets文件(发布版本时,可以不用生成,版本机会出)", createScriptableAssets); exportToCSharp = EditorGUILayout.ToggleLeft("是否生成C#读取程序", exportToCSharp); exportToErlang = EditorGUILayout.ToggleLeft("是否导出服务器表格", exportToErlang); exportToLua = EditorGUILayout.ToggleLeft("是否导出lua表格", exportToLua); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Choose GameTable Asset", EditorStyles.boldLabel, GUILayout.Width(200)); EditorGUILayout.LabelField("Count : " + _objList.Count, EditorStyles.boldLabel, GUILayout.Width(100)); string btnTip = _intTabIndex == 0 ? "创建所有": "更新所有表格到ScriptableObject asset"; if (GUILayout.Button(btnTip)) { UpdateAll(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); if (_objList.Count == 0) { EditorGUILayout.HelpBox(GetHelpString(), MessageType.Warning); return; } //字母排序 _objList.Sort(delegate(Object a, Object b) { return(a.name.CompareTo(b.name)); }); EditorGUILayout.BeginVertical(); _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, false, false); EditorGUILayout.Space(); foreach (Object go in _objList) { if (GUILayout.Button(go.name, _btnStyle)) { EditorGUIUtility.PingObject(go); } } EditorGUILayout.EndScrollView(); EditorGUILayout.EndVertical(); if (UtilityEditor.GetCommonButton(_types[_intTabIndex])) { if (EditorApplication.isCompiling) { Debug.LogError("After wait application compiling then try again."); return; } _boolStartCreate = true; _boolExecuteButtonClick = true; } if (_boolStartCreate) { GUI.enabled = true; UtilityEditor.ShowLoading(); } }
/// <summary> /// 建立 Scriptable Script /// </summary> /// <returns>是否成功建立</returns> bool CreateScriptableScript(string[] variable, Dictionary <string, TableData> dataMap, int keyCol) { //TODO @jiake 是否考虑做个映射表? string templateName = _tableName; string tableClassName = _config.GetScriptableScriptName(_tableName); string dataClassName = _config.GetTableClassScriptName(_tableName); string tableBaseClassName = string.Empty; bool existKey = (keyCol >= 0); if (!TryParseSequenceTable(_tableName, ref templateName, ref tableBaseClassName, ref dataClassName)) { if (Names.Contains(_tableName)) { templateName = "StringScriptable"; } else if (!ExistTemplate(templateName)) { templateName = (existKey ? "ScriptableWithKey" : "Scriptable"); } } string template = GetTemplate(templateName); if (string.IsNullOrEmpty(template)) { return(false); } template = template.Replace("$ScriptableName", tableClassName); template = template.Replace("$ScriptableBaseName", tableBaseClassName); template = template.Replace("$GameTableName", _tableName); template = template.Replace("$ClassName", dataClassName); template = template.Replace( "$GameTablePath", "Config.GameTablePath + GameTableName + Config.FILE_EXTENSION_TXT"); if (existKey) { string fieldName = variable[keyCol]; TableData tableData = dataMap[fieldName]; template = template.Replace("$KeyTypeName", tableData.type); template = template.Replace("$KeyFieldName", variable[keyCol]); } Dictionary <string, string> variableMap = new Dictionary <string, string>(); for (int i = 0; i < variable.Length; i++) { string fieldName = variable[i]; // 不处理忽略字段 if (!dataMap.ContainsKey(fieldName)) { continue; } TableData tableData = dataMap[fieldName]; if (tableData.type == IgnoreType) { continue; } var resultStr = GetDataClassDetial(i, fieldName, tableData); variableMap.Add(fieldName, resultStr); } StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> item in variableMap) { sb.Append(item.Value); } template = template.Replace("$DataLoad", sb.ToString()); // 字段验证 StringBuilder checkFieldSb = new StringBuilder(); int checkFieldIndex = 1; foreach (KeyValuePair <string, string> item in variableMap) { checkFieldSb.Append(string.Format("\"{0}\"", item.Key)); if (checkFieldIndex != variableMap.Count) { checkFieldSb.Append(","); } checkFieldIndex++; } template = template.Replace("$FieldCheck", checkFieldSb.ToString()); UtilityEditor.CreateFolder(_config.GetScriptableScriptsPath()); using (var writer = new StreamWriter(_config.GetScriptableScriptsPath() + _config.GetScriptableScriptName(_tableName, true))) { writer.Write(template); writer.Close(); } AssetDatabase.Refresh(); Debug.Log(string.Format("[Scriptable Script] is Create.\nFile:[{0}] Path:[{1}]", _config.GetScriptableScriptName(_tableName, true), _config.GetScriptableScriptsPath())); return(true); }
/// <summary> /// 建立 Table Class /// </summary> /// <returns>返回该表的拥有的类型</returns> Dictionary <string, TableData> CreateTableScript(string[] summary, string[] variable, string[] type) { string templateDataClass = GetTemplate("TableClass"); if (string.IsNullOrEmpty(templateDataClass)) { return(null); } Dictionary <string, TableData> dataMap = new Dictionary <string, TableData>(); templateDataClass = templateDataClass.Replace("$ClassName", _config.GetTableClassScriptName(_tableName)); //// 第一行内容 //string[] firstConents = lineStr.Split(","[0]); StringBuilder field = new StringBuilder(); for (int i = 0; i < variable.Length; i++) { if (type[i] == IgnoreType) { Debug.Log("Ignore variable name:" + variable[i]); continue; } //透過字元 '[' ']' 判斷是否是Array bool isArray = type[i].EndsWith("[]"); // 字段名 string fieldName = variable[i]; if (dataMap.ContainsKey(fieldName)) { Debug.LogError(string.Format("Duplicate variable name and is not Array, table:[{0}], variable name:[{1}].", _tableName, fieldName)); return(null); } else { dataMap.Add(fieldName, new TableData(summary[i], fieldName, type[i], isArray)); } } foreach (KeyValuePair <string, TableData> item in dataMap) { field.Append(GetProperty(item.Value.summary, item.Value.name, item.Value.type, item.Value.IsArray(), item.Value.isEnd)); } templateDataClass = templateDataClass.Replace("$MemberFields", field.ToString()); // 如果是字符串表的话,只单单生成String表,别的都重复的 if (!IsSequenceTable(_tableName)) { UtilityEditor.CreateFolder(_config.GetTableClassPath()); using (var writer = new StreamWriter(_config.GetTableClassPath() + _config.GetTableClassScriptName(_tableName, true))) { writer.Write(templateDataClass); writer.Close(); } AssetDatabase.Refresh(); Debug.Log(string.Format("{0} created, path : {1}", _config.GetTableClassScriptName(_tableName, true), _config.GetTableClassPath())); } return(dataMap); }