转载地址:http://blog.sina.com.cn/s/blog_140bb6bd40102xajb.html
AssetBundle打包:脚本放在Editor文件夹内
具体代码如下:
using UnityEditor;
using System.IO;
public class CreateAssetBundles {
[MenuItem("自定义菜单/资源打包")]
static void BuildAllAssetBundles()
{
//定义文件夹名字
string dir = "AssetBundles";
//文件夹不存在,则创建
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
//资源打包
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
}
AssetBundle的四种方式资源加载,脚本绑在场景任意物体即可
具体代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class DownLoad : MonoBehaviour {
IEnumerator Start ()
{
//资源包路径
string path1 = "AssetBundles/cubewall.unity3d";
//共享依赖资源包路径
string path2 = "AssetBundles/share.unity3d";
//第一种加载AB的方式 ,从内存中加载 LoadFromMemory
#region
//方法一:异步加载
//加载资源
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
yield return request;
//加载共同依赖资源,如贴图、材质
AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
yield return request2;
AssetBundle ab = request.assetBundle;
AssetBundle ab2 = request2.assetBundle;
//使用里面的资源
GameObject wallPrefab1 = ab.LoadAsset("CubeWall");
Instantiate(wallPrefab1);
//方法二:同步加载(无需用协程)
//加载资源
AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1));
//加载共同依赖资源,如贴图、材质
AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));
//使用里面的资源
GameObject wallPrefab2 = ab.LoadAsset("CubeWall");
Instantiate(wallPrefab2);
#endregion
//第二种加载AB的方式 ,从本地加载 LoadFromFile(无需用协程)
#region
AssetBundle ab5 = AssetBundle.LoadFromFile(path1);
AssetBundle ab6 = AssetBundle.LoadFromFile(path2);
GameObject wallPrefab3 = ab5.LoadAsset("CubeWall");
Instantiate(wallPrefab3);
#endregion
//第三种加载AB的方式 ,从本地或服务器加载 WWW(将被弃用)
#region
//是否准备好
while (Caching.ready == false)
{
yield return null;
}
//从本地加载
//WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);
//从服务器加载
WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);
yield return www;
//是否报错
if (string.IsNullOrEmpty(www.error) == false)
{
Debug.Log(www.error); yield break;
}
AssetBundle ab7 = www.assetBundle;
//使用里面的资源
GameObject wallPrefab4 = ab7.LoadAsset("CubeWall");
Instantiate(wallPrefab4);
#endregion
//第四种加载AB方式 从服务器端下载 UnityWebRequest(新版Unity使用)
#region
//服务器路径 localhost为IP
string uri = @"http://localhost/AssetBundles/cubewall.unity3d";
UnityWebRequest request3 = UnityWebRequest.GetAssetBundle(uri);
yield return request3.Send();
//AssetBundle ab8 = ((DownloadHandlerAssetBundle)request3.downloadHandler).assetBundle;
AssetBundle ab8 = DownloadHandlerAssetBundle.GetContent(request3);
//使用里面的资源
GameObject wallPrefab5 = ab8.LoadAsset("CubeWall");
Instantiate(wallPrefab5);
//加载cubewall.unity3d资源包所依赖的资源包
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset("AssetBundleManifest");
//foreach(string name in manifest.GetAllAssetBundles())
//{
// print(name);
//}
//cubewall.unity3d资源包所依赖的资源包的名字
string[] strs = manifest.GetAllDependencies("cubewall.unity3d");
foreach (string name in strs)
{
AssetBundle.LoadFromFile("AssetBundles/" + name);
}
#endregion
}
}