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

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

相关推荐
Aman19846 小时前
Claude Code做Unity项目移植(二): 自动执行只读操作,告别频繁的
unity·游戏开发·vibe coding·claude code
小雨爱测9 小时前
Photoshop 实战入门:从界面认知到第一张成品图
ui·photoshop
螺丝钉tina13 小时前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
暴走约伯15 小时前
【VFX工具链源码拆解 02】告别机械劳动(上):一键统改百个子粒子颜色与批量重命名
3d·unity·c#·材质
郝学胜-神的一滴15 小时前
游戏设计:原型与开发 18:Unity入门的第一课
人工智能·程序人生·游戏·ui·unity·游戏引擎
淡海水17 小时前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
tedcloud12320 小时前
Docker Compose 部署 Uptime Kuma:配置 HTTPS、QQ 邮箱告警与数据备份
服务器·网络协议·ui·docker·容器·https·开源
LF男男21 小时前
Flower Defense—UIFacade
unity
笨笨饿21 小时前
#60_# 一、Photoshop 2025 如何批量导出 PSD 文件中的所有图层为独立 PNG 图片
stm32·单片机·嵌入式硬件·物联网·ui·51单片机·photoshop
呆呆敲代码的小Y21 小时前
【实战精选面试题】Unity精选面试题详细解答,按模块整理汇总
人工智能·游戏·unity·游戏引擎