【unity系统模块开发】Unity5.5.2UI打包AssetBundle

本文详细介绍了在Unity5.5.2中进行UI AssetBundle打包的流程,包括剥离字体、贴图等资源,修改Prefab的AssetBundle名,并提供了打包的步骤和代码实现。强调了游戏加载时应先加载atlas、font,再加载UI资源,以避免引用丢失。
摘要由CSDN通过智能技术生成

之前已经有几篇文章写打包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.csdn.net/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("只能打包放在 "
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值