当前位置: 首页 > article >正文

unity3d————异步加载练习题

1. 代码示例:

ResourcesMgr类

public class ResourcesMgr
{
    private static ResourcesMgr instance  = new ResourcesMgr();
    public static ResourcesMgr Instance => instance;

    private ResourcesMgr()
    {
    }

    public void LoadRes<T>(string name, UnityAction<T> callBack) where T:Object
    {
        ResourceRequest rq = Resources.LoadAsync<T>(name);
        rq.completed += (a) =>
        {
            //直接得到 传入的 对象 通过它得到资源内容 然后转换成对应类型 传出去 外面不需要转换 直接使用
            callBack((a as ResourceRequest).asset as T);
        };
    }
}

 Lesson18Test 类

public class Lesson18Test : MonoBehaviour
{
    private Texture tex;
    // Start is called before the first frame update
    void Start()
    {
        ResourcesMgr.Instance.LoadRes<Texture>("Tex/TestJPG", (obj) =>
        {
            tex = obj;
        });


        //ResourceRequest rq= Resources.LoadAsync<Texture>("Tex/TestJPG");
        //rq.completed += (a) =>
        //{
            //tex = (a as ResourceRequest).asset as Texture;
        //};
    }

    private void OnGUI()
    {
        if(tex != null)
            GUI.DrawTexture(new Rect(0, 0, 100, 100), tex);
    }
}

2.代码示例

public class SceneMgr
{
    private static SceneMgr instance = new SceneMgr();

    public static SceneMgr Instance => instance;

    private SceneMgr() { }

    public void LoadScene(string name, UnityAction action)
    {
        AsyncOperation ao = SceneManager.LoadSceneAsync(name);
        ao.completed += (a) =>
        {
            //通过那么大表达式 包裹一层
            //在内部 直接调用外部传入的委托即可
            action();
        };
    }
}
public class Lesson20Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        SceneMgr.Instance.LoadScene("Lesson20Test", () => {
            print("加载结束");
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


http://www.kler.cn/a/397584.html

相关文章:

  • [A-18]ARMv8/ARMv9-Memory-内存空间的属性(Attributes Properties)
  • OpenCV、YOLO、VOC、COCO之间的关系和区别
  • 前端pdf预览方案
  • Android LiveData 处理数据倒灌的几种措施
  • 计算机视觉 ---图像读取与显示(OpenCV与Matplotlib)
  • ‌EAC(Estimate at Completion)和ETC(Estimate to Complete)
  • c# Encoding.GetEncoding
  • 后端返回大数问题
  • rk3399开发环境使用Android 10初体验蓝牙功能
  • 计算光纤色散带来的相位移动 matlab
  • vue.js设计与实现(霍春阳著) 章节总结
  • golang对日期格式化
  • Tailwind CSS 和 UnoCSS简单比较
  • 数据库管理-第262期 崖山:知其不可而为之(20241116)
  • 【笔记】Vue3回忆录
  • 【C语言指南】C语言内存管理 深度解析
  • aitrader双界面引擎(dash和streamlit),引入zvt作为数据获取及存储支持
  • 以太坊基础知识结构详解
  • 将大型语言模型(如GPT-4)微调用于文本续写任务
  • STM32设计井下瓦斯检测联网WIFI加Zigbee多路节点协调器传输