unity建立和加載AssetBundle

先說一下爲何要使用AssetBundle吧,之前作東西一直忽略這個問題,如今認爲這個步驟很重要,代碼是次要的,決策和爲何這樣搞纔是關鍵。ui

一句話歸納吧,AssetBundle實現了資源與服務分離,方便作熱更新。this

1、建立AssetBundleurl

兩步:1.設置AssetBundleName;2.調用BuildPipeline.BuildAssetBundles。詳細過程以下:3d

設置AssetBundleName有兩種方式,分爲手動和代碼orm

先講手動,找到你須要被打包的文件,而後以下圖blog

方式2,代碼設置這個AssetBundleName,代碼以下,建立bundle的代碼也一塊兒貼上了:遊戲

using UnityEngine;
using System.IO;  
#if UNITY_EDITOR
using UnityEditor;
#endif

public class BuildBundleMenu : MonoBehaviour {
	
	public static string sourcePathPrefab = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Prefabs/";
	public static string sourcePathMater = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Materials/";
	
	#if UNITY_EDITOR
	[MenuItem( "Example/Build Asset Bundles" )]
	static void BuildABs( )
	{
		
		ClearAssetBundlesName ();  
   		
		FindAllFile (sourcePathPrefab); 
		FindAllFile (sourcePathMater); 
		
		// Put the bundles in a folder called "ABs" within the Assets folder.
		BuildPipeline.BuildAssetBundles( "Assets/ABs", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneOSXIntel64);
	}
	
	
	/// <summary>  
	/// 清除以前設置過的AssetBundleName,避免產生沒必要要的資源也打包  
	/// 以前說過,只要設置了AssetBundleName的,都會進行打包,不論在什麼目錄下  
	/// </summary>  
	static void ClearAssetBundlesName()  
	{  
		int length = AssetDatabase.GetAllAssetBundleNames ().Length;  
		Debug.Log (length);  
		string[] oldAssetBundleNames = new string[length];  
		for (int i = 0; i < length; i++)   
		{  
			oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];  
		}  
   
		for (int j = 0; j < oldAssetBundleNames.Length; j++)   
		{  
			AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);  
		}  
		length = AssetDatabase.GetAllAssetBundleNames ().Length;  
		Debug.Log (length);  
	}  
	
   	/// <summary>
   	/// 遍歷文件夾裏面的全部文件夾和文件
   	/// </summary>
   	/// <param name="source"></param>
	static void FindAllFile(string source)  
	{  
		DirectoryInfo folder = new DirectoryInfo (source);  
		FileSystemInfo[] files = folder.GetFileSystemInfos ();  
		int length = files.Length;  
		for (int i = 0; i < length; i++) {  
			if(files[i] is DirectoryInfo)  
			{  
				FindAllFile(files[i].FullName);  
			}  
			else  
			{  
				if(!files[i].Name.EndsWith(".meta"))  
				{  
					SetAssetName (files[i].FullName);  
				}  
			}  
		}  
	}  
   
	/// <summary>
	/// 爲須要打包的文件設置assetName.
	/// </summary>
	/// <param name="source"></param>
	static void SetAssetName(string source)  
	{    
		string _assetPath = "Assets" + source.Substring (Application.dataPath.Length);  
		//string _assetPath2 = source.Substring (Application.dataPath.Length + 1);   
		//在代碼中給資源設置AssetBundleName  
		AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);  
		//string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);  
		//assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");  
		//assetImporter.assetBundleName = assetName;
		assetImporter.assetBundleName = "Weather.unity3d";
	}  
	#endif
}

  菜單欄會出現這個,點一下就能夠了,bundle建立完成。注意打bundle前要如今Assets目錄下新建一個ABs文件夾,打的bundle都在這個文件夾裏面。ip

 

注:只要設置了AssetBundleName的,都會進行打包,不論在什麼目錄下。資源

2、bundle的加載get

直接貼代碼吧

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadAssetbundle : MonoBehaviour {

	private string pathurl = "";
	// Use this for initialization
	void Start () {
		string pathPrefab = "file://" + Application.dataPath + "/ABs/weather.unity3d";
		
		StartCoroutine (LoadALLGameObject(pathPrefab));
	}

	//讀取所有資源  
	private IEnumerator LoadALLGameObject(string path)  
	{  
		
		WWW bundle = new WWW(path);  

		yield return bundle;  

		//經過Prefab的名稱把他們都讀取出來  
		Object  obj0 =  bundle.assetBundle.LoadAsset("CloudStorm_02_8x8-64.prefab");   
		
		//加載到遊戲中     
		yield return Instantiate(obj0);  
		bundle.assetBundle.Unload(false);  
	} 
}
相關文章
相關標籤/搜索