AssetBundleManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. namespace AtlasUtility
  2. {
  3. using UnityEditor;
  4. using UnityEngine;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. public class AssetBundleManager
  9. {
  10. public static void SwitchAssetBundle(string editorPath, string mobilePath, string desktopPath, Platform toPlatform)
  11. {
  12. AssetImporter mobileImporter = AssetImporter.GetAtPath(mobilePath);
  13. AssetImporter desktopImporter = AssetImporter.GetAtPath(desktopPath);
  14. AssetImporter editorImporter = AssetImporter.GetAtPath(editorPath);
  15. if (toPlatform == Platform.Editor)
  16. {
  17. if (editorImporter == null)
  18. {
  19. Debug.Log("没有编辑器版本");
  20. return;
  21. }
  22. }
  23. else if (toPlatform == Platform.Mobile)
  24. {
  25. if (mobileImporter == null)
  26. {
  27. Debug.Log("没有移动版本");
  28. return;
  29. }
  30. }
  31. else if (toPlatform == Platform.Desktop)
  32. {
  33. if (desktopImporter == null)
  34. {
  35. Debug.LogWarning("没有桌面版本");
  36. return;
  37. }
  38. }
  39. else
  40. {
  41. throw new Exception();
  42. }
  43. List<string> pathes = new List<string>();
  44. List<string> bundleNames = new List<string>();
  45. List<string> bundleVariants = new List<string>();
  46. if (mobileImporter != null)
  47. {
  48. if (mobileImporter.assetBundleName != "" || mobileImporter.assetBundleVariant != "")
  49. {
  50. pathes.Add(mobileImporter.assetPath);
  51. }
  52. }
  53. if (desktopImporter != null)
  54. {
  55. if (desktopImporter.assetBundleName != "" || desktopImporter.assetBundleVariant != "")
  56. {
  57. pathes.Add(desktopImporter.assetPath);
  58. }
  59. }
  60. if (editorImporter != null)
  61. {
  62. if (editorImporter.assetBundleName != "" || editorImporter.assetBundleVariant != "")
  63. {
  64. pathes.Add(editorImporter.assetPath);
  65. }
  66. }
  67. if (pathes.Count == 0)
  68. {
  69. return;
  70. }
  71. if (!IsSameAssetBundle(pathes, bundleNames, bundleVariants))
  72. {
  73. Debug.LogWarning($"{editorPath} 的不同版本有不同的AssetBundle");
  74. return;
  75. }
  76. if (editorImporter != null)
  77. {
  78. editorImporter.SetAssetBundleNameAndVariant("", "");
  79. editorImporter.SaveAndReimport();
  80. }
  81. if (mobileImporter != null)
  82. {
  83. mobileImporter.SetAssetBundleNameAndVariant("", "");
  84. mobileImporter.SaveAndReimport();
  85. }
  86. if (desktopImporter != null)
  87. {
  88. desktopImporter.SetAssetBundleNameAndVariant("", "");
  89. desktopImporter.SaveAndReimport();
  90. }
  91. if (toPlatform == Platform.Editor)
  92. {
  93. editorImporter.assetBundleName = bundleNames[0];
  94. editorImporter.assetBundleVariant = bundleVariants[0];
  95. editorImporter.SaveAndReimport();
  96. }
  97. else if (toPlatform == Platform.Mobile)
  98. {
  99. mobileImporter.assetBundleName = bundleNames[0];
  100. mobileImporter.assetBundleVariant = bundleVariants[0];
  101. mobileImporter.SaveAndReimport();
  102. }
  103. else if (toPlatform == Platform.Desktop)
  104. {
  105. desktopImporter.assetBundleName = bundleNames[0];
  106. desktopImporter.assetBundleVariant = bundleVariants[0];
  107. desktopImporter.SaveAndReimport();
  108. }
  109. else
  110. {
  111. throw new Exception();
  112. }
  113. }
  114. public static bool IsSameAssetBundle(List<string> pathes, List<string> bundleNames, List<string> bundleVariants)
  115. {
  116. for (int i = 0; i < pathes.Count; i++)
  117. {
  118. if (bundleNames.Count > 0 && !bundleNames.Contains(AssetImporter.GetAtPath(pathes[i]).assetBundleName))
  119. {
  120. return false;
  121. }
  122. else
  123. {
  124. bundleNames.Add(AssetImporter.GetAtPath(pathes[i]).assetBundleName);
  125. }
  126. if (bundleVariants.Count > 0 && !bundleVariants.Contains(AssetImporter.GetAtPath(pathes[i]).assetBundleVariant))
  127. {
  128. return false;
  129. }
  130. else
  131. {
  132. bundleVariants.Add(AssetImporter.GetAtPath(pathes[i]).assetBundleVariant);
  133. }
  134. }
  135. return true;
  136. }
  137. }
  138. }