AddressableEditorInitialization.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEditor.AddressableAssets.Settings;
  5. using UnityEngine.AddressableAssets;
  6. namespace UnityEditor.AddressableAssets
  7. {
  8. [InitializeOnLoad]
  9. internal class AddressableEditorInitialization
  10. {
  11. private const string m_EditorInitializedBoolName = nameof(m_EditorInitializedBoolName);
  12. static AddressableEditorInitialization()
  13. {
  14. bool editorInitialized = SessionState.GetBool(m_EditorInitializedBoolName, false);
  15. if (editorInitialized) return;
  16. if (Directory.Exists(Addressables.LibraryPath))
  17. PurgeInvalidAssetEntries(AddressableAssetSettingsDefaultObject.Settings);
  18. SessionState.SetBool(m_EditorInitializedBoolName, true);
  19. }
  20. internal static void PurgeInvalidAssetEntries(AddressableAssetSettings settings)
  21. {
  22. if (settings == null) return;
  23. List<AddressableAssetEntry> entriesToRemove = new List<AddressableAssetEntry>();
  24. foreach (var group in settings.groups)
  25. {
  26. if (group == null)
  27. continue;
  28. foreach (var assetEntry in group.entries)
  29. {
  30. if (assetEntry == null ||
  31. assetEntry.address == AddressableAssetEntry.EditorSceneListName ||
  32. assetEntry.address == AddressableAssetEntry.ResourcesName)
  33. continue;
  34. if (!string.IsNullOrEmpty(assetEntry.AssetPath))
  35. {
  36. string path = Path.GetFullPath(assetEntry.AssetPath);
  37. if (!File.Exists(path) && !Directory.Exists(path))
  38. entriesToRemove.Add(assetEntry);
  39. }
  40. else
  41. entriesToRemove.Add(assetEntry);
  42. }
  43. }
  44. StringBuilder builder = new StringBuilder(
  45. "Addressables was unable to detect the following assets in the project " +
  46. "but they were still part of an Addressable group. They have been removed " +
  47. "from Addressables.");
  48. foreach (var entry in entriesToRemove)
  49. {
  50. builder.AppendLine($"\n{entry.address} at {entry.AssetPath}");
  51. settings.RemoveAssetEntry(entry, false);
  52. }
  53. if (entriesToRemove.Count > 0)
  54. Addressables.Log(builder.ToString());
  55. }
  56. }
  57. }