AddressablesTextureLoaderInspector.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #define SPINE_OPTIONAL_ON_DEMAND_LOADING
  30. #if SPINE_OPTIONAL_ON_DEMAND_LOADING
  31. using System;
  32. using System.Collections.Generic;
  33. using UnityEditor;
  34. using UnityEngine;
  35. using UnityEngine.AddressableAssets;
  36. using UnityEngine.ResourceManagement.AsyncOperations;
  37. namespace Spine.Unity.Editor {
  38. using GenericTextureLoader = GenericOnDemandTextureLoader<AddressableTextureReference, AddressableRequest>;
  39. using GenericTextureLoaderInspector = GenericOnDemandTextureLoaderInspector<AddressableTextureReference, AddressableRequest>;
  40. using PlaceholderMaterialMapping = AddressablesTextureLoader.PlaceholderMaterialMapping;
  41. using PlaceholderTextureMapping = AddressablesTextureLoader.PlaceholderTextureMapping;
  42. [InitializeOnLoad]
  43. [CustomEditor(typeof(AddressablesTextureLoader)), CanEditMultipleObjects]
  44. public class AddressablesTextureLoaderInspector : GenericTextureLoaderInspector {
  45. // Note: This static ctor ensures the generic base class method RegisterPlayModeChangedCallbacks is
  46. // definitely called via InitializeOnLoad. Otherwise problems arose where the base class static ctor code
  47. // is not executed (related to being a generic class).
  48. static AddressablesTextureLoaderInspector () {
  49. // The call below is necessary, otherwise the static GenericTextureLoaderInspector ctor is not called.
  50. GenericTextureLoaderInspector.RegisterPlayModeChangedCallbacks();
  51. }
  52. public class AddressablesMethodImplementations : StaticMethodImplementations {
  53. public override string LoaderSuffix { get { return "_Addressable"; } }
  54. public override GenericTextureLoader GetOrCreateLoader (string loaderPath) {
  55. AddressablesTextureLoader loader = AssetDatabase.LoadAssetAtPath<AddressablesTextureLoader>(loaderPath);
  56. if (loader == null) {
  57. loader = AddressablesTextureLoader.CreateInstance<AddressablesTextureLoader>();
  58. AssetDatabase.CreateAsset(loader, loaderPath);
  59. loader = AssetDatabase.LoadAssetAtPath<AddressablesTextureLoader>(loaderPath);
  60. } else {
  61. loader.Clear(clearAtlasAsset: false);
  62. }
  63. return loader;
  64. }
  65. public override bool SetupOnDemandLoadingReference (
  66. ref AddressableTextureReference targetTextureReference, Texture targetTexture) {
  67. string targetTextureGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetTexture));
  68. if (string.IsNullOrEmpty(targetTextureGUID))
  69. return false;
  70. targetTextureReference.assetReference = new AssetReferenceTexture(targetTextureGUID);
  71. return targetTextureReference.assetReference.IsValid();
  72. }
  73. }
  74. #region Context Menu Item
  75. [MenuItem("CONTEXT/AtlasAssetBase/Add Addressables Loader")]
  76. static void AddAddressablesLoader (MenuCommand cmd) {
  77. if (staticMethods == null)
  78. staticMethods = new AddressablesMethodImplementations();
  79. staticMethods.AddOnDemandLoader(cmd);
  80. }
  81. #endregion
  82. protected override StaticMethodImplementations CreateStaticMethodImplementations () {
  83. return new AddressablesMethodImplementations();
  84. }
  85. protected override void DrawSingleLineTargetTextureProperty (SerializedProperty property) {
  86. EditorGUILayout.PropertyField(property.FindPropertyRelative("assetReference"), GUIContent.none, true);
  87. }
  88. }
  89. }
  90. #endif