[Unity3d]打包Assetbundle並加載

因爲咱們要將模型資源放在遠程的服務器端,但若是直接放fbx模型是不能夠加載的,因此咱們能夠將fbx作成預設或者是直接將其打包成assetbundle格式的,而後經過www來加載獲取。html


說下使用方法:web

一、把附件腳本放到工程文件夾下的...\Assets\Editor文件夾下。windows

二、在工程的Project視圖裏點擊想要保存的資源,網絡上推薦的是Prefab,右鍵點擊,選擇菜單裏最下面的兩個選項任意一個均可以,第一個選項對應的自定義屬性有一個過時了,可是不影響使用。緩存

三、指定文件的構建路徑和文件後綴,後綴無所謂。服務器

四、推薦製造作法:網絡

任何形式的資源均可以,包括集合資源,好比建立一個空的GameObject,把全部想要關聯的其餘GameObject都拖進去,而後在project視圖裏建立一個prefab,將這個集合資源GameObject拖進去做爲一個prefab。執行上面的第二、3步驟。編輯器


官方的講解:http://game.ceeger.com/Script/BuildPipeline/BuildPipeline.BuildAssetBundle.htmlide


1.首先要講一下不一樣平臺下的一個StreamingAssets路徑,這是不一樣的。學習

	    //不一樣平臺下StreamingAssets的路徑是不一樣的,這裏須要注意一下。 	    public static readonly string PathURL = #if UNITY_ANDROID   //安卓 		"jar:file://" + Application.dataPath + "!/assets/"; #elif UNITY_IPHONE  //iPhone 		Application.dataPath + "/Raw/"; #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  //windows平臺和web平臺 	"file://" + Application.dataPath + "/StreamingAssets/"; #else         string.Empty; #endif

這就獲取到了不一樣平臺的一個路徑,咱們能夠將打包的文件放在這些路徑下,而後再從這路徑去讀取資源。測試

2.關於打包assetbundle的腳本

using UnityEngine; using System.Collections; using UnityEditor;  public class Test : Editor { 	//打包單個 	[MenuItem("Custom Editor/Create AssetBunldes Main")] 	static void CreateAssetBunldesMain () 	{         //獲取在Project視圖中選擇的全部遊戲對象 		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);           //遍歷全部的遊戲對象 		foreach (Object obj in SelectedAsset)  		{ 			//本地測試:建議最後將Assetbundle放在StreamingAssets文件夾下,若是沒有就建立一個,由於移動平臺下只能讀取這個路徑 			//StreamingAssets是隻讀路徑,不能寫入 			//服務器下載:就不須要放在這裏,服務器上客戶端用www類進行下載。 			string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle"; 			if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {   				Debug.Log(obj.name +"資源打包成功"); 			}  			else  			{  				Debug.Log(obj.name +"資源打包失敗"); 			} 		} 		//刷新編輯器 		AssetDatabase.Refresh ();	 		 	} 	 	[MenuItem("Custom Editor/Create AssetBunldes ALL")] 	static void CreateAssetBunldesALL () 	{ 		 		Caching.CleanCache ();    		string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";   		 		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);   		foreach (Object obj in SelectedAsset)  		{ 			Debug.Log ("Create AssetBunldes name :" + obj); 		} 		 		//這裏注意第二個參數就行 		if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) { 			AssetDatabase.Refresh (); 		} else { 			 		} 	} 	 	[MenuItem("Custom Editor/Create Scene")] 	static void CreateSceneALL () 	{ 		//清空一下緩存 		Caching.CleanCache(); 		string Path = Application.dataPath + "/MyScene.unity3d"; 		string  []levels = {"Assets/Level.unity"};     	//打包場景     	BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes); 		AssetDatabase.Refresh (); 	} 	 } 

前提要新手動建立一個StreamingAssets文件夾

3.讀取資源,這裏只舉例從本地讀取,跟從網絡讀取是同樣的,能夠參考官方文檔:

http://game.ceeger.com/Script/WWW/WWW.html

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"); 	} } 


若是assetbundle文件放在服務器端,直接用www.loadfromcacheordownload()經過版原本控制是否從服務器下載並保存到本地。本人親自測試,這個方法是能下載到本地的,至於下載在本地哪兒我就不太清楚了,我猜想應該是存在沙盒文件下(移動開發者的朋友應該知道),固然也能夠本身來作版本控制,那樣更靈活,而且擺脫www.loadfromcacheordownload()方法的束縛,貌似這個方法存貯文件是有空間大小限制的,聽說是50M,本人沒有親自考證,後期我會本身作一個版本控制!


==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ羣:858550         cocos2dx QQ羣:280818155

====================== 相互學習,共同進步 ===================

相關文章
相關標籤/搜索