Unity加载CSV配置表

20 篇文章 2 订阅

这个配置管理器的功能是在编辑器模式在从Resources目录下加载配置,打出的包要从AssetBundle里面加载,如果各位不需要的话,直接在DownloadConfigs里面注释掉LoadLocalConfig之外的所有行就好了,这个应该很简单,我就不多说了。

RegistAllLoadFuncs()方法里面注册的是一个传string参数的方法,这个应该也很明显,具体的实现每个项目应该都有自己的一套,我们的项目都是csv格式,大家自己实现就好了。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UConfigManager : MonoBehaviour
{
        private static UConfigManager mInstance;
        public static UConfigManager Instance
        {
                get
                {
                        if (mInstance == null)
                        {
                                GameObject go = new GameObject("UConfigManager");
                                DontDestroyOnLoad(go);
                                mInstance = go.AddComponent<UConfigManager>();
                        }
                        return mInstance;
                }
        }
        public void Init() { }
 
        private Dictionary<string, Action<string>> dExcelLoaders = new Dictionary<string, Action<string>>();
        private void RegistLoadFunc(string sExcelName, Action<string> func)
        {
                dExcelLoaders.Add(sExcelName, func);
        }
        private void RegistAllLoadFuncs()
        {
                /*RegistLoadFunc("Levels", ULoaderLevel.Instance.LoadData);
                RegistLoadFunc("Localization", ULoaderLocalization.Instance.LoadData);*/
        }
        void Awake()
        {
                mInstance = this;
 
                RegistAllLoadFuncs();
 
                DownloadConfigs();
        }
 
        private void DownloadConfigs()
        {
#if UNITY_EDITOR
                LoadLocalConfig();
#else
                StartCoroutine(DoDownload());
#endif
        }
 
        public static string DownLoadConfigPath = "http://192.168.1.100/configs/configs.assetbundle";
        private IEnumerator DoDownload()
        {
                var www = new WWW(DownLoadConfigPath);
                yield return www;
                if (string.IsNullOrEmpty(www.error))
                {
                        var ab = www.assetBundle;
                        LoadAssetBundle(ab);
                        ab.Unload(true);
                }
                else
                {
                        Debug.Log("加载远端配置失败,正在加载本地配置。");
                        LoadLocalConfig();
                }
                www.Dispose();
        }
 
        private void LoadAssetBundle(AssetBundle ab)
        {
                Debug.Log("下载远端配置成功,正在加载远端配置。");
                foreach (var s in dExcelLoaders.Keys)
                {
                        var text = ab.LoadAsset(s) as TextAsset;
                        if (text == null)
                        {
                                Debug.Log("Cannot load " + s + " from asset bundle");
                                continue;
                        }
                        dExcelLoaders[s](text.text);
                }
 
        }
 
        private void LoadLocalConfig()
        {
                foreach (var s in dExcelLoaders.Keys)
                {
                        var text = Resources.Load<TextAsset>("Configs/" + s);
                        if (text == null)
                        {
                                Debug.Log("Cannot load " + s + " from local");
                                continue;
                        }
                        dExcelLoaders[s](text.text);
                }
        }
}

剩下的就是在合适的时机,调用

UConfigManager.Instance.Init();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值