AddressablesTextureLoader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 System.Linq;
  34. using UnityEngine;
  35. using UnityEngine.AddressableAssets;
  36. using UnityEngine.ResourceManagement.AsyncOperations;
  37. namespace Spine.Unity {
  38. [System.Serializable]
  39. public struct AddressableTextureReference : ITargetTextureReference {
  40. [SerializeField] public AssetReferenceTexture assetReference;
  41. #if UNITY_EDITOR
  42. public Texture EditorTexture {
  43. get {
  44. return (Texture)assetReference.editorAsset;
  45. }
  46. }
  47. #endif
  48. }
  49. public struct AddressableRequest : IOnDemandRequest {
  50. public AsyncOperationHandle<Texture> handle;
  51. public bool WasRequested {
  52. get { return handle.IsValid(); }
  53. }
  54. public bool WasSuccessfullyLoaded {
  55. get { return handle.IsValid() && handle.Status == AsyncOperationStatus.Succeeded; }
  56. }
  57. public bool IsTarget (Texture texture) {
  58. return handle.Result == texture;
  59. }
  60. public void Release () {
  61. Addressables.Release(handle);
  62. }
  63. }
  64. [System.Serializable]
  65. public class AddressablesTextureLoader : GenericOnDemandTextureLoader<AddressableTextureReference, AddressableRequest> {
  66. public override void CreateTextureRequest (AddressableTextureReference targetReference,
  67. MaterialOnDemandData materialData, int textureIndex, Material materialToUpdate,
  68. System.Action<Texture> onTextureLoaded) {
  69. OnTextureRequested(materialToUpdate, textureIndex);
  70. materialData.textureRequests[textureIndex].handle = targetReference.assetReference.LoadAssetAsync<Texture>();
  71. materialData.textureRequests[textureIndex].handle.Completed += (obj) => {
  72. if (obj.Status == AsyncOperationStatus.Succeeded) {
  73. Texture loadedTexture = (Texture)targetReference.assetReference.Asset;
  74. materialToUpdate.mainTexture = loadedTexture;
  75. OnTextureLoaded(materialToUpdate, textureIndex);
  76. if (onTextureLoaded != null) onTextureLoaded(loadedTexture);
  77. } else {
  78. OnTextureLoadFailed(materialToUpdate, textureIndex);
  79. }
  80. };
  81. }
  82. public override Texture GetAlreadyLoadedTexture (int materialIndex, int textureIndex) {
  83. AddressableTextureReference targetReference = placeholderMap[materialIndex].textures[textureIndex].targetTextureReference;
  84. return (Texture)targetReference.assetReference.Asset;
  85. }
  86. }
  87. }
  88. #endif