【C#】Xasset加载资源模块

分享一下之前接Xasset的模块Code【仅用于业务参考】

using System;
using System.Collections.Generic;
using System.IO;
using Common;
using Cysharp.Threading.Tasks;
using UnityEngine;
using xasset;
using xasset.example;
using Logger = xasset.Logger;
using Object = UnityEngine.Object;

namespace Resource
{
    public class ResourceModule : BaseModule
    {
        public string Name => "ResourceModule"; // 获取对应模块的名称
        public LoadMode loadMode = LoadMode.LoadByRelativePath;

        public string[] filters =
        {
            "Scenes", "Prefabs", "Textures"
        };

        private static Dictionary<string, Asset> cache = new Dictionary<string, Asset>(); // 存储已加载的资源

        public async UniTask BeforeInit()
        {
            Debug.Log($"{Name} XAsset init start");
            Versions.VerifyMode = VerifyMode.Size;
            Bundle.AutoUpdate = true;
            await Versions.InitializeAsync();
            Initialize();
            Debug.Log($"{Name} XAsset init finish");
            PreLoadResource();
        }

        public void Initialize()
        {
            switch (loadMode)
            {
                case LoadMode.LoadByName:
                    Manifest.customLoader += LoadByName;
                    break;
                case LoadMode.LoadByNameWithoutExtension:
                    Manifest.customLoader += LoadByNameWithoutExtension;
                    break;
                default:
                    Manifest.customLoader = null;
                    break;
            }
        }

        public void PreLoadResource()
        {
            // 加载预加载资源
            LoadNewAsset<GameObject>("Assets/.prefab");
        }

        //加载指定资源  path相对路径:Assets/.....  type获取的类型
        public static T LoadNewAsset<T>(string path) where T : Object
        {
            if (cache.TryGetValue(path, out var value))
            {
                return (T)value.asset;
            }

            value = Asset.Load(path, typeof(T));
            if (value != null)
            {
                cache.Add(path, value);
                return (T)value.asset;
            }

            Debug.Log("LoadNewAsset value is null");
            return null;
        }

        // 卸载指定路径的资源
        public static void UnLoadAsset(string path)
        {
            //引用计数减 1
            cache[path].Release();
            cache.Remove(path);
        }

        private string LoadByName(string assetPath)
        {
            if (filters == null || filters.Length == 0)
            {
                return null;
            }

            if (!Array.Exists(filters, assetPath.Contains))
            {
                return null;
            }

            var assetName = Path.GetFileName(assetPath);
            return assetName;
        }

        private string LoadByNameWithoutExtension(string assetPath)
        {
            if (filters == null || filters.Length == 0)
            {
                return null;
            }

            if (!Array.Exists(filters, assetPath.Contains))
            {
                return null;
            }

            var assetName = Path.GetFileNameWithoutExtension(assetPath);
            return assetName;
        }

        public void Init()
        {
            // Method intentionally left empty.
        }

        public void ModuleUpdate()
        {
            // Method intentionally left empty.
        }

        public void Resume()
        {
            // Method intentionally left empty.
        }

        public void Restart()
        {
            // Method intentionally left empty.
        }
        
        public void OpenModuleDialog()
        {
            // Method intentionally left empty.
        }

        public void Pause()
        {
            // Method intentionally left empty.
        }

        public void Destroy()
        {
            cache.Clear();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米莱虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值