Unity之AssetBundles读取打包对象

using UnityEngine;
using System.Collections;


public class RunScript : MonoBehaviour
{


    //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
    public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
 string.Empty;
#endif


    public void OnClickSingleButton()
    {
         Caching.CleanCache();
        //StartCoroutine(Load(PathURL + "login.assetbundle"));
        StartCoroutine(LoadMainGameObject(PathURL + "Prefab1.assetbundle"));
        StartCoroutine(LoadMainGameObject(PathURL + "Prefab2.assetbundle"));
    }


    public void OnClickAllButton()
    {
        StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle"));
    }


    //读取一个资源


    private IEnumerator LoadMainGameObject(string path)
    {
        WWW bundle = new WWW(path);
        yield return bundle;


        //加载到游戏中
        yield return Instantiate(bundle.assetBundle.mainAsset);
        bundle.assetBundle.Unload(false); 
    }








    // 加载
    IEnumerator Load(string path)
    {
        WWW bundle = WWW.LoadFromCacheOrDownload(path, 1);


        yield return bundle;
    }


    //读取全部资源
    private IEnumerator LoadALLGameObject(string path)
    {
        WWW bundle = new WWW(path);


        yield return bundle;


        //加载复合对象,且通过名称把他们读取出来  
        GameObject obj0 = (GameObject)bundle.assetBundle.LoadAsset("Prefab1");
        GameObject obj1 = (GameObject)bundle.assetBundle.LoadAsset("Prefab2");
        //加载到游戏中 
        yield return Instantiate(obj0);
        yield return Instantiate(obj1);
        bundle.assetBundle.Unload(false);
    }


    private IEnumerator LoadMainCacheGameObject(string path)
    {
        WWW bundle = WWW.LoadFromCacheOrDownload(path, 5);


        yield return bundle;


        //加载到游戏中
        yield return Instantiate(bundle.assetBundle.mainAsset);


        bundle.assetBundle.Unload(false);
    }






}

你可能感兴趣的:(unity,打包,Assetbundle)