好吧!廢話不多講,直接上圖上操作哈!
1.首先還是新建一個unity3d項目,導入需要資源文件,我這邊導入一個asset store 模型插件 unity-chan資源。
2.第一步,我們先創建打包AssetBundle 的環境的,項目需要一個內置的文件夾Editor
(這是必須操作,這是爲了可以在項目編輯器中打包AssetBundle資源),並且附上AssetBundleEditor.cs組件類。 需要注意的是 :不同的平臺參數是不一樣的,IOS平臺上Assetbundle打包時需要使用 BuildTarget.iPhone 參數。
AssetBundleEditor.cs 代碼如下:
-
using UnityEngine;
-
using System.Collections;
-
using UnityEditor;
-
using System.IO;
-
-
public class AssetBundleEditor : Editor
-
{
-
-
public static string sourcePath = Application.dataPath + "/Resource";
-
private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets";
-
-
[MenuItem("AssetBundles/BuildAssetBundle")]
-
public static void BuildAssetBundle()
-
{
-
string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS));
-
if (!Directory.Exists(outputPath))
-
{
-
Directory.CreateDirectory(outputPath);
-
}
-
-
-
BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/IOS",BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS);
-
-
AssetDatabase.Refresh();
-
-
Debug.Log("打包完成");
-
-
}
-
}
-
-
public class Platform
-
{
-
public static string GetPlatformFolder(BuildTarget target)
-
{
-
switch (target)
-
{
-
case BuildTarget.Android:
-
return "Android";
-
case BuildTarget.iOS:
-
return "IOS";
-
case BuildTarget.StandaloneWindows:
-
case BuildTarget.StandaloneWindows64:
-
return "Windows";
-
case BuildTarget.StandaloneOSXIntel:
-
case BuildTarget.StandaloneOSXIntel64:
-
case BuildTarget.StandaloneOSXUniversal:
-
return "OSX";
-
default:
-
return null;
-
}
-
}
-
}
3.第二步,我們把打包好的AssetBundle資源解包出來,現在在場景中新建一個LoadAssetBundle的對象,實現加載asset bundle資源的腳本。
LoadAssetBundle.cs 代碼如下:
-
using UnityEngine;
-
using System.Collections;
-
using System.Collections.Generic;
-
-
public class LoadAssetBundle : MonoBehaviour
-
{
-
-
public string PathURL;
-
public GameObject CurrentObj;
-
-
void Start()
-
{
-
#if UNITY_IPHONE
-
PathURL = "file://"+Application.dataPath + "/Raw/IOS";
-
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
-
PathURL = "file://"+Application.streamingAssetsPath+"/IOS";
-
#endif
-
-
}
-
-
void OnGUI()
-
{
-
if(GUI.Button(new Rect(100,100,100,100),"加載美少女"))
-
{
-
StartCoroutine (GetAssetBundleObj("girl_001",PathURL));
-
}
-
}
-
-
public IEnumerator GetAssetBundleObj(string objName,string path="")
-
{
-
string filePath = System.IO.Path.Combine (path,objName);
-
-
WWW w = new WWW(filePath);
-
yield return w;
-
AssetBundle curBundleObj = w.assetBundle;
-
-
AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof(GameObject));
-
yield return obj;
-
CurrentObj = Instantiate(obj.asset) as GameObject;
-
-
yield return null;
-
curBundleObj.Unload(false);
-
w.Dispose();
-
}
-
-
}
4.其實從運行項目的時候,我們會發現模型的加載不正常,因爲它丟失了材質,而我們找到的解決的方法是在Edit->Project Setting->Graphics
設置中的Always Include Shaders 中增加模型的Shader,Shader配置好以後,記得重新打包模型。
5.好吧,接下來我們Build一個版本在真機上,這個涉及到 IOS 開發部署流程,有興趣的同學可以去研究一下,然後,我在這邊直接操作啦!
5.0 首先BuildSetting 切換平臺到IOS;
5.1 直接在真機上的效果。
最後,我們來看一下www訪問方式:
(1).非緩存方式:
WWW w=new WWW(url:string);
通過創建一個WWW實例來對AssetBundle文件下載,下載後的AssetBundle文件將不會進入Unity的緩存區。
(2)緩存方式:
WWW w=WWW.LoadFromCacheOrDownload(url:string,version:int);
下載後的AssetBundle會自動被保存到Unity引擎的緩存區內,該方法是Unity推薦的AssetBundle下載方式。下載AssetBundle的時候,該接口會先在本地緩存中查找該文件,看其之前是否被下載過,如果下載過,則直接從緩存中加載,如果沒有,則從服務器盡享下載。這樣做的好處是可以節省AssetBundle文件的下載時間,從而提高遊戲資源的載入速度(還可以節省下載流量)。
注意:但是WWW.LoadFromCacheOrDownload(url:string,version:int)在測試情況下你可能會頻繁的打包生成Assetbundle,如果忘記改版本號的話可能會讀取之前的緩存,可能就會看不到新的效果,所以建議在bunild
Assetbundle的時候強制清空一下緩存。
Caching.CleanCache();