制作编辑器SO,每个UI的路径是一个字段,填UI路径,加上特性,把UI类型填入。导出时反射读取特性,拿到类型,作为键,构造字典,>json导出。
cs
[CreateAssetMenu(menuName = "MyFarm/UI配置表")]
public class UIConfigSO : ScriptableObject
{
[Header("入口")]
[MustHas(typeof(EntrancePanel))]
public string panelEntrance;
[MustHas(typeof(PanelLogin))]
public string panelLogin;
[MustHas(typeof(PanelRegister))]
public string panelRegister;
[MustHas(typeof(SelectServerPanel))]
public string panelSelectServer;
[MustHas(typeof(BGPanel))]
public string BGPanel;
[Header("通用")]
[MustHas(typeof(PanelPause))]
public string panelPause;
[MustHas(typeof(PanelSettings))]
public string panelSettings;
[MustHas(typeof(PanelLoading))]
public string panelLoading;
[Header("Gameplay")]
[MustHas(typeof(HUDPanel))]
public string panelHUD;
[MustHas(typeof(MissionPanel))]
public string panelMission;
[MustHas(typeof(DetailPanel))]
public string panelDetail;
[MustHas(typeof(OrderPanel))]
public string panelOrder;
[MustHas(typeof(PanelBuild))]
public string panelBuild;
[MustHas(typeof(PanelBuildDetail))]
public string panelBuildDetail;
[MustHas(typeof(PanelCook))]
public string panelCook;
[MustHas(typeof(PanelCookDetail))]
public string panelCookDetail;
[MustHas(typeof(PanelGameOver))]
public string panelGameOver;
Dictionary<string, string> dic = new();
public string jsonPath;
[ContextMenu("导出json")]
void ToJson()
{
FieldInfo[] fields = GetType().GetFields();
dic.Clear();
foreach (FieldInfo field in fields)
{
var att = field.GetCustomAttribute<MustHasAttribute>();
if (att != null)
{
dic.Add(att.TargetType.Name, field.GetValue(this).ToString());
}
}
string json=JsonConvert.SerializeObject(dic,Formatting.Indented);
string path = $"{Application.dataPath}/../{jsonPath}";
File.WriteAllText(path, json);
AssetDatabase.Refresh();
}
}
读取使用时输入类型参数,得到类型名字,从字典中得到路径。