创建自定义配置的解析器和创建器的过程(Localization的配置器和解析器创建过程也是类似):
- 创建一个
ConfigObject
,从ConfigObject
派生 - 创建解析器和创建器,创建器实现
IConfigCreate
接口,解析器实现IConfigParse
接口
内置的CSV大体代码,具体代码看Cabin Icarus\UnityEditorFrame\Editor\Scripts\Config\Csv
public partial class CsvConfigObject:ConfigObject
{
//CsvConfigObject的绘制
protected override void DrawConfigObject(LocalizationManager localizationManager, int currentIndex)
{
var config = _configs[currentIndex];
EditorGUILayout.LabelField(currentIndex.ToString(), GUILayout.Width(50));
EditorGUI.BeginChangeCheck();
{
GUI.SetNextControlName(KeyFocusName);
newValue = EditorGUILayout.DelayedTextField(config.Key);
}
if (EditorGUI.EndChangeCheck())
{
if (!EditorGUIUtility.editingTextField && GUI.GetNameOfFocusedControl() != KeyFocusName)
{
return;
}
ModifyKey(config.Key, newValue);
}
EditorGUILayout.Space();
EditorGUI.BeginChangeCheck();
{
GUI.SetNextControlName(ValueFocusName);
newValue = EditorGUILayout.DelayedTextField(config.Value);
}
if (EditorGUI.EndChangeCheck())
{
if (!EditorGUIUtility.editingTextField && GUI.GetNameOfFocusedControl() != ValueFocusName)
{
return;
}
SetValue(config.Key, newValue);
}
EditorGUIUtility.SetIconSize(new Vector2(12,12));
{
if (GUILayout.Button(new GUIContent(EditorGUIUtility.FindTexture("d_P4_DeletedLocal")),
GUILayout.Width(18), GUILayout.Height(18)))
{
Remove(config.Key);
}
}
EditorGUIUtility.SetIconSize(Vector2.zero);
}
}
public class CsvConfigCreate:IConfigCreate
{
public string Extension { get; } = "txt";
public void Create(string content, string savePath)
{
File.WriteAllText(savePath,content);
}
}
public class CsvConfigParse:IConfigParse<CsvConfigObject>
{
public CsvConfigObject Parse(string content)
{
if (string.IsNullOrWhiteSpace(content))
{
return new CsvConfigObject(_separator);
}
var lines = content.SplitToLines();
Dictionary<string, string> config = new Dictionary<string, string>();
string[] strs;
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line) || line[0] == '#')
{
continue;
}
strs = line.Split(_separator);
if (strs.Length < 2)
{
EditorFrameLog.Error($"continue {line} Read.");
continue;
}
config[strs[0]] = strs[1];
}
return new CsvConfigObject(config, _separator);
}
}