天天看點

05AssetBundle的四種加載資源方式

05AssetBundle的四種加載資源方式

file:///C:/Program%20Files%20(x86)/unity2018/Editor/Data/Documentation/en/Manual/AssetBundles-Native.html

01:從記憶體中加載

05AssetBundle的四種加載資源方式
void Start()
    { 
        StartCoroutine(LoadAssetBundles(Application.streamingAssetsPath + "/share", "share"));
        StartCoroutine(LoadAssetBundles(Application.streamingAssetsPath + "/cubewall.unity3d", "cubewall"));
    }


    IEnumerator LoadAssetBundles(string path,string name)
    {
       AssetBundleCreateRequest request=AssetBundle.LoadFromFileAsync(path);
       yield return request;
       AssetBundle assetBundle=request.assetBundle;
       GameObject item=assetBundle.LoadAsset<GameObject>(name);
        if (item!=null)
        {
            GameObject go = GameObject.Instantiate(item);
            go.name = "cubewall";
        }
      
    }      
05AssetBundle的四種加載資源方式

02:從本地加載

05AssetBundle的四種加載資源方式

03從伺服器加載

05AssetBundle的四種加載資源方式

本地加載

void Start()
    {
 string path= "file:" + Application.streamingAssetsPath + "/cubewall.unity3d";
        StartCoroutine(WWWAssetBundles(path, "cubewall"));
 IEnumerator WWWAssetBundles(string path,string name)
 }
    {
        //是否準備好
        while (Caching.ready==false)
        {
            yield return null;
        }
        //從本地加載
        //WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);
        //從伺服器加載
        //WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);
        WWW www=WWW.LoadFromCacheOrDownload(path, 1);
        yield return www;
        //是否報錯
        if (string.IsNullOrEmpty(www.error)==false)
        {
           Debug.Log(www.error);
           yield break; //停止協程
        }

        AssetBundle assetBundle=www.assetBundle;
        GameObject item=Instantiate(assetBundle.LoadAsset<GameObject>(name));
        item.name = "item";
    }      

伺服器加載

string path= "http://localhost/StreamingAssets/cubewall.unity3d";
        StartCoroutine(WWWAssetBundles(path, "cubewall"));