Unity下的游戏资源管理器C#

项目中的资源管理器代码:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 资源管理器
namespace C_Framework
{
    public class C_ResMgr : C_MonoSingleton<C_ResMgr>
    {
        // 正在加载的资源映射
        private List<C_IResLoader> m_LoadingResLoaderList = new List<C_IResLoader>();

        // 已经加载的资源映射
        private List<C_IResLoader> m_LoadedResLoaderList = new List<C_IResLoader>();

        // AssetBundle
        private List<C_IResLoader> m_AssetBundleLoaderList = new List<C_IResLoader>();


        void Update()
        {
            // 不能用foreach remove后会报错
            // 遍历正在加载的资源,检测是否已经加载完成

            for (int i = m_LoadingResLoaderList.Count - 1; i >= 0; i--)
            {
                C_IResLoader loader = m_LoadingResLoaderList[i];
                if (loader.IsDone())
                {
                    m_LoadedResLoaderList.Add(loader);

                    loader.LoadDoneCallback();
                    m_LoadingResLoaderList.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 判断是否是已经加载完成的资源
        /// </summary>
        /// <param name="resName">资源名</param>
        /// <returns></returns>
        public bool IsLoadedRes(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return false;

            resName = StandardResName(resName);

            foreach (C_IResLoader loader in m_LoadedResLoaderList)
            {
                if (loader.ResName == resName)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// 判断是否是已经加载完成的资源
        /// </summary>
        /// <param name="resName">资源名</param>
        /// <returns></returns>
        public bool IsLoadedAssetBundle(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return false;

            resName = StandardAssetBundleName(resName);

            foreach (C_IResLoader loader in m_AssetBundleLoaderList)
            {
                if (loader.ResName == resName && loader.IsDone())
                    return true;
            }

            return false;
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="resName"></param>
        /// <param name="loadResDone"></param>
        public void LoadRes(string resName, C_VoidDelegate.WithObject loadResDone = null)
        {
            if (string.IsNullOrEmpty(resName))
                return;

            resName = StandardResName(resName);

            foreach (C_IResLoader loader in m_LoadedResLoaderList)
            {
                if (loader.ResName == resName)
                {
                    if (loadResDone != null)
                        loadResDone(loader.GetResObj());

                    return;
                }
            }

            foreach (C_IResLoader loader in m_LoadingResLoaderList)
            {
                if (loader.ResName == resName)
                {
                    if (loadResDone != null)
                        loader.AddLoadDoneCallback(loadResDone);

                    return;
                }
            }

            C_ResourceLoader resLoader = new C_ResourceLoader(resName, loadResDone);
            m_LoadingResLoaderList.Add(resLoader);
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="resName"></param>
        /// <param name="loadResDone"></param>
        public void LoadAssetBundle(string resName, C_VoidDelegate.WithObject loadResDone = null)
        {
            if (string.IsNullOrEmpty(resName))
                return;

            resName = StandardAssetBundleName(resName);

            foreach (C_IResLoader loader in m_AssetBundleLoaderList)
            {
                if (loader.ResName == resName)
                {
                    if (loadResDone != null)
                    {
                        if (loader.IsDone())
                        {
                            loadResDone(loader.GetResObj());
                        }
                        else
                        {
                            loader.AddLoadDoneCallback(loadResDone);
                        }
                    }

                    return;
                }
            }

            C_AssetBundleLoader resLoader = new C_AssetBundleLoader(resName, loadResDone);
            m_AssetBundleLoaderList.Add(resLoader);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Unload(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return;

            UnloadRes(resName);
            UnloadAssetBundle(resName);
        }

        public void UnloadRes(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return;

            resName = StandardResName(resName);

            foreach (C_IResLoader loader in m_LoadingResLoaderList)
            {
                if (loader.ResName == resName)
                {
                    loader.Unload();

                    m_LoadingResLoaderList.Remove(loader);
                }
            }

            foreach (C_IResLoader loader in m_LoadedResLoaderList)
            {
                if (loader.ResName == resName)
                {
                    loader.Unload();

                    m_LoadedResLoaderList.Remove(loader);
                }
            }
        }

        public void UnloadAssetBundle(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return;

            resName = StandardAssetBundleName(resName);

            foreach (C_IResLoader loader in m_AssetBundleLoaderList)
            {
                if (loader.ResName == resName)
                {
                    loader.Unload();

                    m_AssetBundleLoaderList.Remove(loader);
                }
            }
        }

        public string StandardResName(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return "";

            int end = resName.LastIndexOf('.');
            return resName.Substring(0, (end == -1 ? resName.Length : end));
        }

        public string StandardAssetBundleName(string resName)
        {
            if (string.IsNullOrEmpty(resName))
                return "";

            return resName.Substring(resName.LastIndexOf("/") + 1).ToLower();
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值