您好,登錄后才能下訂單哦!
支持多平臺的Assetbundle的應用示例,貼代碼,供學習!
這里不同平臺的StreamingAssets是不同的,所以我們要寫預處理!
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 void OnGUI() { if(GUILayout.Button("Main Assetbundle")) { //StartCoroutine(LoadMainGameObject(PathURL + "Prefab0.assetbundle")); //StartCoroutine(LoadMainGameObject(PathURL + "Prefab1.assetbundle")); StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab0.assetbundle")); StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab1.assetbundle")); } if(GUILayout.Button("ALL Assetbundle")) { StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle")); } if(GUILayout.Button("Open Scene")) { StartCoroutine(LoadScene()); } } //讀取一個資源 private IEnumerator LoadMainGameObject(string path) { WWW bundle = new WWW(path); yield return bundle; //加載到游戲中 yield return Instantiate(bundle.assetBundle.mainAsset); bundle.assetBundle.Unload(false); } //讀取全部資源 private IEnumerator LoadALLGameObject(string path) { WWW bundle = new WWW(path); yield return bundle; //通過Prefab的名稱把他們都讀取出來 Object obj0 = bundle.assetBundle.Load("Prefab0"); Object obj1 = bundle.assetBundle.Load("Prefab1"); //加載到游戲中 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); } private IEnumerator LoadScene() { WWW download = WWW.LoadFromCacheOrDownload ("file://"+Application.dataPath + "/MyScene.unity3d", 1); yield return download; var bundle = download.assetBundle; Application.LoadLevel ("Level"); } }
關于Unity3D,我們有個專門技術討論的大群,可以進行技術交流和咨詢,群號:858550 歡迎進行技術討論,里面有不少大牛
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。