AssetBundle加载的四种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27032631/article/details/77575316
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class LoadFromFileExample : MonoBehaviour {

    string path;
   
	void Start () {


        path = "AssetBundle/scene/cubewall.unity3d";//路径
        AssetBundle.LoadFromFile("AssetBundle/material/wood.unity3d");//加载依赖贴图,材质

        //  StartCoroutine(LoadAssetBundleFromMemoryAsync());
        //  LoadAssetBundleFromMemory();
        //  LoadFromFile();
          StartCoroutine(LoadFromFileAsyn());
    }


    #region 第一种类型

    //第一种加载异步
    IEnumerator LoadAssetBundleFromMemoryAsync()
    {
        AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return createRequest;
        AssetBundle AB = createRequest.assetBundle;
        GameObject obj = AB.LoadAsset<GameObject>("cube");
        Instantiate(obj);
    }
    //第一种同步加载
    private void LoadAssetBundleFromMemory()
    {
         AssetBundle AB= AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
         GameObject obj = AB.LoadAsset<GameObject>("cube");
        Instantiate(obj); 
    }

    #endregion

    #region 第二种类型
    //同步加载
    void LoadFromFile()
    {
        AssetBundle AB = AssetBundle.LoadFromFile(path);
        if (AB == null)
        {
            Debug.LogWarning("Fail to load AssetBundle");
            return;
        }
        GameObject obj = AB.LoadAsset<GameObject>("cube");
        Instantiate(obj);
    }
    //异步加载
    IEnumerator LoadFromFileAsyn()
    {
         AssetBundleCreateRequest createRequest=AssetBundle.LoadFromFileAsync(path);
        yield return createRequest;

        AssetBundle AB = createRequest.assetBundle;
        if (AB == null)
        {
            Debug.LogWarning("Fail to load AssetBundle");
            yield break;
        }

       AssetBundleRequest Request = AB.LoadAssetAsync<GameObject>("cube");
        yield return Request;
        if (Request == null)
        {
            Debug.LogWarning("Fail to load Request");
            yield break;
        }
        GameObject obj = Request.asset as GameObject;
        Instantiate(obj);

    }

    #endregion
}

猜你喜欢

转载自blog.csdn.net/qq_27032631/article/details/77575316
今日推荐