美文网首页
Resources加载资源

Resources加载资源

作者: 晓龙酱 | 来源:发表于2017-09-15 19:25 被阅读28次

    注意:

    • 资源必须放在Resources目录下
    • 资源路径相对于Resources目录,且不加后缀名
    • 即使是后缀不同的资源,也不要重名,否则加载的时候要指定类型。
    • 路径使用正斜杠”/”
    • Resources中的所有资源,都会被打到客户端中
    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

    相关文章

      网友评论

          本文标题:Resources加载资源

          本文链接:https://www.haomeiwen.com/subject/vudesxtx.html