Unity3d基础:自己动手写打包AssetBundle工具

博客展示了一个名为Packager的类,用于多平台资源打包。创建Editor文件夹并放入该类,可实现iPhone、安卓和Windows平台的打包。定义了不同平台的打包方法,还包含获取当前路径和打包资源的方法,会判断资源输出路径是否存在,不存在则创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 创建文件夹Editor,将这个类放到里边
  2.  
  3. public class Packager
  4. {
  5.     //打包iphone,菜单栏显示的样式,优先级
  6.     [MenuItem ("Packagers/Build iPhone", false, 1)]
  7.     public static void BuileIPhone ()
  8.     { 
  9.         //打包目标类型
  10.         BuildTarget target = BuildTarget.iOS;
  11.         //打包
  12.         BuildAssetResource (target, true);
  13.     }
  14.     //打包安卓,菜单栏显示的菜单样式,优先级
  15.     [MenuItem ("Packagers/Build Android", false, 2)]
  16.     public static void BuileAndroid ()
  17.     {
  18.         //打包目标类型
  19.         BuildTarget target = BuildTarget.Android;
  20.         //打包
  21.         BuildAssetResource (target, true);
  22.     }
  23.     //打包windows平台,优先级
  24.     [MenuItem ("Packagers/Build Windows", false, 3)]
  25.     public static void BuileWindows ()
  26.     {
  27.         //打包平台目标
  28.         BuildTarget target = BuildTarget.StandaloneWindows;
  29.         //打包
  30.         BuildAssetResource (target, true);
  31.     }
  32.     /// <summary>
  33.     /// 属性 返回当前路径
  34.     /// </summary>
  35.     /// <returns>The data path.</returns>
  36.     public static string AppDataPath {
  37.         get {
  38.             //返回当前路径,转换成小写
  39.             return Application.dataPath.ToLower ();
  40.         }
  41.     }
  42.  
  43.     /// <summary>
  44.     /// 打包资源
  45.     /// </summary>
  46.     /// <param name="target">目标平台</param>
  47.     /// <param name="isWin">If set to <c>true</c> is window.</param>
  48.     public static void BuildAssetResource (BuildTarget target, bool isWin)
  49.     {
  50.         //资源输出路径
  51.         string resPath = AppDataPath + "/" + "StreamingAssets/" ;
  52.         //判断路径是否存在,不存在就创建
  53.         if (!Directory.Exists (resPath)) {
  54.             Directory.CreateDirectory (resPath);
  55.         }
  56.         //打包                                                 资源路径,压缩格式,                                    打包目标平台
  57.         BuildPipeline.BuildAssetBundles (resPath, BuildAssetBundleOptions.None, target);
  58.         AssetDatabase.Refresh ();
  59.     } 
  60. }
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值