zoukankan      html  css  js  c++  java
  • 跟我从零基础学习Unity3D开发--资源打包篇(AssetBundle)

    好久没更新了,一直在加班敢项目进度。这里和关注我的博客的童鞋表示一下歉意!这里有我录的Unity3D从零开始的视频教程大家可以关注一下:http://www.imooc.com/view/555  视频会陆续的更新,需要讲什么也可以留言给我。

    之前在项目中一直用的是别人以前的写的打包代码,后来项目需要需要修改以前的代码把打包的代码过了一遍,今天把自己遇到的问题和打包思路在这路写下来。

    在untiy支援加载方面分为两个种类:

    第一类:Resources方式加载-------我们不用打包资源,放在项目中的Resources目录下就能在代码中直接加载出来。

    第二类:assetBundle方式加载-----需要我们打包资源,把每个资源打包成一个个Bundle,然后加载。

    两类资源加载方式在游戏项目中都会用,Resources加载的资源一般是要求非常及时的,assetBundle加载相对要慢一些因为需要把assetBundle资源解压出来。assetBundle也是方便项目中做热更新(在游戏对外发布的过程中,我们需要替换一些资源,通过热更新的方式把资源更新出去供当前的客户端下载替换)。Assetbundle是官方推荐使用的打包方式,AssetbundleUnity(Pro)提供的资源打包策略。

    打包思路:

    *高能

    • a、打包的资源一定要放在StreamingAssets目录下因为在移动平台下只能访问这个目录
    • b、AssetBundle的保存后缀名可以是assetbundle或者unity3d
    • c、BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer

    1、准备好我们需要打包的资源

    2、加载需要打包的资源

    3、获取当前对象列表的所有依赖项

    4、设置打包编译选项

    5、开始打包

    重要函数解释:

    Selection.objects   ---------------------------------------------------  这个方法是用于判断在Asset目录中返回选中了哪些资源文件
    AssetDatabase.GetAssetpath()-------------------------------------- 获取的是工程文件相对于Asset的路径
    AssetDatabase.LoadMainAssetAtPath()----------------------------  加载指定路径下的资源
    EditorUtility.CollectDependencies()--------------------------------- 获取当前对象列表的所有依赖项
    BuildAssetBundleOptions ------------------------------------------- 打包编译选项
    BuildPipeline.BuildAssetBundle() ---------------------------------- 打包函数
    Path.GetFileNameWithoutExtension() ----------------------------- 获取路径下的不要扩展名的文件名
    Path.ChangeExtension() --------------------------------------------  改变路径的文件后缀名
    EditorUserBuildSettings.activeBuildTarget -----------------------  判断当前编译平台
     
     
    下面列出打包实例代码!
    实例:
     
     1 using UnityEngine;
     2 using System.Collections;
     3 using UnityEditor;
     4 using System.IO;
     5 using System.Collections.Generic;
     6 public class AssetBoundExport :EditorWindow {
     7 
     8     [MenuItem("Window/资源打包")]
     9     static void Init()
    10     {
    11         EditorWindow.GetWindow(typeof(AssetBoundExport));
    12     }
    13 
    14     void OnGUI()
    15     {
    16         /*
    17         *ui1.prefab  和ui2.prefab 是两个预制件,里面都包含loading.png这张图
    18         *这个方法是讲依赖打包的
    19         *通一个资源用在多个预制件中的时候我们就采用这种依赖打包更小
    20         */
    21         if (GUILayout.Button("打包UI对象", GUILayout.Width(100)))
    22         {
    23 
    24             BuildPipeline.PushAssetDependencies();
    25 
    26             Object baseTexture = AssetDatabase.LoadMainAssetAtPath("Assets/Texture/loading.png");
    27             ExportBundle<Texture2D>(AssetDatabase.GetAssetPath(baseTexture));
    28 
    29             BuildPipeline.PushAssetDependencies();
    30             Object p1 = AssetDatabase.LoadMainAssetAtPath("Assets/Prefab/ui1.prefab");
    31             ExportBundle<GameObject>(AssetDatabase.GetAssetPath(p1));
    32             BuildPipeline.PopAssetDependencies();
    33 
    34             BuildPipeline.PushAssetDependencies();
    35             Object p2 = AssetDatabase.LoadMainAssetAtPath("Assets/Prefab/ui2.prefab");
    36             ExportBundle<GameObject>(AssetDatabase.GetAssetPath(p2));
    37             BuildPipeline.PopAssetDependencies();
    38 
    39             BuildPipeline.PopAssetDependencies();
    40             AssetDatabase.Refresh();
    41         }
    42     
    43     }
    44     /// <summary>
    45     /// 这个方法是用于单独给某类资源打包(这个方法实例是打图片的)
    46     /// 应用场景:在实际项目中我们每次不一定是要全部打包,更多的时候是只需要打某一个资源
    47     /// </summary>
    48     [MenuItem("Assets/打包/Textrue")]
    49     public static void PackageTextureAssetBundle()
    50     {
    51         Object[] _Object = Selection.objects;
    52         for (int i = 0; i < _Object.Length; i++)
    53         {
    54             ExportBundle<Texture2D>(AssetDatabase.GetAssetPath(_Object[i]));
    55         }   
    56     }
    57     
    58     /// <summary>
    59     /// 打包通用类
    60     /// </summary>
    61     /// <typeparam name="T"></typeparam>
    62     /// <param name="path"></param>
    63     private static void ExportBundle<T>(string path) where T:Object
    64     {
    65         T assetObject = AssetDatabase.LoadMainAssetAtPath(path) as T;
    66         if (assetObject == null)
    67         {
    68             Debug.Log("can't load "+path);
    69             return;
    70         }
    71 
    72         string _path = Application.dataPath + "/StreamingAssets/PC/" + Path.GetFileNameWithoutExtension(path);
    73         _path = Path.ChangeExtension(_path, ".unity3d");
    74 
    75         List<Object> obejectList = new List<Object>();
    76         Object[] tempobject = new Object[1];
    77         tempobject[0] = assetObject;
    78         Object[] kDepens = EditorUtility.CollectDependencies(tempobject);
    79         for (int i = 0; i < kDepens.Length; i++)
    80         {
    81             obejectList.Add(kDepens[i]);   
    82         }
    83   
    84         BuildAssetBundleOptions babo = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
    85 | BuildAssetBundleOptions.DeterministicAssetBundle;
    86       bool istru=   BuildPipeline.BuildAssetBundle(assetObject, obejectList.ToArray(),_path, babo, EditorUserBuildSettings.activeBuildTarget);
    87       if (istru == true)
    88           Debug.Log("打包成功");
    89       else
    90           Debug.Log("打包失败");
    91     }
    92 }
  • 相关阅读:
    Tomcat && Servlet
    List,Set,Collections工具类
    多表查询
    常用的API--集合
    msmpeng.exe阻止移动硬盘弹出
    接口400错误解析
    JDBC/Mybatis连接数据库报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    tomcat启动报错:A child container failed during start
    PAT 1019 数字黑洞
    PAT 1017 A除以B
  • 原文地址:https://www.cnblogs.com/gaojiangshan/p/5032626.html
Copyright © 2011-2022 走看看