unity創建和加載AssetBundle


先說一下為什么要使用AssetBundle吧,以前做東西一直忽略這個問題,現在認為這個步驟很重要,代碼是次要的,決策和為什么這樣搞才是關鍵。

一句話概括吧,AssetBundle實現了資源與服務分離,方便做熱更新。

一、創建AssetBundle

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

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

先講手動,找到你需要被打包的文件,然后如下圖

方式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都在這個文件夾里面。

 

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

二、bundle的加載

直接貼代碼吧

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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM