1.1 .从unity中打包gameobject到指定文件中//在Unity编辑器中添加菜单
[MenuItem("Assets/Build AssetBundle From Selection")]
static void ExportResource()
{
// 打开保存面板,获得用户选择的路径
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);
//最后两个参数还可以该变,当报错不能解压的时候,在加上BuildAssetBundleOptions.UncompressedAssetBundle,一般 BuildTarget不用改,除非使用webplayer
}
}
这时候就可以在编辑器的asset菜单下面看到我们创建的Build AssetBundle From Selection选项,选择一个场景中的预制物体,就可以打包出来了,注意AssetBundle的保存后缀名可以是assetbundle或者unity3d。
2.1从unity中打包场景到指定文件中
[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);
}
}
2.2这时候就可以在编辑器的asset菜单下面看到我们创建的Build AssetBundle From Selection选项,选择一个场景,就可以打包出来了,注意BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer。
以上便是打包场景和gameobject到本地的方法,下面我们来实现从本地加载东西到编辑器中
方法一:使用www加载
using System;
using UnityEngine;
using System.Collections;
public class Load: MonoBehaviour
{
private string BundleURL = "file:///C:/cube.assetbundle";自己保存导出来的 gameobject的目录,这个后缀名ye可以为 .unity3d;
private string SceneURL = "file:///C:/scene1.unity3d";//自己保存导出来的场景的目录
void Start()
{
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");
}
}
}
方法二:直接用AssetBundle.Load来进行加载,其中Address =“C://”,Name= “cube”,最后一个参数为保存的后缀名可以为unity3d或者assetbundle,要是场景的话为unity3d;
AssetBundle assetRequest = AssetBundle.CreateFromFile(Address+ Name+ ".assetbundle");
UnityEngine.Object obj= assetRequest.LoadAll(“cube”);
if (obj.GetType().ToString() == "UnityEngine.GameObject") //之所以判断一下,防止你保存的根目录下有别的物体
{
Gameobject go= Instantiate(obj) as GameObject;
}
assetRequest.Unload(false);//释放掉,方便下一次的使用
方法三:如果存储的时候放在中问路径,方法一的www就不行了,只用借助方法二,且使用FileStream,可以先通过文件流把AssetBundle读取到内存,在通过CreateFromMemory 创建AssetBundle资源,这样的话就避开了WWW不能加载中文路径的问题。
public class LoadAsset : MonoBehaviour
{
AssetBundleCreateRequest asset;//定义一个资源包创建请求
void Start()
{
StartCoroutine(LoadAssetBundle()); //启动加载
}
IEnumerator LoadAssetBundle()
{
FileStream AssetIO = new FileStream(Application.dataPath + @"/Prb/哈哈/cube.assetbundle", FileMode.Open, FileAccess.ReadWrite);//创建文件流
byte[] assetbytes = new byte[AssetIO.Length];//设置读取长度
AssetIO.Read(assetbytes, 0, (int)AssetIO.Length);//读入内存
AssetIO.Close();//关闭内存
asset = AssetBundle.CreateFromMemory(assetbytes);//从内存中创建资源
yield return asset;
AssetBundle asb = asset.assetBundle;
if(asset.isDone)
{
Instantiate(asb.Load("cube"));
}
}