ABPackerInfoManifestWindow.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XGame.Framework.i18n;
  7. namespace XGame.Editor.Build.AssetBundles
  8. {
  9. public class ABPackerInfoManifestWindow
  10. {
  11. private ABPackerInfoManifest manifest;
  12. private readonly bool isInspector;
  13. private GUIContent _shaderContent;
  14. private ABPackerReorderableList<ABPackerInfoItem, ABPackerInfo> _abPackerList;
  15. private ABPackerReorderableList<ABGroupInfoItem, ABGroupInfo> _abGroupList;
  16. public ABPackerInfoManifestWindow(ABPackerInfoManifest manifest, bool isInspector)
  17. {
  18. this.manifest = manifest;
  19. this.isInspector = isInspector;
  20. _shaderContent = new GUIContent("是否合并Shader", "将*.shadervariants文件和其所依赖的shader一起打在一个包内");
  21. var apckerToolTip = "打包分类 \r\nTopDirectoryOnly:默认方式,当前目录的资源(不包含子目录)打一个包 \r\nAllDirectories:当前目录及子目录的资源打一个包 \r\nSingleAsset:单个资源打包";
  22. _abPackerList = new ABPackerReorderableList<ABPackerInfoItem, ABPackerInfo>(manifest.PackerInfos, "Assetbundle打包配置", apckerToolTip)
  23. {
  24. IsFoldout = true
  25. };
  26. var groupToolTip = "设置分组的资源,Assetbundle会生成在指定分组文件夹下: StreamAssets/{Platform}/AssetBundles/{GroupName}/ ";
  27. _abGroupList = new ABPackerReorderableList<ABGroupInfoItem, ABGroupInfo>(manifest.GroupInfos, "Assetbundle分组配置", groupToolTip)
  28. {
  29. IsFoldout = true
  30. };
  31. }
  32. public void OnGUI()
  33. {
  34. EditorGUILayout.BeginVertical();
  35. manifest.bundleNameMode = (AssetBundleNameMode)EditorGUILayout.EnumPopup("Bundle命名模式", manifest.bundleNameMode);
  36. manifest.isMergeShader = EditorGUILayout.Toggle(_shaderContent, manifest.isMergeShader);
  37. _abPackerList.OnGUI();
  38. _abGroupList.OnGUI();
  39. DrawButtons();
  40. DrawMsg();
  41. EditorGUILayout.EndVertical();
  42. }
  43. private void DrawButtons()
  44. {
  45. var apply = GUILayout.Button("Apply");
  46. if (apply)
  47. {
  48. //刷新
  49. OnApplyBtn();
  50. }
  51. }
  52. private void DrawMsg()
  53. {
  54. GUILayout.Space(20);
  55. EditorGUILayout.HelpBox("友情提示:如有修改请及时上传同步 Manifest 文件,否则后果自负。", MessageType.Warning);
  56. //GUI.color = Color.red;
  57. //GUILayout.TextArea("友情提示:如有修改请及时上传同步 Manifest 文件,否则后果自负。", GUI.skin.textArea);
  58. //GUI.color = Color.white;
  59. GUILayout.Space(5);
  60. EditorGUI.BeginDisabledGroup(true);
  61. EditorGUILayout.ObjectField("Manifest", manifest, typeof(Object), false);
  62. EditorGUI.EndDisabledGroup();
  63. }
  64. private void OnApplyBtn()
  65. {
  66. ApplyPackerInfos();
  67. ApplyGroupInfos();
  68. manifest.Save();
  69. Debug.Log("ABPackerInfoManifest apply completed.");
  70. }
  71. private void ApplyPackerInfos()
  72. {
  73. var guids = new HashSet<string>();
  74. var packerInfos = new List<ABPackerInfo>();
  75. foreach (var item in manifest.PackerInfos)
  76. {
  77. var guid = item.GUID;
  78. if (string.IsNullOrEmpty(guid))
  79. {
  80. Debug.LogWarning($"AssetbundlePacker 资源为空. {item}");
  81. }
  82. else if (guids.Contains(guid))
  83. {
  84. Debug.LogError($"AssetbundlePacker 资源重复. Path: {AssetDatabase.GUIDToAssetPath(guid)}");
  85. }
  86. else
  87. {
  88. guids.Add(guid);
  89. packerInfos.Add(item);
  90. }
  91. }
  92. manifest.PackerInfos.Clear();
  93. manifest.PackerInfos.AddRange(packerInfos);
  94. }
  95. private void ApplyGroupInfos()
  96. {
  97. var datas = _abGroupList.GetItemDatas();
  98. var guids = new HashSet<string>();
  99. var groupMap = new Dictionary<string, ABGroupInfo>();
  100. foreach (var data in datas)
  101. {
  102. var groupName = data.groupName.Trim();
  103. if (string.IsNullOrEmpty(groupName) || Asset.FileUtil.HasChinese(groupName))
  104. {
  105. Debug.LogError($"AssetbundleGroup 名字为空或者包含中文字符! GroupName:{groupName}");
  106. continue;
  107. }
  108. if (LanguageUtils.IsLanguage(groupName))
  109. {
  110. Debug.LogError($"AssetbundleGroup 名字不能为多语言枚举. GroupName:{groupName}");
  111. continue;
  112. }
  113. if (groupMap.ContainsKey(groupName))
  114. {
  115. Debug.LogError($"AssetbundleGroup 名字重复. GroupName:{groupName}");
  116. continue;
  117. }
  118. for(var i = data.guidList.Count - 1; i >= 0; i--)
  119. {
  120. var guid = data.guidList[i];
  121. if (string.IsNullOrEmpty(guid))
  122. {
  123. data.guidList.RemoveAt(i);
  124. }
  125. else if (guids.Contains(guid))
  126. {
  127. data.guidList.RemoveAt(i);
  128. Debug.LogError($"AssetbundleGroup 资源重复. Path:{AssetDatabase.GUIDToAssetPath(guid)}");
  129. }
  130. else
  131. {
  132. guids.Add(guid);
  133. }
  134. }
  135. if (data.guidList.Count < 1)
  136. {
  137. Debug.LogError($"AssetbundleGroup 资源列表为空! GroupName:{groupName}");
  138. continue;
  139. }
  140. data.groupName = groupName;
  141. groupMap.Add(groupName, data);
  142. }
  143. manifest.GroupInfos.Clear();
  144. foreach(var item in groupMap)
  145. {
  146. manifest.GroupInfos.Add(item.Value);
  147. }
  148. }
  149. public void Close()
  150. {
  151. (_abPackerList as System.IDisposable)?.Dispose();
  152. _abPackerList = null;
  153. (_abGroupList as System.IDisposable)?.Dispose();
  154. _abGroupList = null;
  155. manifest = null;
  156. }
  157. }
  158. }