UI路径配置SO>字典json工作流

制作编辑器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();
    }
}

读取使用时输入类型参数,得到类型名字,从字典中得到路径。

相关推荐
stevenzqzq2 小时前
mvi理解2-ui状态和业务状态
ui
淡海水5 小时前
02-06-原理篇-增量式GC与并发GC
算法·unity·c#·并发·gc·增量
阿松爱学习18 小时前
【Unity开发】Aspose.PDF的使用
unity·pdf·c#
刘科领18 小时前
使用ollama & openweb-ui 本地搭建自己的AI
人工智能·python·ui·ai
淡海水19 小时前
02-04-原理篇-GC Roots与对象图遍历
算法·unity·c#·图·gc·遍历·roots
xy34531 天前
Axure 9.0 动态面板基本操作
前端·ui·html·axure·原型·产品设计
霍格沃兹测试学院-小舟畅学1 天前
UI 智能遍历测试:状态识别、路径规划与覆盖率评估
microsoft·ui
Pocker_Spades_A1 天前
Penpot自托管设计协作:Docker部署、MCP接入与Codex生成UI实操记录
ui·docker·容器
兰亭妙微UI设计公司2 天前
兰亭妙微界面设计公司分享:如何高效改善设计质量|实战分析
ui