SceneBundleCollector.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using XGame.Editor.Asset;
  4. using XGame.Framework;
  5. namespace XGame.Editor.Build.AssetBundles
  6. {
  7. public class SceneBundleCollector
  8. {
  9. public Dictionary<long, AssetBundleData> bundleBuildMap;
  10. private BundleContext _context;
  11. public SceneBundleCollector(BundleContext context)
  12. {
  13. _context = context;
  14. bundleBuildMap = new Dictionary<long, AssetBundleData>();
  15. }
  16. /// <summary>
  17. /// 收集scene的bundle信息
  18. /// </summary>
  19. /// <param name="assetBundleBuilds">bundle信息集合</param>
  20. /// <returns></returns>
  21. public bool Collect()
  22. {
  23. var sceneNames = new HashSet<string>();
  24. var xgameConfig = AddressableHelper.LoadAssetByName<XGameConfig>(Framework.Define.AssetDefine.XGAME_CONFIG_NAME);
  25. var launchScene = xgameConfig.launchSceneName.ToLower();
  26. // 遍历所有场景
  27. for (int i = 0, len = EditorBuildSettings.scenes.Length; i < len; ++i)
  28. {
  29. var editorScene = EditorBuildSettings.scenes[i];
  30. if (!editorScene.enabled)
  31. {
  32. continue;
  33. }
  34. var sceneName = System.IO.Path.GetFileNameWithoutExtension(editorScene.path).ToLower();
  35. // 判断是否有同名scene
  36. if (sceneNames.Contains(sceneName))
  37. {
  38. BuildLog.Error($"有相同名字的Scene:{sceneName}");
  39. return false;
  40. }
  41. sceneNames.Add(sceneName);
  42. // Launch场景不打assetBundle,build Setting中enable的场景打包
  43. if (sceneName != launchScene)
  44. {
  45. AddSceneBundle(editorScene.path);
  46. }
  47. }
  48. sceneNames.Clear();
  49. return true;
  50. }
  51. private void AddSceneBundle(string scenePath)
  52. {
  53. var bundle = _context.AddRawAssetBundle(new string[] { scenePath }, AssetDatabase.AssetPathToGUID(scenePath), false);
  54. bundleBuildMap.Add(bundle.bundleId, bundle);
  55. }
  56. //private AssetBundleData CreateBundleBuild(string scenePath, string sceneName, string bundleName = null)
  57. //{
  58. // if (string.IsNullOrEmpty(bundleName))
  59. // {
  60. // var tempBundleName = scenePath.Remove(scenePath.Length - 6).Replace('/', '_').ToLower();
  61. // bundleName = $"scene_{tempBundleName}";
  62. // }
  63. // var assetNames = new string[] { scenePath };
  64. // var addreNames = new string[] { scenePath };
  65. // AssetBundleData assetBundleBuild = new AssetBundleData(bundleName, Define.BUNDLE_VARIANT, assetNames, addreNames);
  66. // return assetBundleBuild;
  67. //}
  68. }
  69. }