SkeletonRendererCustomMaterials.cs 7.5 KB

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