Qframework 中超级方便的kitres

文章介绍了如何在Unity项目中使用QFramework的ResLoader组件进行资源同步加载,并实现自动回收功能。脚本需配合ResMgr.Init()初始化,加载数据时通过AddObjectForDestroyWhenRecycle2Cache确保资源释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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



public class TestResKit : MonoBehaviour
{
    ResLoader mResLoader = ResLoader.Allocate();
    private void Awake()
    {

    }

    /// <summary>
    /// 每一个需要加载资源的单元(脚本,界面)申请一个ResLoader
    /// ResLoader 本身会记录该脚本加载过的资源
    /// </summary>
    


    /// <summary>
    /// 通过 LoadSync 同步加载资源
    /// 只需要传入资源名就行,不需要传入AssetBundle名
    /// </summary>
    void Start()
    {
        this.GetComponent<SpriteRenderer>().sprite= mResLoader.LoadSync<Sprite>("forest_of_whispers_verdant_woods_tiles") ;
    }

    private void OnDestroy()
    {
        // 释放所有本脚本加载过的资源
        // 释放只是释放资源的引用
        // 当资源的引用数量为 0,会进行真正的卸载操作
        mResLoader.Recycle2Cache();
        mResLoader = null;
    }
}

使用方法就是,把这个脚本贴在需要加载资源的controller上。

用它来加载资源!这样如果自己没有了,资源就会自己回收自己

然后别忘了在什么地方吧这个初始化一下

            ResMgr.Init();

另外一个,就是加载一些数据或者图片集,可以这样做:

        public Sprite GetGoalLevelSprite(string id)
        {
            var spriteAtlas = mResLoader.LoadSync<SpriteAtlas>(GameConfig.GoalMapAtlasPath);
            var icon = spriteAtlas.GetSprite(id);
            mResLoader.AddObjectForDestroyWhenRecycle2Cache(icon);
            return icon;
        }
  public class ConfigSystem : BaseSystem, IConfigSystem
    {
        private ResLoader mResLoader = ResLoader.Allocate();

        private cfg.Tables tables;
        public cfg.Tables Tables => tables;

        public override void RegisterEvents()
        {
            tables = new cfg.Tables(file =>
                JSON.Parse(Resources.Load<TextAsset>("TextAsset/JsonConfig/" + file).text));

        }

        public cfg.LevelMapSet GetMapSet(string id)
        {
            cfg.LevelMapSet result;
            try
            {
                result = Tables.TbLevelMap.Get(id);
            }
            catch (Exception e)
            {
                Debug.LogError($"TbLevelMap id: {id} {e}");
                throw;
            }

            return result;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值