using UnityEngine;
using System.Collections;
using UnityEditor;
//Assetbundle 创建
public class AssetBundleControl{
//逐个打包 //不常用
[MenuItem("Custom Editor/Create AssetBundles Main")]
static void CreateAssetBundlesMain()
{
//获取选取的对象
Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//遍历对象
foreach(var it in selectedAssets)
{
string sourcePath = AssetDatabase.GetAssetPath(it);
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets是只读路径,不能写入
//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + it.name + ".assetbundle";
if(BuildPipeline.BuildAssetBundle(it,null,targetPath,BuildAssetBundleOptions.CollectDependencies))
{
Debug.Log(it.name+"资源打包成功");
}
else
{
Debug.Log(it.name+"资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
//打包在一起
[MenuItem("Custom Editor/Create AssetBundles All")]
static void CreateAssetBundlesAll()
{
Caching.CleanCache(); //清空缓存
string path = Application.dataPath + "/StreamingAssets/All.assetbundle";
Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (var it in selectedAssets)
{
//Debug.Log("create assetbundle name:" + it);
}
if (BuildPipeline.BuildAssetBundle(null, selectedAssets, path, BuildAssetBundleOptions.CollectDependencies))
{
AssetDatabase.Refresh();
Debug.Log("打包所有");
}
else
{
}
}
}
using UnityEngine;
using System.Collections;using System.Collections.Generic;
//Assetbundle 生成资源
public class AssetBundle_LoadAsset:MonoBehaviour {
private static AssetBundle_LoadAsset instance;
public static AssetBundle_LoadAsset Instance
{
get
{
if(instance==null)
{
Debug.Log("this must be added on the object");
return null;
}
return instance;
}
}
private AssetBundle_LoadAsset()
{
instance = this;
}
private GameObject preObj; //还未生成的预制体
private List<GameObject> preObjs;
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif
void OnGUI()
{
//if(GUILayout.Button("单个"))
//{
// LoadMainObject("test1.assetbundle");
//}
//if(GUILayout.Button("多资源的单个资源"))
//{
// LoadTogetherObject_One("All.assetbundle", "test2");
//}
//if (GUILayout.Button("多资源的所有资源"))
//{
// LoadTogetherObject_All("All.assetbundle");
//}
}
//返回一个为生成的物体 包含一个资源的Assetbundle
public GameObject LoadMainObject(string assetName)
{
preObj = null;
StartCoroutine(LoadMainObject_(PathURL+assetName));
return preObj;
}
//返回生成所有的资源 包含多个资源的Assetbundle
public List<GameObject> LoadTogetherObject_All(string path)
{
if(preObjs!=null)
foreach(var it in preObjs)
Destroy(it);
preObjs = null;
preObjs = new List<GameObject>();
StartCoroutine(LoadTogetherObject_All_(PathURL+path));
return preObjs;
}
//返回一个指定生成的物体 包含多个资源的Assetbundle
public GameObject LoadTogetherObject_One(string path,string assetName)
{
preObj = null;
StartCoroutine(LoadTogetherObject_One_(PathURL+path, assetName));
return preObj;
}
private IEnumerator LoadMainObject_(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
yield return preObj=Instantiate(bundle.assetBundle.mainAsset)as GameObject;
bundle.assetBundle.Unload(false);
}
private IEnumerator LoadTogetherObject_All_(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
foreach (var it in bundle.assetBundle.LoadAllAssets())
{
preObjs.Add(Instantiate(it)as GameObject);
}
bundle.assetBundle.Unload(false);
}
private IEnumerator LoadTogetherObject_One_(string path,string assetName)
{
WWW bundle = new WWW(path);
yield return bundle;
yield return preObj =Instantiate(bundle.assetBundle.LoadAsset(assetName))as GameObject;
bundle.assetBundle.Unload(false);
}
}