1. 程式人生 > >【Unity】Unity5版本的AssetBundle打包方案之資源打包

【Unity】Unity5版本的AssetBundle打包方案之資源打包

using UnityEngine;
using System.Collections;
using UnityEditor;
/// <summary>
/// 指令碼位置:Editor資料夾下
/// 指令碼功能:打包
/// </summary>
public class CreateAssetBundle:Editor
{
	[MenuItem ("Assets/Build AssetBundle Long")]
	static void BuildAssetsBundles ()
	{
		BuildPipeline.BuildAssetBundles (Application.dataPath + "/AssetBundles");
	}

}

using UnityEngine;
using System.Collections;
/// <summary>
/// 指令碼位置:需要載入物體的場景中任意物體上
/// 指令碼功能:載入物體
/// </summary>
public class LoadAssetBundle : MonoBehaviour
{
		
	private string url;
	private string assetname;

	void Start ()
	{
		url = "file://" + Application.dataPath + "/AssetBundles/player1.assetbundle";

		// unity預製體名字
		assetname = "Long";

		StartCoroutine (Download ());
	}

	IEnumerator Download ()
	{

		WWW www = new WWW (url);
		yield return www;
		if (www.error != null) {
			Debug.Log ("下載失敗");

		} else {
			AssetBundle bundle = www.assetBundle;

			// 載入的是預設體的名字,不是打包的assetbundle的名字
			Object obj = bundle.LoadAsset (assetname);
			Instantiate (obj,Vector3.zero,Quaternion.identity);   
                 
                        // AssetBundle.Unload(false),釋放AssetBundle檔案記憶體映象,不銷燬Load建立的Assets物件
                        // AssetBundle.Unload(true),釋放AssetBundle檔案記憶體映象同時銷燬所有已經Load的Assets記憶體映象
                        bundle.Unload (false);
		}

		// 中斷正在載入過程中的WWW
		www.Dispose ();
	}


}