用代码给选中的对象打包Assetbundle,方便给自己打包的内容分类。如果先设置Assetbundlename在打包的时候会一次将设置了Assetbundle的内容打包,不利于分类。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class BuildAssetBundle
{
[MenuItem("Assets/AssetBundle/Build Each",false,1)]
static void BuildAssetbundle1()
{
BuildEach();
}
[MenuItem("Assets/AssetBundle/Build Each(Uncompress)",false,1)]
static void BuildAssetbundle2()
{
BuildEach(false);
}
static void BuildEach(bool compressed = true)
{
if(Selection.objects.Length ==0)
{
EditorUtility.DisplayDialog("Error","No resource selection in project panel","OK");
return;
}
UnityEngine.Object[] selectedAsset = Selection.objects;
string savePath = EditorUtility.SaveFolderPanel("Save AssetBundle", Application.dataPath + "/StreamingAssets/","");
if(savePath!="")
{
string assetPath = Application.dataPath + "/StreamingAssets/";
if(!Directory.Exists(assetPath)){
Directory.CreateDirectory(assetPath);
}
#if UNITY_IPHONE
BuildTarget bt = BuildTarget.iOS;
#elif UNITY_ANDROID
BuildTarget bt = BuildTarget.Android;
#else
BuildTarget bt = BuildTarget.StandaloneOSXUniversal;
#endif
BuildAssetBundleOptions bo = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle;
if(compressed == false)
{
bo = bo | BuildAssetBundleOptions.UncompressedAssetBundle;
}
AssetBundleBuild[] abb = new AssetBundleBuild[selectedAsset.Length];
int counter = 0;
foreach(UnityEngine.Object obj in selectedAsset)
{
abb[counter] = new AssetBundleBuild();
abb[counter].assetBundleName = obj.name;
abb[counter].assetNames = new string[1];
abb[counter].assetNames[0] = AssetDatabase.GetAssetPath(obj);
Debug.Log(AssetDatabase.GetAssetPath(obj));
Debug.Log(abb[counter].assetNames[0]);
counter++;
}
BuildPipeline.BuildAssetBundles(savePath,abb,bo,bt);
AssetDatabase.Refresh();
}
}
}
加载AssetBundle的两种方法,里面的一些路径需要根据自己的文件路径来写。特别要注意各平台的路径的不同
using UnityEngine;
using System.Collections;
public class LoadAssetBundel : MonoBehaviour {
public string mainfestname = "Prefab";
public string assetbundlename = "test";
// Use this for initialization
public string path;
private void Start()
{
#if UNITY_EDITOR || UNITY_IOS
path = "file://" + Application.streamingAssetsPath;
#elif UNITY_ANDROID
path = Application.streamingAssetsPath;
#endif
}
void OnGUI()
{
if(GUILayout.Button("LoadAssetBundle"))
{
// 加载总的manifest的assetbundle
AssetBundle mainfestbunde = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/Prefab");
//AssetBundleManifest
AssetBundleManifest mainfest = (AssetBundleManifest)mainfestbunde.LoadAsset("AssetBundleManifest");
//处理依赖关系
string[] dependentAssetBundles = mainfest.GetAllDependencies("sphere.assetbundle");
//加载所有的依赖文件
AssetBundle[] abs = new AssetBundle[dependentAssetBundles.Length];
for(int i = 0; i< abs.Length; i++)
{
abs[i] = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/" + dependentAssetBundles[i]);
}
//加载需要的文件
AssetBundle spherebundle = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/sphere.assetbundle");
GameObject Sphere = (GameObject)spherebundle.LoadAsset("Sphere");
Instantiate(cube);
mainfestbunde.Unload(false);
spherebundle.Unload(false);
}
if(GUILayout.Button("LoadFromCacheOrDownload"))
{
StartCoroutine(LoadFromCacheOrDownload());
}
}
IEnumerator LoadFromCacheOrDownload()
{
WWW www = WWW.LoadFromCacheOrDownload(path + "/Prefab/Prefab", 0);
yield return www;
AssetBundle mainfestbundle = www.assetBundle;
AssetBundleManifest manifest = (AssetBundleManifest)mainfestbundle.LoadAsset("AssetBundleManifest");
mainfestbundle.Unload(false);
string[] dependAssetbundle = manifest.GetAllDependencies("human.assetbundle");
//加载所有的依赖文件
AssetBundle[] abs = new AssetBundle[dependAssetbundle.Length];
for (int i = 0; i < abs.Length; i++)
{
WWW www2 = WWW.LoadFromCacheOrDownload(path + "/Prefab/" + dependAssetbundle[i], 0);
yield return www2;
abs[i] = www2.assetBundle;
}
WWW www3 = WWW.LoadFromCacheOrDownload(path + "/Prefab/human.assetbundle", 0);
yield return www3;
AssetBundle human = www3.assetBundle;
GameObject obj = (GameObject)Instantiate(human.LoadAsset("human"));
human.Unload(false);
}
}
本文介绍了一个Unity中用于单独打包Assetbundle的方法,并演示了两种不同的Assetbundle加载方式。该方法允许开发者针对选中的资源进行独立打包,便于管理和分类。此外,还提供了具体的代码实现,帮助读者理解如何实现这一功能。
2859

被折叠的 条评论
为什么被折叠?



