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

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

相关推荐
zdr尽职尽责2 天前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
辰小忆3 天前
11 · RunCurveChart —— 实时运行曲线
c++·ui·duilib
ellis19704 天前
Claude Code[二] 上下文相关的机制
unity·ai编程
敲上瘾5 天前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
zdr尽职尽责5 天前
Unity 3C 动画系统:从「蜘蛛网」到可维护的动画架构
unity·架构·游戏引擎
国家一级摸鱼选手5 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
FRing036 天前
vue2干货总结10(json-server、vant-ui、postcss插件、axios二次封装、图形验证码、toast轻提示)
开发语言·前端·vue.js·ui·json·vue2·postcss
暴走约伯7 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
xy34537 天前
Axure 9.0 垂直菜单介绍
ui·产品经理·axure·原型设计
寂寞的船8 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua