SkeletonRendererCustomMaterials.cs 7.2 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. #define SPINE_OPTIONAL_MATERIALOVERRIDE
  33. // Contributed by: Lost Polygon
  34. using System;
  35. using System.Collections.Generic;
  36. using UnityEngine;
  37. namespace Spine.Unity {
  38. #if NEW_PREFAB_SYSTEM
  39. [ExecuteAlways]
  40. #else
  41. [ExecuteInEditMode]
  42. #endif
  43. public class SkeletonRendererCustomMaterials : MonoBehaviour {
  44. #region Inspector
  45. public SkeletonRenderer skeletonRenderer;
  46. [SerializeField] protected List<SlotMaterialOverride> customSlotMaterials = new List<SlotMaterialOverride>();
  47. [SerializeField] protected List<AtlasMaterialOverride> customMaterialOverrides = new List<AtlasMaterialOverride>();
  48. #if UNITY_EDITOR
  49. void Reset () {
  50. skeletonRenderer = GetComponent<SkeletonRenderer>();
  51. // Populate atlas list
  52. if (skeletonRenderer != null && skeletonRenderer.skeletonDataAsset != null) {
  53. var atlasAssets = skeletonRenderer.skeletonDataAsset.atlasAssets;
  54. var initialAtlasMaterialOverrides = new List<AtlasMaterialOverride>();
  55. foreach (AtlasAssetBase atlasAsset in atlasAssets) {
  56. foreach (Material atlasMaterial in atlasAsset.Materials) {
  57. var atlasMaterialOverride = new AtlasMaterialOverride {
  58. overrideDisabled = true,
  59. originalMaterial = atlasMaterial
  60. };
  61. initialAtlasMaterialOverrides.Add(atlasMaterialOverride);
  62. }
  63. }
  64. customMaterialOverrides = initialAtlasMaterialOverrides;
  65. }
  66. }
  67. #endif
  68. #endregion
  69. void SetCustomSlotMaterials () {
  70. if (skeletonRenderer == null) {
  71. Debug.LogError("skeletonRenderer == null");
  72. return;
  73. }
  74. for (int i = 0; i < customSlotMaterials.Count; i++) {
  75. SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i];
  76. if (slotMaterialOverride.overrideDisabled || string.IsNullOrEmpty(slotMaterialOverride.slotName))
  77. continue;
  78. Slot slotObject = skeletonRenderer.skeleton.FindSlot(slotMaterialOverride.slotName);
  79. skeletonRenderer.CustomSlotMaterials[slotObject] = slotMaterialOverride.material;
  80. }
  81. }
  82. void RemoveCustomSlotMaterials () {
  83. if (skeletonRenderer == null) {
  84. Debug.LogError("skeletonRenderer == null");
  85. return;
  86. }
  87. for (int i = 0; i < customSlotMaterials.Count; i++) {
  88. SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i];
  89. if (string.IsNullOrEmpty(slotMaterialOverride.slotName))
  90. continue;
  91. Slot slotObject = skeletonRenderer.skeleton.FindSlot(slotMaterialOverride.slotName);
  92. Material currentMaterial;
  93. if (!skeletonRenderer.CustomSlotMaterials.TryGetValue(slotObject, out currentMaterial))
  94. continue;
  95. // Do not revert the material if it was changed by something else
  96. if (currentMaterial != slotMaterialOverride.material)
  97. continue;
  98. skeletonRenderer.CustomSlotMaterials.Remove(slotObject);
  99. }
  100. }
  101. void SetCustomMaterialOverrides () {
  102. if (skeletonRenderer == null) {
  103. Debug.LogError("skeletonRenderer == null");
  104. return;
  105. }
  106. #if SPINE_OPTIONAL_MATERIALOVERRIDE
  107. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  108. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  109. if (atlasMaterialOverride.overrideDisabled)
  110. continue;
  111. skeletonRenderer.CustomMaterialOverride[atlasMaterialOverride.originalMaterial] = atlasMaterialOverride.replacementMaterial;
  112. }
  113. #endif
  114. }
  115. void RemoveCustomMaterialOverrides () {
  116. if (skeletonRenderer == null) {
  117. Debug.LogError("skeletonRenderer == null");
  118. return;
  119. }
  120. #if SPINE_OPTIONAL_MATERIALOVERRIDE
  121. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  122. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  123. Material currentMaterial;
  124. if (!skeletonRenderer.CustomMaterialOverride.TryGetValue(atlasMaterialOverride.originalMaterial, out currentMaterial))
  125. continue;
  126. // Do not revert the material if it was changed by something else
  127. if (currentMaterial != atlasMaterialOverride.replacementMaterial)
  128. continue;
  129. skeletonRenderer.CustomMaterialOverride.Remove(atlasMaterialOverride.originalMaterial);
  130. }
  131. #endif
  132. }
  133. // OnEnable applies the overrides at runtime, and when the editor loads.
  134. void OnEnable () {
  135. if (skeletonRenderer == null)
  136. skeletonRenderer = GetComponent<SkeletonRenderer>();
  137. if (skeletonRenderer == null) {
  138. Debug.LogError("skeletonRenderer == null");
  139. return;
  140. }
  141. skeletonRenderer.Initialize(false);
  142. SetCustomMaterialOverrides();
  143. SetCustomSlotMaterials();
  144. }
  145. // OnDisable removes the overrides at runtime, and in the editor when the component is disabled or destroyed.
  146. void OnDisable () {
  147. if (skeletonRenderer == null) {
  148. Debug.LogError("skeletonRenderer == null");
  149. return;
  150. }
  151. RemoveCustomMaterialOverrides();
  152. RemoveCustomSlotMaterials();
  153. }
  154. [Serializable]
  155. public struct SlotMaterialOverride : IEquatable<SlotMaterialOverride> {
  156. public bool overrideDisabled;
  157. [SpineSlot]
  158. public string slotName;
  159. public Material material;
  160. public bool Equals (SlotMaterialOverride other) {
  161. return overrideDisabled == other.overrideDisabled && slotName == other.slotName && material == other.material;
  162. }
  163. }
  164. [Serializable]
  165. public struct AtlasMaterialOverride : IEquatable<AtlasMaterialOverride> {
  166. public bool overrideDisabled;
  167. public Material originalMaterial;
  168. public Material replacementMaterial;
  169. public bool Equals (AtlasMaterialOverride other) {
  170. return overrideDisabled == other.overrideDisabled && originalMaterial == other.originalMaterial && replacementMaterial == other.replacementMaterial;
  171. }
  172. }
  173. }
  174. }