您的位置:首页 > 移动开发 > Unity3D

unity编辑器扩展--AssetBundle菜单

2017-10-31 17:54 399 查看
选中预制体,点击菜单按钮打包到固定文件夹

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;

public class CreatAssetBundles{
const string PATH = "Data/asset/";
const string fileEndName = ".aseetBundle";
[MenuItem("MyTools/打包AB/打包选中的对象")]
static void CreateAssetBundlesMain()
{
Object[] selectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
if (selectedAsset.Length == 0)
{
Debug.LogError("未选中任何对象!");
return;
}

string toPath = GetAssetBundlePath();

foreach (var obj in selectedAsset)
{
string objName = obj.name;
//Debug.Log(toPath);

if (!Directory.Exists(toPath))
{
Directory.CreateDirectory(toPath);

}

string targetPath = toPath + objName.ToLower() + fileEndName;
if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, GetTargetPlatform()))
{
Debug.Log(objName + "资源打包成功");
}else
{
Debug.Log(objName + "资源打包失败");
}

}
}
[MenuItem("MyTools/打包AB/按AB包名打包所以对象")]
static void BuildleAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/Data",BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, GetTargetPlatform());
}

public static string GetAssetBundlePath()
{
BuildTarget target = GetTargetPlatform();
return Application.dataPath + PATH + target.ToString().ToLower() + "/";
}

public static BuildTarget GetTargetPlatform()
{
BuildTarget target = BuildTarget.Android;
#if UNITY_STANDALONE
target = BuildTarget.StandaloneWindows;
#elif UNITY_IPHONE
target = BuildTarget.iOS;
#elif UNITY_ANDROID
target = BuildTarget.Android;
#endif

return target;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: