unity3D AssetBundle載入方式

想飛的小李子發表於2018-11-19


首先在Editor資料夾下建立快捷打包的方式
[MenuItem(“AssetBuiled/Build”)]
static void EditorWindow()
{
string path = “AssetBundle”;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
如果需要在安卓端的話 修改下 方法中的BuildTarget
把預製體和材質球設定為下圖格式 然後點選視窗下的AssetBuiled中的Build**
這是載入後的效果 一個cube和對應的材質球

在這裡插入圖片描述
預製體和對應材質球設定 把他們放置在同一個資料夾CC下
在這裡插入圖片描述在這裡插入圖片描述blog.csdnimg.cn/20181119173144714.)這裡插入圖片描述](https://img-blog.csdnimg.cn/20181119173025865.?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x5YjE5OTQwNTI2,size_16,color_FFFFFF,t_70)
void Start()
{
//1.從本地載入
AssetBundle assetBundle = AssetBundle.LoadFromFile(“AssetBundle/cc/cube”);
GameObject cube = assetBundle.LoadAsset(“cube”);
Instantiate(cube);
AssetBundle AB = AssetBundle.LoadFromFile(“AssetBundle/AssetBundle”);
AssetBundleManifest manifset = AB.LoadAsset(“AssetBundleManifest”);
string[] manifsetAB = manifset.GetAllDependencies(“cc/cube”);
foreach (var item in manifsetAB)
{
//獲取到cube所有的依賴項後給載入出來
AssetBundle.LoadFromFile(“AssetBundle/” + item);
}
}加粗樣式
IEnumerator Start()
{
#region 從記憶體載入
AssetBundleCreateRequest asset = AssetBundle.LoadFromFileAsync(@“C:\Users\Administrator\Desktop\鬥地主\單機版\assetBuild\AssetBundle\cc\cube”);
yield return asset;
AssetBundle assetBundle = asset.assetBundle;
GameObject cube = assetBundle.LoadAsset(“Cube”);
Instantiate(cube);
AssetBundle AB = AssetBundle.LoadFromFile(“AssetBundle/AssetBundle”);
AssetBundleManifest manifset = AB.LoadAsset(“AssetBundleManifest”);
string[] manifsetAB = manifset.GetAllDependencies(“cc/cube”);
foreach (var item in manifsetAB)
{
//獲取到cube所有的依賴項後給載入出來
AssetBundle.LoadFromFile(“AssetBundle/” + item);
}
#endregion
#region 用WWW類載入
//WWW a = new WWW(@“http://192.168.201.35/AssetBundle/cc/cube”);
//yield return a;
//AssetBundle asset = a.assetBundle;
//GameObject cube = asset.LoadAsset(“Cube”);
//Instantiate(cube);
//AssetBundle ab = AssetBundle.LoadFromFile(“AssetBundle/AssetBundle”);
//AssetBundleManifest bundleManifest = ab.LoadAsset(“AssetBundleManifest”);
//string[] all = bundleManifest.GetAllDependencies(“cc/cube”);
//foreach (var item in all)
//{
// AssetBundle.LoadFromFile(“AssetBundle/” + item);
//}
#endregion
#region 從伺服器載入
//UnityWebRequest request = UnityWebRequest.GetAssetBundle(@“http://192.168.201.35/AssetBundle/cc/cube”);
//yield return request.Send();
//AssetBundle asset = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
//GameObject cube = asset.LoadAsset(“cube”);
//Instantiate(cube);
//AssetBundle AB = AssetBundle.LoadFromFile(“AssetBundle/AssetBundle”);
//AssetBundleManifest manifset = AB.LoadAsset(“AssetBundleManifest”);
//string[] manifsetAB = manifset.GetAllDependencies(“cc/cube”);
//foreach (var item in manifsetAB)
//{
// //獲取到cube所有的依賴項後給載入出來
// AssetBundle.LoadFromFile(“AssetBundle/” + item);
//}
#endregion
}
目前不太懂伺服器的相關知識,後兩種只是一個小的嘗試

相關文章