SkeletonGraphicCustomMaterials.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. [HelpURL("http://esotericsoftware.com/spine-unity#SkeletonGraphicCustomMaterials")]
  42. public class SkeletonGraphicCustomMaterials : MonoBehaviour {
  43. #region Inspector
  44. public SkeletonGraphic skeletonGraphic;
  45. [SerializeField] protected List<AtlasMaterialOverride> customMaterialOverrides = new List<AtlasMaterialOverride>();
  46. [SerializeField] protected List<AtlasTextureOverride> customTextureOverrides = new List<AtlasTextureOverride>();
  47. #if UNITY_EDITOR
  48. void Reset () {
  49. skeletonGraphic = GetComponent<SkeletonGraphic>();
  50. // Populate material list
  51. if (skeletonGraphic != null && skeletonGraphic.skeletonDataAsset != null) {
  52. var atlasAssets = skeletonGraphic.skeletonDataAsset.atlasAssets;
  53. var initialAtlasMaterialOverrides = new List<AtlasMaterialOverride>();
  54. foreach (AtlasAssetBase atlasAsset in atlasAssets) {
  55. foreach (Material atlasMaterial in atlasAsset.Materials) {
  56. var atlasMaterialOverride = new AtlasMaterialOverride {
  57. overrideEnabled = false,
  58. originalTexture = atlasMaterial.mainTexture
  59. };
  60. initialAtlasMaterialOverrides.Add(atlasMaterialOverride);
  61. }
  62. }
  63. customMaterialOverrides = initialAtlasMaterialOverrides;
  64. }
  65. // Populate texture list
  66. if (skeletonGraphic != null && skeletonGraphic.skeletonDataAsset != null) {
  67. var atlasAssets = skeletonGraphic.skeletonDataAsset.atlasAssets;
  68. var initialAtlasTextureOverrides = new List<AtlasTextureOverride>();
  69. foreach (AtlasAssetBase atlasAsset in atlasAssets) {
  70. foreach (Material atlasMaterial in atlasAsset.Materials) {
  71. var atlasTextureOverride = new AtlasTextureOverride {
  72. overrideEnabled = false,
  73. originalTexture = atlasMaterial.mainTexture
  74. };
  75. initialAtlasTextureOverrides.Add(atlasTextureOverride);
  76. }
  77. }
  78. customTextureOverrides = initialAtlasTextureOverrides;
  79. }
  80. }
  81. #endif
  82. #endregion
  83. void SetCustomMaterialOverrides () {
  84. if (skeletonGraphic == null) {
  85. Debug.LogError("skeletonGraphic == null");
  86. return;
  87. }
  88. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  89. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  90. if (atlasMaterialOverride.overrideEnabled)
  91. skeletonGraphic.CustomMaterialOverride[atlasMaterialOverride.originalTexture] = atlasMaterialOverride.replacementMaterial;
  92. }
  93. }
  94. void RemoveCustomMaterialOverrides () {
  95. if (skeletonGraphic == null) {
  96. Debug.LogError("skeletonGraphic == null");
  97. return;
  98. }
  99. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  100. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  101. Material currentMaterial;
  102. if (!skeletonGraphic.CustomMaterialOverride.TryGetValue(atlasMaterialOverride.originalTexture, out currentMaterial))
  103. continue;
  104. // Do not revert the material if it was changed by something else
  105. if (currentMaterial != atlasMaterialOverride.replacementMaterial)
  106. continue;
  107. skeletonGraphic.CustomMaterialOverride.Remove(atlasMaterialOverride.originalTexture);
  108. }
  109. }
  110. void SetCustomTextureOverrides () {
  111. if (skeletonGraphic == null) {
  112. Debug.LogError("skeletonGraphic == null");
  113. return;
  114. }
  115. for (int i = 0; i < customTextureOverrides.Count; i++) {
  116. AtlasTextureOverride atlasTextureOverride = customTextureOverrides[i];
  117. if (atlasTextureOverride.overrideEnabled)
  118. skeletonGraphic.CustomTextureOverride[atlasTextureOverride.originalTexture] = atlasTextureOverride.replacementTexture;
  119. }
  120. }
  121. void RemoveCustomTextureOverrides () {
  122. if (skeletonGraphic == null) {
  123. Debug.LogError("skeletonGraphic == null");
  124. return;
  125. }
  126. for (int i = 0; i < customTextureOverrides.Count; i++) {
  127. AtlasTextureOverride atlasTextureOverride = customTextureOverrides[i];
  128. Texture currentTexture;
  129. if (!skeletonGraphic.CustomTextureOverride.TryGetValue(atlasTextureOverride.originalTexture, out currentTexture))
  130. continue;
  131. // Do not revert the material if it was changed by something else
  132. if (currentTexture != atlasTextureOverride.replacementTexture)
  133. continue;
  134. skeletonGraphic.CustomTextureOverride.Remove(atlasTextureOverride.originalTexture);
  135. }
  136. }
  137. // OnEnable applies the overrides at runtime, and when the editor loads.
  138. void OnEnable () {
  139. if (skeletonGraphic == null)
  140. skeletonGraphic = GetComponent<SkeletonGraphic>();
  141. if (skeletonGraphic == null) {
  142. Debug.LogError("skeletonGraphic == null");
  143. return;
  144. }
  145. skeletonGraphic.Initialize(false);
  146. SetCustomMaterialOverrides();
  147. SetCustomTextureOverrides();
  148. }
  149. // OnDisable removes the overrides at runtime, and in the editor when the component is disabled or destroyed.
  150. void OnDisable () {
  151. if (skeletonGraphic == null) {
  152. Debug.LogError("skeletonGraphic == null");
  153. return;
  154. }
  155. RemoveCustomMaterialOverrides();
  156. RemoveCustomTextureOverrides();
  157. }
  158. [Serializable]
  159. public struct AtlasMaterialOverride : IEquatable<AtlasMaterialOverride> {
  160. public bool overrideEnabled;
  161. public Texture originalTexture;
  162. public Material replacementMaterial;
  163. public bool Equals (AtlasMaterialOverride other) {
  164. return overrideEnabled == other.overrideEnabled && originalTexture == other.originalTexture && replacementMaterial == other.replacementMaterial;
  165. }
  166. }
  167. [Serializable]
  168. public struct AtlasTextureOverride : IEquatable<AtlasTextureOverride> {
  169. public bool overrideEnabled;
  170. public Texture originalTexture;
  171. public Texture replacementTexture;
  172. public bool Equals (AtlasTextureOverride other) {
  173. return overrideEnabled == other.overrideEnabled && originalTexture == other.originalTexture && replacementTexture == other.replacementTexture;
  174. }
  175. }
  176. }
  177. }