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

相关推荐

  1. 【C#】Xasset资源模块

    2024-02-09 09:16:03       32 阅读
  2. 【运行环境】资源的形式

    2024-02-09 09:16:03       16 阅读
  3. 模型参数

    2024-02-09 09:16:03       19 阅读
  4. react之unpkg.com前端资源慢、不出

    2024-02-09 09:16:03       31 阅读
  5. flutter学习-day8-资源文件和管理

    2024-02-09 09:16:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-09 09:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-09 09:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-09 09:16:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-09 09:16:03       20 阅读

热门阅读

  1. K8S系列文章之 [Alpine搭建docker环境]

    2024-02-09 09:16:03       29 阅读
  2. Go基础知识学习-习题题解

    2024-02-09 09:16:03       34 阅读
  3. 2023.2.6

    2023.2.6

    2024-02-09 09:16:03      37 阅读
  4. SQL世界之命令语句Ⅲ

    2024-02-09 09:16:03       32 阅读
  5. 机器学习之T与F分布

    2024-02-09 09:16:03       28 阅读