LuaFramework:自动打包工具

1.资源的创建

注意一下命名规则,一个面板及其相关的东西都放在同一个文件夹中,如果文件夹命名为xxx,则面板预制要命名为xxxPanel


2.打包

以文件夹为单位进行打包,打包类为Packager.cs。这里要打包的东西分两种,一种为图片等资源,另一种为代码资源(即lua脚本)。对lua脚本的打包已经被框架搞好了,不需要我们考虑,我们要考虑的是对前者的打包,详细的见Packager.cs的HandleExampleBundle方法,这里我做了个小工具:

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Text;
  6. public enum SuffixEnum
  7. {
  8. Prefab,
  9. Png,
  10. Csv,
  11. Txt,
  12. }
  13. public class AddBuildMapUtility : EditorWindow {
  14. int count = 0;
  15. List< string> bundleNameList = new List< string>();
  16. List suffixList = new List();
  17. List< string> pathList = new List< string>();
  18. Vector2 scrollValue = Vector2.zero;
  19. [ MenuItem("LuaFramework/AddBuildMapUtility")]
  20. static void SetAssetBundleNameExtension()
  21. {
  22. EditorWindow.GetWindow();
  23. }
  24. void OnGUI()
  25. {
  26. EditorGUILayout.BeginHorizontal();
  27. if (GUILayout.Button( "添加一项"))
  28. {
  29. AddItem();
  30. }
  31. if (GUILayout.Button( "清除所有项"))
  32. {
  33. Clear();
  34. }
  35. if (GUILayout.Button( "读取文件(.csv)"))
  36. {
  37. Clear();
  38. string path = EditorUtility.OpenFilePanel( "", Application.dataPath, "csv");
  39. string content = File.ReadAllText(path);
  40. string[] contents = content.Split( new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);
  41. for ( int i = 0; i < contents.Length; i++)
  42. {
  43. string[] a = contents[i].Split( ',');
  44. AddItem(a[ 0], StringToEnum(a[ 1]), a[ 2]);
  45. }
  46. }
  47. if (GUILayout.Button( "保存"))
  48. {
  49. string path = EditorUtility.SaveFilePanel( "", Application.dataPath, "AssetBundleInfo", "csv");
  50. StringBuilder sb = new StringBuilder();
  51. for ( int i = 0; i < count; i++)
  52. {
  53. if ( string.IsNullOrEmpty(bundleNameList[i])) break;
  54. sb.Append(bundleNameList[i] + ",");
  55. sb.Append(EnumToString(suffixList[i]) + ",");
  56. sb.Append(pathList[i] + "\r\n");
  57. }
  58. File.WriteAllText(path, sb.ToString());
  59. AssetDatabase.Refresh();
  60. }
  61. if (GUILayout.Button( "自动填写(所有选中的)"))
  62. {
  63. int startIndex = count;
  64. for ( int i = 0; i < Selection.objects.Length; i++)
  65. {
  66. AddItem();
  67. AutoFill(startIndex, Selection.objects[i]);
  68. startIndex++;
  69. }
  70. }
  71. EditorGUILayout.EndHorizontal();
  72. EditorGUILayout.LabelField( "注意:请以文件夹为单位进行选择!!!文件夹名即为包名!!!");
  73. scrollValue = EditorGUILayout.BeginScrollView(scrollValue);
  74. for ( int i = 0; i < count; i++)
  75. {
  76. EditorGUILayout.BeginVertical();
  77. EditorGUILayout.BeginHorizontal();
  78. EditorGUILayout.LabelField(i.ToString() + "AB包名");
  79. bundleNameList[i] = EditorGUILayout.TextField( "", bundleNameList[i]);
  80. suffixList[i] = (SuffixEnum)EditorGUILayout.EnumPopup( "类型", suffixList[i]);
  81. pathList[i] = EditorGUILayout.TextField( "路径", pathList[i]);
  82. if (GUILayout.Button( "自动填写(单个)"))
  83. {
  84. AutoFill(i, Selection.objects[ 0]);
  85. }
  86. if (GUILayout.Button( "输出路径"))
  87. {
  88. Debug.Log(pathList[i]);
  89. }
  90. if (GUILayout.Button( "删除该项"))
  91. {
  92. RemoveItem(i);
  93. }
  94. EditorGUILayout.EndHorizontal();
  95. EditorGUILayout.EndVertical();
  96. }
  97. EditorGUILayout.EndScrollView();
  98. }
  99. void Clear()
  100. {
  101. count = 0;
  102. bundleNameList = new List< string>();
  103. suffixList = new List();
  104. pathList = new List< string>();
  105. }
  106. void AddItem(string bundleName = "", SuffixEnum suffix = SuffixEnum.Prefab, string path = "")
  107. {
  108. count++;
  109. bundleNameList.Add(bundleName);
  110. suffixList.Add(suffix);
  111. pathList.Add(path);
  112. }
  113. void RemoveItem(int index)
  114. {
  115. count--;
  116. bundleNameList.Remove(bundleNameList[index]);
  117. suffixList.Remove(suffixList[index]);
  118. pathList.Remove(pathList[index]);
  119. }
  120. void AutoFill(int index, Object selectedObject)
  121. {
  122. string path = AssetDatabase.GetAssetPath(selectedObject);
  123. bundleNameList[index] = path.Remove( 0, path.LastIndexOf( "/") + 1).ToLower() + LuaFramework.AppConst.ExtName;
  124. string[] files = Directory.GetFiles(path);
  125. string[] temp = files[ 0].Split( '.');
  126. suffixList[index] = StringToEnum( "*." + temp[ 1]);
  127. pathList[index] = path;
  128. }
  129. public static string EnumToString(SuffixEnum se)
  130. {
  131. switch (se)
  132. {
  133. case SuffixEnum.Prefab:
  134. return "*.prefab";
  135. case SuffixEnum.Png:
  136. return "*.png";
  137. case SuffixEnum.Csv:
  138. return "*.csv";
  139. case SuffixEnum.Txt:
  140. return "*.txt";
  141. default:
  142. return "null";
  143. }
  144. }
  145. public static SuffixEnum StringToEnum(string s)
  146. {
  147. switch (s)
  148. {
  149. case "*.prefab":
  150. return SuffixEnum.Prefab;
  151. case "*.png":
  152. return SuffixEnum.Png;
  153. case "*.csv":
  154. return SuffixEnum.Csv;
  155. case "*.txt":
  156. return SuffixEnum.Txt;
  157. default:
  158. return SuffixEnum.Prefab;
  159. }
  160. }
  161. }





然后对HandleExampleBundle这个方法进行修改:

UGUI版本:

  1. static void HandleExampleBundle()
  2. {
  3. string resPath = AppDataPath + "/" + AppConst.AssetDir + "/";
  4. if (!Directory.Exists(resPath)) Directory.CreateDirectory(resPath);
  5. //AddBuildMap("prompt" + AppConst.ExtName, "*.prefab", "Assets/LuaFramework/Examples/Builds/Prompt");
  6. //AddBuildMap("message" + AppConst.ExtName, "*.prefab", "Assets/LuaFramework/Examples/Builds/Message");
  7. //AddBuildMap("prompt_asset" + AppConst.ExtName, "*.png", "Assets/LuaFramework/Examples/Textures/Prompt");
  8. //AddBuildMap("shared_asset" + AppConst.ExtName, "*.png", "Assets/LuaFramework/Examples/Textures/Shared");
  9. string content = File.ReadAllText(Application.dataPath + "/AssetBundleInfo.csv");
  10. string[] contents = content.Split( new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);
  11. for ( int i = 0; i < contents.Length; i++)
  12. {
  13. string[] a = contents[i].Split( ',');
  14. //UnityEngine.Debug.Log(a[0]); UnityEngine.Debug.Log(a[1]); UnityEngine.Debug.Log(a[2]);
  15. AddBuildMap(a[ 0], a[ 1], a[ 2]);
  16. }
  17. }

那么,每一次打包,先点击LuaFramework/AddBuildMapUtility,准备好AB包的信息,然后点击LuaFramework/Build xxx Resource来进行打包,在window平台下,检查Assets\StreamingAssets中的资源是否正确。还有最重要的一点,每次对lua文件或者其他资源更改后,都要点击菜单栏LuaFramework/Build xxx Resource来重新打包!

你可能感兴趣的:(扩展工具)