Resources

作者:追风剑情 发布于:2017-7-25 23:55 分类:Unity3d

一、新建Resources目录。

二、把要利用Resources类加载的资源放到此目录下。

示例一:同步加载UI


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 同步加载UI
/// </summary>
public class ResourceTest : MonoBehaviour {

    void Start () {
        //动态加载prefab
        GameObject ui = Resources.Load<GameObject>("UI") as GameObject;
        Instantiate(ui);
    }
}


示例二:异步加载UI


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 异步加载UI
/// </summary>
public class ResourceAsyn : MonoBehaviour {

    void Start () {
        StartCoroutine(LoadUI());
    }

    IEnumerator LoadUI()
    {
        yield return null;
        ResourceRequest req = Resources.LoadAsync<GameObject>("UI");
        while (!req.isDone)
        {
            Debug.Log(req.progress);//输出加载进度
            yield return null;
        }
           
        GameObject ui = req.asset as GameObject;
        Instantiate(ui);
    }
}


运行测试

11111.png

22222.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号