转载自:http://blog.youkuaiyun.com/liqiangeastsun/article/details/49719027
写到这才看到未经允许不让转载。。。那我链接就放这吧。那我就写4的食用方法。()
- 以下这些是5.0之前的版本
- //在Unity编辑器中添加菜单
- [MenuItem("Assets/Build AssetBundle From Selection")]
- static void ExportResourceRGB2()
- {
- // 打开保存面板,获得用户选择的路径
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
- if (path.Length != 0)
- {
- // 选择的要保存的对象
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- //打包
- BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
- }
- }
场景
- [MenuItem("Assets/Save Scene")]
- static void ExportScene()
- {
- // 打开保存面板,获得用户选择的路径
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
- if (path.Length != 0)
- {
- // 选择的要保存的对象
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- string[] scenes = {"Assets/scene1.unity"};
- //打包
- BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
- }
- }
注意事项
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
- 加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
- using System;
- using UnityEngine;
- using System.Collections;
- public class Load: MonoBehaviour
- {
- private string BundleURL = "file:///C:/cube.assetbundle";
- private string SceneURL = "file:///C:/scene1.unity3d";
- void Start()
- {
- //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";
- Debug.Log(BundleURL);
- StartCoroutine(DownloadAssetAndScene());
- }
- IEnumerator DownloadAssetAndScene()
- {
- //下载assetbundle,加载Cube
- using (WWW asset = new WWW(BundleURL))
- {
- yield return asset;
- AssetBundle bundle = asset.assetBundle;
- Instantiate(bundle.Load("Cube"));
- bundle.Unload(false);
- yield return new WaitForSeconds(5);
- }
- //下载场景,加载场景
- using (WWW scene = new WWW(SceneURL))
- {
- yield return scene;
- AssetBundle bundle = scene.assetBundle;
- Application.LoadLevel("scene1");
- }
- }
- }
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
下面这些是坑。
-
常见问题IEnumerator DownloadAssetAndScene(){//下载assetbundle,加载Cubeusing (WWW asset = new WWW(url)){yield return asset;AssetBundle bundle = asset.assetBundle;Instantiate(bundle.LoadAsset<GameObject>("Cube"));//这里一定要表明是什么类型//Instantiate(bundle.LoadAsset("Cube"));//这里一定要表明是什么类型,不标明类型是不能产生的bundle.Unload(false);//这里一定要释放assetbundle。因为AB不能加载两次。yield return new WaitForSeconds(5);}
}AssetBundle ab = www.assetBundle;GameObject gobj = ab.LoadAsset(realName) as GameObject;//同样,也是要声明产生物体的类型if (gobj != null)Instantiate(gobj);ab.Unload(false);
以上我们使用的方法都是不打包依赖的,但是本物体的perfab 直接打包的话也会出现问题。最好的办法是把你本想打包的prefab放在
一个空的gameobject下,再进行打包,否则会出现 打包物体没有所带脚本的情况,还有就是再电脑上测试时会出现模型贴图丢失的情况,不要怕,在真机上测试时,贴图还是有的。这一点为什么这么奇葩,我还真说不清楚。
- 以下这些是5.0之前的版本
- //在Unity编辑器中添加菜单
- [MenuItem("Assets/Build AssetBundle From Selection")]
- static void ExportResourceRGB2()
- {
- // 打开保存面板,获得用户选择的路径
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
- if (path.Length != 0)
- {
- // 选择的要保存的对象
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- //打包
- BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
- }
- }
场景
- [MenuItem("Assets/Save Scene")]
- static void ExportScene()
- {
- // 打开保存面板,获得用户选择的路径
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
- if (path.Length != 0)
- {
- // 选择的要保存的对象
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- string[] scenes = {"Assets/scene1.unity"};
- //打包
- BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
- }
- }
注意事项
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
- 加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
- using System;
- using UnityEngine;
- using System.Collections;
- public class Load: MonoBehaviour
- {
- private string BundleURL = "file:///C:/cube.assetbundle";
- private string SceneURL = "file:///C:/scene1.unity3d";
- void Start()
- {
- //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";
- Debug.Log(BundleURL);
- StartCoroutine(DownloadAssetAndScene());
- }
- IEnumerator DownloadAssetAndScene()
- {
- //下载assetbundle,加载Cube
- using (WWW asset = new WWW(BundleURL))
- {
- yield return asset;
- AssetBundle bundle = asset.assetBundle;
- Instantiate(bundle.Load("Cube"));
- bundle.Unload(false);
- yield return new WaitForSeconds(5);
- }
- //下载场景,加载场景
- using (WWW scene = new WWW(SceneURL))
- {
- yield return scene;
- AssetBundle bundle = scene.assetBundle;
- Application.LoadLevel("scene1");
- }
- }
- }
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。