选中prefabs,生成AssetBundle
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using UObject = UnityEngine.Object;
public class CreateAseetBundle {
[MenuItem("CreateAssetBundle/AssetBundle_Android")]
static void ExportRoom_Android()
{
CreatAssetBundle(BuildTarget.Android);
}
[MenuItem("CreateAssetBundle/AssetBundle_IOS")]
static void ExportRoom_IOS()
{
CreatAssetBundle(BuildTarget.iOS);
}
[MenuItem("CreateAssetBundle/AssetBundle_Win")]
static void ExportRoom_Win()
{
CreatAssetBundle(BuildTarget.StandaloneWindows);
}
/// <summary>
/// 创建AssetBundle 包括材质等 -->prefabs
/// </summary>
static void CreatAssetBundle(BuildTarget target)
{
UObject[] selection = Selection.GetFiltered(typeof(UObject), SelectionMode.DeepAssets);
foreach (UObject item in selection)
{
string prefabPath = AssetDatabase.GetAssetPath(item);
string[] resourcesName = new string[1];
resourcesName[0] = prefabPath;
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
buildMap[0].assetBundleName = item.name + "_assetbundle";
Debug.Log(resourcesName[0]);
buildMap[0].assetNames = resourcesName;
string exportPath = GetBundleUrl(target);
BuildPipeline.BuildAssetBundles(exportPath, buildMap, BuildAssetBundleOptions.None, target);
// BuildPipeline.BuildAssetBundles(dirFile, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
AssetDatabase.Refresh();
}
static string GetBundleUrl(BuildTarget target)
{
if (target == BuildTarget.Android)
return Application.streamingAssetsPath + "/Android/";
else if (target == BuildTarget.iOS)
return Application.streamingAssetsPath + "/IOS/";
else if (target == BuildTarget.StandaloneWindows)
return Application.streamingAssetsPath + "/Win/";
return Application.streamingAssetsPath + "/WinEditor/";
}
}
加载
/// <summary>
/// 初始化加载
/// </summary>
IEnumerator GetStartLoadAsset(string assetName)
{
WWW www = new WWW("file://" + Application.streamingAssetsPath + @"/Win/" + assetName + "_assetbundle");
yield return www;
AssetBundle ab = www.assetBundle;
GameObject obj = ab.LoadAsset(assetName) as GameObject;
GameObject genObj = Instantiate(obj);
genObj.name = assetName + "_child";
genObj.transform.parent = GameObject.Find(assetName).transform;
genObj.transform.localPosition = new Vector3(0, 0, 0);
genObj.transform.localScale = new Vector3(1f, 1f, 1f);
genObj.transform.localEulerAngles = new Vector3(0, 0, 0);
www.assetBundle.Unload(false);
}