ABPacket.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //这是原码
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. public class ABPacket : Editor
  9. {
  10. //Ab包目标文件目录
  11. public static string path = Application.dataPath + "/Rec/ui/panel/";
  12. //public static string path = Application.dataPath + "/Rec/ui/texture/";
  13. //public static string path = Application.dataPath + "/Rec/ui/Material/";
  14. //Ab包出包文件目录
  15. public static string pathExit = Application.streamingAssetsPath;
  16. public static string pathPxit = Application.persistentDataPath;
  17. //Ab包目标文件集合
  18. private static List<AssetBundleBuild> abs = new List<AssetBundleBuild>();
  19. [MenuItem("ABmanager/打包Windows64")]
  20. static void BuildABs()
  21. {
  22. BuildTarget target = BuildTarget.StandaloneWindows64;
  23. PackAB(target);
  24. }
  25. [MenuItem("ABmanager/打包安卓")]
  26. static void BuildABAnzhuo()
  27. {
  28. BuildTarget target = BuildTarget.Android;
  29. PackAB(target);
  30. }
  31. [MenuItem("ABmanager/打包ios")]
  32. static void BuildABIos()
  33. {
  34. BuildTarget target = BuildTarget.iOS;
  35. PackAB(target);
  36. }
  37. //打包具体实现
  38. private static void PackAB(BuildTarget buildTarget)
  39. {
  40. FABPacket();
  41. BuildPipeline.BuildAssetBundles(pathExit, abs.ToArray(), BuildAssetBundleOptions.None, buildTarget);
  42. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  43. }
  44. static void FABPacket()
  45. {
  46. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  47. DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
  48. //每个目录打一个AB包
  49. for (int i = 0; i < directoryInfos.Length; i++)
  50. {
  51. List<string> prefabs = new List<string>();
  52. FileInfo[] fileInfos = directoryInfos[i].GetFiles("*", SearchOption.AllDirectories);
  53. for (int j = 0; j < fileInfos.Length; j++)
  54. {
  55. if (fileInfos[j].Name.EndsWith(".meta"))
  56. {
  57. continue;
  58. }
  59. prefabs.Add(fileInfos[j].FullName.Replace("\\", "/"));
  60. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  61. }
  62. AssetBundleBuild prefabAB = new AssetBundleBuild
  63. {
  64. assetBundleName = directoryInfos[i].Name,
  65. assetNames = prefabs.Select(s => ToSelectAssetPath(s)).ToArray(),
  66. };
  67. abs.Add(prefabAB);
  68. }
  69. }
  70. private static string ToSelectAssetPath(string s)
  71. {
  72. string path = s.Substring(s.IndexOf("Assets/"));
  73. Debug.Log(path);
  74. return path;
  75. }
  76. }