SkeletonGraphicCustomMaterials.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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
  13. * or 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
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
  30. #define NEW_PREFAB_SYSTEM
  31. #endif
  32. using System;
  33. using System.Collections.Generic;
  34. using UnityEngine;
  35. namespace Spine.Unity {
  36. #if NEW_PREFAB_SYSTEM
  37. [ExecuteAlways]
  38. #else
  39. [ExecuteInEditMode]
  40. #endif
  41. public class SkeletonGraphicCustomMaterials : MonoBehaviour {
  42. #region Inspector
  43. public SkeletonGraphic skeletonGraphic;
  44. [SerializeField] protected List<AtlasMaterialOverride> customMaterialOverrides = new List<AtlasMaterialOverride>();
  45. [SerializeField] protected List<AtlasTextureOverride> customTextureOverrides = new List<AtlasTextureOverride>();
  46. #if UNITY_EDITOR
  47. void Reset () {
  48. skeletonGraphic = GetComponent<SkeletonGraphic>();
  49. // Populate material list
  50. if (skeletonGraphic != null && skeletonGraphic.skeletonDataAsset != null) {
  51. var atlasAssets = skeletonGraphic.skeletonDataAsset.atlasAssets;
  52. var initialAtlasMaterialOverrides = new List<AtlasMaterialOverride>();
  53. foreach (AtlasAssetBase atlasAsset in atlasAssets) {
  54. foreach (Material atlasMaterial in atlasAsset.Materials) {
  55. var atlasMaterialOverride = new AtlasMaterialOverride {
  56. overrideEnabled = false,
  57. originalTexture = atlasMaterial.mainTexture
  58. };
  59. initialAtlasMaterialOverrides.Add(atlasMaterialOverride);
  60. }
  61. }
  62. customMaterialOverrides = initialAtlasMaterialOverrides;
  63. }
  64. // Populate texture list
  65. if (skeletonGraphic != null && skeletonGraphic.skeletonDataAsset != null) {
  66. var atlasAssets = skeletonGraphic.skeletonDataAsset.atlasAssets;
  67. var initialAtlasTextureOverrides = new List<AtlasTextureOverride>();
  68. foreach (AtlasAssetBase atlasAsset in atlasAssets) {
  69. foreach (Material atlasMaterial in atlasAsset.Materials) {
  70. var atlasTextureOverride = new AtlasTextureOverride {
  71. overrideEnabled = false,
  72. originalTexture = atlasMaterial.mainTexture
  73. };
  74. initialAtlasTextureOverrides.Add(atlasTextureOverride);
  75. }
  76. }
  77. customTextureOverrides = initialAtlasTextureOverrides;
  78. }
  79. }
  80. #endif
  81. #endregion
  82. void SetCustomMaterialOverrides () {
  83. if (skeletonGraphic == null) {
  84. Debug.LogError("skeletonGraphic == null");
  85. return;
  86. }
  87. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  88. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  89. if (atlasMaterialOverride.overrideEnabled)
  90. skeletonGraphic.CustomMaterialOverride[atlasMaterialOverride.originalTexture] = atlasMaterialOverride.replacementMaterial;
  91. }
  92. }
  93. void RemoveCustomMaterialOverrides () {
  94. if (skeletonGraphic == null) {
  95. Debug.LogError("skeletonGraphic == null");
  96. return;
  97. }
  98. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  99. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  100. Material currentMaterial;
  101. if (!skeletonGraphic.CustomMaterialOverride.TryGetValue(atlasMaterialOverride.originalTexture, out currentMaterial))
  102. continue;
  103. // Do not revert the material if it was changed by something else
  104. if (currentMaterial != atlasMaterialOverride.replacementMaterial)
  105. continue;
  106. skeletonGraphic.CustomMaterialOverride.Remove(atlasMaterialOverride.originalTexture);
  107. }
  108. }
  109. void SetCustomTextureOverrides () {
  110. if (skeletonGraphic == null) {
  111. Debug.LogError("skeletonGraphic == null");
  112. return;
  113. }
  114. for (int i = 0; i < customTextureOverrides.Count; i++) {
  115. AtlasTextureOverride atlasTextureOverride = customTextureOverrides[i];
  116. if (atlasTextureOverride.overrideEnabled)
  117. skeletonGraphic.CustomTextureOverride[atlasTextureOverride.originalTexture] = atlasTextureOverride.replacementTexture;
  118. }
  119. }
  120. void RemoveCustomTextureOverrides () {
  121. if (skeletonGraphic == null) {
  122. Debug.LogError("skeletonGraphic == null");
  123. return;
  124. }
  125. for (int i = 0; i < customTextureOverrides.Count; i++) {
  126. AtlasTextureOverride atlasTextureOverride = customTextureOverrides[i];
  127. Texture currentTexture;
  128. if (!skeletonGraphic.CustomTextureOverride.TryGetValue(atlasTextureOverride.originalTexture, out currentTexture))
  129. continue;
  130. // Do not revert the material if it was changed by something else
  131. if (currentTexture != atlasTextureOverride.replacementTexture)
  132. continue;
  133. skeletonGraphic.CustomTextureOverride.Remove(atlasTextureOverride.originalTexture);
  134. }
  135. }
  136. // OnEnable applies the overrides at runtime, and when the editor loads.
  137. void OnEnable () {
  138. if (skeletonGraphic == null)
  139. skeletonGraphic = GetComponent<SkeletonGraphic>();
  140. if (skeletonGraphic == null) {
  141. Debug.LogError("skeletonGraphic == null");
  142. return;
  143. }
  144. skeletonGraphic.Initialize(false);
  145. SetCustomMaterialOverrides();
  146. SetCustomTextureOverrides();
  147. }
  148. // OnDisable removes the overrides at runtime, and in the editor when the component is disabled or destroyed.
  149. void OnDisable () {
  150. if (skeletonGraphic == null) {
  151. Debug.LogError("skeletonGraphic == null");
  152. return;
  153. }
  154. RemoveCustomMaterialOverrides();
  155. RemoveCustomTextureOverrides();
  156. }
  157. [Serializable]
  158. public struct AtlasMaterialOverride : IEquatable<AtlasMaterialOverride> {
  159. public bool overrideEnabled;
  160. public Texture originalTexture;
  161. public Material replacementMaterial;
  162. public bool Equals (AtlasMaterialOverride other) {
  163. return overrideEnabled == other.overrideEnabled && originalTexture == other.originalTexture && replacementMaterial == other.replacementMaterial;
  164. }
  165. }
  166. [Serializable]
  167. public struct AtlasTextureOverride : IEquatable<AtlasTextureOverride> {
  168. public bool overrideEnabled;
  169. public Texture originalTexture;
  170. public Texture replacementTexture;
  171. public bool Equals (AtlasTextureOverride other) {
  172. return overrideEnabled == other.overrideEnabled && originalTexture == other.originalTexture && replacementTexture == other.replacementTexture;
  173. }
  174. }
  175. }
  176. }