之前已经有几篇文章写打包AssetBundle,但毕竟没有实际在项目中写过都写的比较浅。
刚好最近项目更新Unity5.5.2就顺便由我来更新ui打包流程
这里就把这次的经验写一下
这里还是稍微解释一下打包的基本目的:
打包ui就是把你做的界面打包出来成assetbundle包,讲道理你就把每个界面打成bundle包在游戏中你就可以直接加载来用,但是这样子的话你的每个bundle包就会非常的大,为什么呢,是因为这样子每个界面的bundle包里都包含这个界面用到的字体,贴图atlas,texture,shader还可以有你自己使用的动态组件。
这样子你的同一份资源都复制了很多份出来。
当然不能这样子做
所以我们就需要把这些atlas,font给剥离开来独立打包
不过新的打包方式让我们不用再手动剥离开来这些东西,也就是没有了剥离之后要重新引用的烦恼。
所要做的事就是把需要打包的东西改一改它的AssetBudnle名再一起build就好了
总的来说,就是把一个ui界面的下的atlas,font,texture,component记录下来,
通过修改这个ui.prefab,atlas,font,texture,compoent的预制件的bundle名,最后调一下打包函数就可以了
(有的项目texutre和shader是有自己写的管理类来加载,需要把引用置空的,这时候就需要实例ui.prefab,并把引用赋空,拷贝一份到另外一个文件夹中)
上一篇ui打包文章
http://blog.youkuaiyun.com/chrisfxs/article/details/55046339
而这次的流程是
1.清理数据,建立文件夹
2.遍历所有文件夹下的ui界面prefab
3.遍历所有prefab下的compoent(动态组件)
4.找出所有的资源(如atlas,font)用个list记录下来
5.修改这些资源和这个ui.prefab的assetbundle名
6.build
补充一点!
游戏加载assetbundle的时候要先加载atlas,font这些资源,最后加载ui资源
这样才能保证引用没有丢失
下面是代码
//#if UNITY_5_MODE
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System.IO;
public class BuildAssetbundle
{
public static string m_destFileName = "Assetbundle";
#if UNITY_ANDROID
public static string PlatformExtension = ".android";
public static string Extension = ".x.android";
public static BuildTarget Target = BuildTarget.Android;
public static string ASSETBUNDLE_PATH = Application.dataPath + "/../AndroidResources/StreamingAssets";
public static string FULL_ASSETBUNDLE_PATH = ASSETBUNDLE_PATH + "/" + m_destFileName;
#elif UNITY_IOS
public static string PlatformExtension = ".ios";
public static string Extension = ".x.ios";
public static BuildTarget Target = BuildTarget.iOS;
public static string ASSETBUNDLE_PATH = Application.dataPath + "/../IosResources/StreamingAssets";
#endif
#if BUILD_UI
//需要打包的资源
public static List<UIFont> fontList = new List<UIFont>();
public static List<UIAtlas> atlasList = new List<UIAtlas>();
public static List<GameObject> componentList = new List<GameObject>();
public static List<GameObject> panelList = new List<GameObject>();
public static void BuildOnePanel()
{
ClearAllBundleName();
CleanTempData();
GameObject[] selectGameObjects = Selection.gameObjects;
UnityEngine.Object selectFloder = Selection.activeObject;
if (selectGameObjects != null && selectGameObjects.Length != 0)
{
foreach (GameObject selGb in selectGameObjects)
{
string selGbPath = AssetDatabase.GetAssetPath(selGb);
if (selGbPath.StartsWith(BuildUtils.PANEL_ASSET_PATH))
{
if (selGbPath.EndsWith(BuildUtils.PREFAB_SUFFIX))
{
GameObject uiInstance = PrefabUtility.InstantiatePrefab(selGb) as GameObject;
CheckComponent(uiInstance);
FindRefrence(uiInstance.transform);
string prefabPath = BuildUtils.TEMP_PANEL_PREFAB_PATH + "/" + uiInstance.name + BuildUtils.PREFAB_SUFFIX;
PrefabUtility.CreatePrefab(prefabPath, uiInstance);
GameObject prefabAsset = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
panelList.Add(prefabAsset);
UnityEngine.Object.DestroyImmediate(uiInstance);
}
else
{
Debug.LogWarning("选择的资源 " + selGb.name + " 不是界面Prefab!");
}
}
else
{
Debug.LogWarning("只能打包放在 "