zoukankan      html  css  js  c++  java
  • AsssetBunlder打包

    unity3d,资源过多的话。可以压缩成一个资源包。加载出来后。可以解压。找到自己需要的资源

    就想.net网站。很多图标都是放一个大图片上。而不是一个图标就是一个图片

    因为是在项目编辑时候给资源打包。并不是项目运行。所以要用到Editor。untiy编辑类,脚本放到Editor文件夹中

     1 using UnityEngine;
     2 using System.Collections;
     3 using UnityEditor;
     4 public class AsssetBunlder
     5 {
     6     [MenuItem("Custom/SayHello")]
     7     static void SayHello()
     8     {
     9         Debug.Log("hello");
    10     }
    11 
    12     [MenuItem("Custom/CreateBundle")]
    13     static void CreateBoundle()
    14     {
    15         /*
    16          * Selection类,在编辑状态下,获取选中的对象引用
    17          */
    18         Object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);
    19 
    20         string path = string.Empty;
    21 
    22         foreach (Object ojb in objs)
    23         {
    24             path = Application.streamingAssetsPath + "/" + ojb.name + ".unity3d";
    25             //创建AssetBunlder
    26             BuildPipeline.BuildAssetBundle(ojb, null, path);
    27 
    28             //BuildTarget.Android :不同平台打包路径不同
    29             //BuildPipeline.BuildAssetBundle(ojb, null, path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
    30         }
    31         //自动刷新
    32         AssetDatabase.Refresh();
    33     }
    34 
    35 
    36 
    37     [MenuItem("Custom/CreateAllBundle")]
    38     static void CreateAllBoundle()
    39     {
    40         /*
    41          * Selection类,在编辑状态下,获取选中的对象引用
    42          */
    43         Object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);
    44 
    45         string path = string.Empty;
    46         path = Application.streamingAssetsPath + "/All.assetbundle";
    47         //创建AssetBunlder
    48         BuildPipeline.BuildAssetBundle(null, objs, path);
    49 
    50         //自动刷新
    51         AssetDatabase.Refresh();
    52     }
    53 
    54 
    55     //场景打包
    56     [MenuItem("Custom/StreamBundle")]
    57     static void StreamBundleScene()
    58     {
    59         //要压缩的场景位置
    60         string[] leves = { "Assets/Scene/Demo2.unity" };
    61 
    62         //压缩后。资源保存的位置
    63         string path = Application.streamingAssetsPath + @"/Test.assetsbundle";
    64 
    65         /*
    66          BuildAdditionalStreamedScenes:以流的方式打包场景
    67          * StandaloneWindows64:在window64位电脑下打包
    68          */
    69         BuildPipeline.BuildPlayer(leves, path, BuildTarget.StandaloneWindows64, BuildOptions.BuildAdditionalStreamedScenes);
    70 
    71 
    72 
    73     }
    74 
    75 }

    单击菜单。会出现你刚写的菜单选项

    单击不同的选项就可以根据不同的资源打包了

    当读取资源前必须解压

      1 using UnityEngine;
      2 using System.Collections;
      3 
      4 public class AssetBoundleContorller : MonoBehaviour
      5 {
      6 
      7 
      8     /*
      9      * streamingAssetsPath路径。在pc和Andoid,phone中是不同的
     10      * 所以要用预处理(#if #end if)
     11      */
     12 
     13     //    public static string RUL;
     14     //#if UNIYT_ANDROID
     15     //    "jar:file://"+ Application.dataPath+@"/assets";
     16     //#elif UNITY_IPHONE
     17     //    "";
     18     //#endif
     19 
     20 
     21     //#if UNITY_EDITOR
     22     //    string filepath = Application.dataPath + "/StreamingAssets" + "/my.xml";
     23     //#elif UNITY_IPHONE
     24     // string filepath = Application.dataPath +"/Raw"+"/my.xml";
     25     //#elif UNITY_ANDROID
     26     // string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
     27     //#else
     28 
     29     //#endif
     30 
     31     //    public static string RUL =
     32     //#if UNITY_IPHONE
     33     //     string filepath = Application.dataPath +"/assets";
     34     //#elif UNITY_ANDROID
     35     //     "jar:file://" + Application.dataPath + "/assets/";
     36     //#else
     37     // Application.streamingAssetsPath + @"/Sphere.unity3d";
     38     //#endif
     39 
     40 
     41 
     42     string filepath = string.Empty;
     43     void Awake()
     44     {
     45         #if UNITY_ANDROID
     46             filepath = "jar:file://" + Application.dataPath +@"/assets";
     47         #endif
     48 
     49         #if UNITY_IPHONE
     50             filepath = "Application.dataPath +@"/Raw";
     51         #endif
     52 
     53         #if UNITY_STANDALONE_WIN
     54             filepath = @"file://" + Application.streamingAssetsPath + @"/assets";
     55         #endif
     56     }
     57 
     58 
     59     // Use this for initialization
     60     void Start()
     61     {
     62         //1:拿到我们的Assetboundle
     63 
     64         //string path = @"file://" + Application.streamingAssetsPath + "/Sphere.unity3d";
     65 
     66         //StartCoroutine(GetAssetboundle(path));
     67 
     68         string path = @"file://" + Application.streamingAssetsPath + "/all.assetbundle";
     69         StartCoroutine(GetAssetboundle(path, "Sphere"));
     70     }
     71 
     72     /// <summary>
     73     /// 加载一个资源包
     74     /// </summary>
     75     /// <param name="path"></param>
     76     /// <returns></returns>
     77     IEnumerator GetAssetboundle(string path)
     78     {
     79         //没有缓存
     80         WWW www = new WWW(path);
     81 
     82 
     83         //缓存机制,缓存在 硬盘中  5代码版本号,每次缓存都会加1
     84         WWW www1 = WWW.LoadFromCacheOrDownload(path, 5);
     85 
     86         yield return www;
     87         //2:解压并实例化出来
     88         //www.assetBundl返回的是加载AssetBundle压缩文件
     89         //www.assetBundle.mainAsset:解压获取到物体
     90         Instantiate(www.assetBundle.mainAsset);
     91 
     92         //销毁assetBundle的内存
     93         www.assetBundle.Unload(false);
     94     }
     95 
     96 
     97 
     98     /// <summary>
     99     /// 很多资源打包在一起,根据名称找到相应的资源
    100     /// </summary>
    101     /// <param name="path"></param>
    102     /// <param name="name"></param>
    103     /// <returns></returns>
    104     IEnumerator GetAssetboundle(string path, string name)
    105     {
    106         WWW www = new WWW(path);
    107         yield return www;
    108 
    109         //查看资源里面所以的子资源
    110         //www.assetBundle.LoadAll;
    111 
    112 
    113         //Load 根据名字加载资源
    114         Instantiate(www.assetBundle.Load(name));
    115     }
    116 
    117 
    118     // Update is called once per frame
    119     void Update()
    120     {
    121         //加载场景
    122         if (Input.GetMouseButtonDown(0))
    123         {
    124             string path = @"file://" + Application.streamingAssetsPath + "/Test.assetsbundle";
    125             StartCoroutine(LoadScene(path, "Demo2"));
    126         }
    127     }
    128 
    129     /// <summary>
    130     /// 
    131     /// </summary>
    132     /// <param name="path"></param>
    133     /// <param name="name">打包场景前的scene名称</param>
    134     /// <returns></returns>
    135     IEnumerator LoadScene(string path, string name)
    136     {
    137         WWW www = new WWW(path);
    138         yield return www;
    139 
    140         //使用之前必须要解压assetbundle
    141         AssetBundle bundle = www.assetBundle;
    142 
    143         //加载场景
    144         Application.LoadLevel(name);
    145 
    146         www.assetBundle.Unload(false);
    147 
    148     }
    149 }

     对于不同平台打包路径是不同的。具体看

    http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    http://www.taidous.com/article-417-1.html

    http://unity3d.9tech.cn/jiaocheng/2013/0621/32651.html

  • 相关阅读:
    根据第三方提供的wsdl报文(axis2开发),进行的webservice应用的开发实例
    调用axis2开发的接口遇到的问题
    使用 DJ Java Decompiler 将整个jar包反编译成源文件
    解析Myeclipse项目下的.classpath文件
    使用cxf开发webservice应用时抛出异常
    hibernate 映射 多对一
    jquery获取元素的值,获取当前对象的父对象等等
    web项目中加入struts2、spring的支持,并整合两者
    Struts2中的 配置文件
    对list集合中的对象按照对象的某一属性进行排序
  • 原文地址:https://www.cnblogs.com/nsky/p/4674314.html
Copyright © 2011-2022 走看看