赞
踩
1. Assets下新建GameRes文件夹
2. 新建一个UI预设
public class ABTool : Editor
{
[MenuItem("GameTool/BuildAB包")]
public static void BuildAB()
{
string targetPath = Application.streamingAssetsPath + "/ABRes";
BuildUtils.BuildGameResBundle(targetPath);
Debug.Log("BuildAssetBundle Done");
}
}
public class BuildUtils { /// <summary> /// 打包游戏资源AssetBundle /// </summary> public static void BuildGameResBundle(string targetPath) { Hashtable tb = new Hashtable(); tb["uiprefabs.bundle"] = "GameRes/AUI"; AssetBundleBuild[] buildArray = MakeAssetBundleBuildArray(tb); BuildBundles(buildArray, targetPath); } /// <summary> /// 根据哈希表构建AssetBundleBuild列表 /// </summary> /// <param name="tb">哈希表,key为assetBundleName,value为目录</param> /// <returns></returns> public static AssetBundleBuild[] MakeAssetBundleBuildArray(Hashtable tb) { AssetBundleBuild[] buildArray = new AssetBundleBuild[tb.Count]; int index = 0; foreach (string key in tb.Keys) { buildArray[index].assetBundleName = key; List<string> fileList = new List<string>(); fileList = GetFiles(Application.dataPath + "/" + tb[key], true); buildArray[index].assetNames = fileList.ToArray(); ++index; } return buildArray; } /// <summary> /// 递归遍历获取目标目录中的所有文件 /// </summary> /// <param name="sourceDir">目标目录</param> /// <param name="splitAssetPath">是否要切割目录,以Assets目录为根</param> public static List<string> GetFiles(string sourceDir, bool splitAssetPath) { List<string> fileList = new List<string>(); string[] fs = Directory.GetFiles(sourceDir); string[] ds = Directory.GetDirectories(sourceDir); for (int i = 0, len = fs.Length; i < len; ++i) { var index = splitAssetPath ? fs[i].IndexOf("Assets") : 0; fileList.Add(fs[i].Substring(index)); } for (int i = 0, len = ds.Length; i < len; ++i) { fileList.AddRange(GetFiles(ds[i], splitAssetPath)); } return fileList; } /// <summary> /// 打AssetBundle /// </summary> /// <param name="buildArray">AssetBundleBuild列表</param> public static void BuildBundles(AssetBundleBuild[] buildArray, string targetPath) { if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } BuildPipeline.BuildAssetBundles(targetPath, buildArray, BuildAssetBundleOptions.ChunkBasedCompression, GetBuildTarget()); } /// <summary> /// 获取当前平台 /// </summary> public static BuildTarget GetBuildTarget() { #if UNITY_STANDALONE return BuildTarget.StandaloneWindows; #elif UNITY_ANDROID return BuildTarget.Android; #else return BuildTarget.iOS; #endif } }
点击工具栏上的GameTool/BuildAB包开始自动打包
生成AB资源
注意事项
/// AssetBundle资源字典 Dictionary<string, AssetBundle> dic_abres; /// <summary> /// 加载资源包 /// </summary> void LoadABRes() { dic_abres = new Dictionary<string, AssetBundle>(); string[] res = { "uiprefabs"}; for (int i = 0; i < res.Length; i++) { StartCoroutine(LoadAb(res[i])); } } IEnumerator LoadAb(string name) { AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/ABRes/" + name + ".bundle"); yield return ab; if (ab.assetBundle != null && !dic_abres.ContainsKey(name)) dic_abres.Add(name, ab.assetBundle); } /// <summary> /// 加载Assets资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Abname"></param> /// <param name="Resname"></param> /// <returns></returns> public T LoadAssets<T>(string Abname, string Resname) where T : UnityEngine.Object { T obj = null; //Debug.Log("Type: " + typeof(T).ToString() + "/" + "Res: " + Resname); if (!dic_abres.ContainsKey(Abname)) { Debug.LogError("AB资源不存在"); } else { if (typeof(T) == typeof(GameObject)) { /// 如果是GameObject就返回一个Clone的Obj obj = Instantiate(dic_abres[Abname].LoadAsset<T>(Resname)); } else { obj = dic_abres[Abname].LoadAsset<T>(Resname); } } return obj; } private void Awake() { LoadABRes(); } void Start() { GameObject go = LoadAssets<GameObject>("loadui", name); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。