Resources加载资源

2017-09-15  本文已影响28人  晓龙酱

注意:

void Start()
{
    GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
    Renderer rend = go.GetComponent<Renderer>();
    rend.material.mainTexture = Resources.Load("glass") as Texture;
}

Resources.Load(string path, Type systemTypeInstance);

void Start()
{
    // cube.mat 与 cube.prefab重名,所以加载时要指定类型
    Object objMat = Resources.Load("Materials\cube", typeof(Material));

    Object objGO = Resources.Load("Prefabs\cube", typeof(GameObject));

    GameObject go = Instantiate(objGO) as GameObject;
    go.getComponent<MeshRenderer>().material = objMat;
}

Resources.LoadAll

public static Object[] LoadAll(string path);
public static Object[] LoadAll(string path, Type systemTypeInstance);
public static T[] LoadAll(string path);
void Start()
{   
    GameObject[] arr = Resources.LoadAll<GameObject>("Prefabs");
    foreach(GameObject go in arr)
    {
        Instantiate(go);
    }
}

Resources.LoadAsync

public static ResourceRequest LoadAsync(string path);
public static ResourceRequest LoadAsync(string path, Type type);
void Start () {

    StartCoroutine(Load(new string[]{"Prefabs/Cube", "Prefabs/Sphere"}));
       

    // 使用协程异步加载
    IEnumerator Load(string[] arr)
    {
        foreach(string str in arr)
        {
            ResourceRequest rr = Resources.LoadAsync<GameObject>(str);
            yield return rr;

            Instantiate(rr.asset).name = rr.asset.name;
        }
    }
}

Resources.UnloadUnusedAssets

上一篇 下一篇

猜你喜欢

热点阅读