2016年9月10日 星期六

Unity內用WWW載入AssetBundle

大家都知道,AssetBundle是Unity用來打包資源的方式。
大家也知道,AssetBundle對於Script的不方便,一定要在Project內事先放入相同的Script,才能讓AssetBundle上的Script相對應到。

但這都不是重點,本篇的重點是將AssetBundle匯入到Unity程式內使用。
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AssetBundleLoadController : MonoBehaviour {
  5.  
  6.     void Start ()
  7.     {
  8.         StartCoroutine( LoadAssetBundle() );
  9.     }
  10.  
  11.     private IEnumerator LocalLoad()
  12.     {
  13.         WWW fileBundle = new WWW( "file://D:/xxx.assetbundle" );
  14.         yield return fileBundle;
  15.  
  16.         GameObject tempObject = null;
  17.         yield return tempObject = Instantiate( fileBundle.assetBundle.mainAsset ) as GameObject;
  18.         tempObject.name = tempObject.name.Replace( "(Clone)", string.Empty );
  19.  
  20.         fileBundle.assetBundle.Unload( false );
  21.         Resources.UnloadUnusedAssets();
  22.     }
  23. }
這樣就能使用AssetBundle的內容物了。

沒有留言:

張貼留言