Unity5.X打包与加载AssetBundle
所有脚本代码来自官方
打包AssetBundle:
在Editor界面,点击prefab或文件,在AssetsBundle处设置此prefab的Assetsbundles属性。
点New可以创建AssetBundle名称,输入“/”可设置下一级名称。
多个物体可设为同一个AssetBundle名称。
添加一个脚本,给Editor增加一个打包的功能:
using UnityEditor;
public class CreateAssetBundles
{
[MenuItem ("Assets/Build AssetBundles")]
static void BuildAllAssetBundles ()
{
BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneOSXUniversal);
}
}
其中”AssetBundles”为需要手工添加的文件夹名称。
在顶部Assets目录中的底部会出现Build Assetbundles选项。点击后会开始build进程。
AssetBundles目录中会出现一个AssetBundles文件(作用未知)和一个AssetBundles.manifest文件,manifest文件里写明了各AssetBundles的关系。
如果之前设置的Assetbundle属性有多个层级,则AssetBundles文件夹中会出现相应的下一级文件夹,AssetBundle文件以及它对应的manifest文件。
加载AssetBundle:
有两种方式
1,下载但不储存到缓存中
using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
public string BundleURL;
public string AssetName;
IEnumerator Start() {
// Download the file from the URL. It will not be saved in the Cache
using (WWW www = new WWW(BundleURL)) {
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
Instantiate(bundle.LoadAsset(AssetName));
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
2,下载并储存到缓存中(推荐)
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour {
public string BundleURL;
public string AssetName;
public int version;
void Start() {
BundleURL="file://"+Application.dataPath+BundleURL;
StartCoroutine (DownloadAndCache());
}
IEnumerator DownloadAndCache (){
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
// Instantiate(bundle.mainAsset);
// Instantiate(bundle.LoadAsset(AssetName));
foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
{
Instantiate(temp);
}
// Instantiate(bundle.LoadAllAssets());
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
加载脚本中两个变量的说明:
-变量BundleURL的格式:
–如果为UnityEditor本地:
—“file://”+“application.datapath”+“/文件夹名称/文件名”
–如果为服务器:
—http://….
-变量AssetName:
–为prefab或文件的名字。
如果要同时实例化所有AssetBundle中的物体(如果都是prefab),可用LoadAllAssets()方法,例如:
foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
{
Instantiate(temp);
}
这样,之前被打包进AssetBundle内的prefab就会出现在场景中了。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了