SkeletonRendererCustomMaterials.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. skeletonRenderer.CustomSlotMaterials[slotObject] = slotMaterialOverride.material;
  81. }
  82. }
  83. void RemoveCustomSlotMaterials () {
  84. if (skeletonRenderer == null) {
  85. Debug.LogError("skeletonRenderer == null");
  86. return;
  87. }
  88. for (int i = 0; i < customSlotMaterials.Count; i++) {
  89. SlotMaterialOverride slotMaterialOverride = customSlotMaterials[i];
  90. if (string.IsNullOrEmpty(slotMaterialOverride.slotName))
  91. continue;
  92. Slot slotObject = skeletonRenderer.skeleton.FindSlot(slotMaterialOverride.slotName);
  93. Material currentMaterial;
  94. if (!skeletonRenderer.CustomSlotMaterials.TryGetValue(slotObject, out currentMaterial))
  95. continue;
  96. // Do not revert the material if it was changed by something else
  97. if (currentMaterial != slotMaterialOverride.material)
  98. continue;
  99. skeletonRenderer.CustomSlotMaterials.Remove(slotObject);
  100. }
  101. }
  102. void SetCustomMaterialOverrides () {
  103. if (skeletonRenderer == null) {
  104. Debug.LogError("skeletonRenderer == null");
  105. return;
  106. }
  107. #if SPINE_OPTIONAL_MATERIALOVERRIDE
  108. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  109. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  110. if (atlasMaterialOverride.overrideDisabled)
  111. continue;
  112. skeletonRenderer.CustomMaterialOverride[atlasMaterialOverride.originalMaterial] = atlasMaterialOverride.replacementMaterial;
  113. }
  114. #endif
  115. }
  116. void RemoveCustomMaterialOverrides () {
  117. if (skeletonRenderer == null) {
  118. Debug.LogError("skeletonRenderer == null");
  119. return;
  120. }
  121. #if SPINE_OPTIONAL_MATERIALOVERRIDE
  122. for (int i = 0; i < customMaterialOverrides.Count; i++) {
  123. AtlasMaterialOverride atlasMaterialOverride = customMaterialOverrides[i];
  124. Material currentMaterial;
  125. if (!skeletonRenderer.CustomMaterialOverride.TryGetValue(atlasMaterialOverride.originalMaterial, out currentMaterial))
  126. continue;
  127. // Do not revert the material if it was changed by something else
  128. if (currentMaterial != atlasMaterialOverride.replacementMaterial)
  129. continue;
  130. skeletonRenderer.CustomMaterialOverride.Remove(atlasMaterialOverride.originalMaterial);
  131. }
  132. #endif
  133. }
  134. // OnEnable applies the overrides at runtime, and when the editor loads.
  135. void OnEnable () {
  136. if (skeletonRenderer == null)
  137. skeletonRenderer = GetComponent<SkeletonRenderer>();
  138. if (skeletonRenderer == null) {
  139. Debug.LogError("skeletonRenderer == null");
  140. return;
  141. }
  142. skeletonRenderer.Initialize(false);
  143. SetCustomMaterialOverrides();
  144. SetCustomSlotMaterials();
  145. }
  146. // OnDisable removes the overrides at runtime, and in the editor when the component is disabled or destroyed.
  147. void OnDisable () {
  148. if (skeletonRenderer == null) {
  149. Debug.LogError("skeletonRenderer == null");
  150. return;
  151. }
  152. RemoveCustomMaterialOverrides();
  153. RemoveCustomSlotMaterials();
  154. }
  155. [Serializable]
  156. public struct SlotMaterialOverride : IEquatable<SlotMaterialOverride> {
  157. public bool overrideDisabled;
  158. [SpineSlot]
  159. public string slotName;
  160. public Material material;
  161. public bool Equals (SlotMaterialOverride other) {
  162. return overrideDisabled == other.overrideDisabled && slotName == other.slotName && material == other.material;
  163. }
  164. }
  165. [Serializable]
  166. public struct AtlasMaterialOverride : IEquatable<AtlasMaterialOverride> {
  167. public bool overrideDisabled;
  168. public Material originalMaterial;
  169. public Material replacementMaterial;
  170. public bool Equals (AtlasMaterialOverride other) {
  171. return overrideDisabled == other.overrideDisabled && originalMaterial == other.originalMaterial && replacementMaterial == other.replacementMaterial;
  172. }
  173. }
  174. }
  175. }