/// <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); }
/// <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); }