zoukankan      html  css  js  c++  java
  • 使用ScriptableObject创建.asset文件

    .asset一般用来存储一些配置,比如SDK初始化的相关参数。

    using System.Collections.Generic;
    using UnityEngine;
    
    namespace XXX
    {
        [CreateAssetMenu(fileName="xxx", menuName="xxx")]
        public class CommonConfig : ScriptableObject
        {
            [HideInInspector]
            public List<string> Keys;
    
            [HideInInspector]
            public List<string> Values;
    
            [System.Serializable]
            public class Param
            {
                public string strA = "";
                public string strB = "";
                public string strC = "";
                public string strD = "";
            }
    
            public Param paramXXA = null;
            public Param paramXXB = null;
    
            private Dictionary<string, string> Map;
    
            private static CommonConfig instance;
    
            public static CommonConfig Instance
            {
                get
                {
                    if (instance == null)
                    {
                        instance = (CommonConfig)ResourceManager.LoadAsset("assets/xxx.asset", typeof(CommonConfig));
                    }
    
                    return instance;
                }
            }
    
            private void Awake()
            {
                UpdateData();
            }
    
            private void UpdateData()
            {
                Map = new Dictionary<string, string>(Keys.Count);
    
                for (int i = 0; i < Keys.Count; i++)
                {
                    Map.Add(Keys[i], Values[i]);
                }
            }
    
            public string Get(string key)
            {
                string value;
                if (Map.TryGetValue(key, out value))
                {
                    return value;
                }
    
                return key;
            } 
        }
    }

    定义为public的变量就存储在xxx.asset中,通过 CommonConfig.Instance.Get 直接获取数据。

    如果在运行时需要修改数据,需要使用下面的

    EditorUtility.SetDirty(CommonConfig.Instance);
    AssetDatabase.SaveAssets();
  • 相关阅读:
    泛型与非泛型
    C# 调用CMD,执行DOS命令
    数据库设计随笔(ZZ)
    关于三层开发(zz)
    三层架构学习(转)
    Node.js & child_process All In One
    HTML5 Canvas Tag Cloud All In One
    macOS & VSCode terminal show current git branch All In One
    飞书 excel 固定列 All In One
    git diff one of committed file All In One
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/7603407.html
Copyright © 2011-2022 走看看