RenderExistingMesh.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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. #if UNITY_2018_2_OR_NEWER
  33. #define HAS_GET_SHARED_MATERIALS
  34. #endif
  35. using System.Collections.Generic;
  36. using UnityEngine;
  37. namespace Spine.Unity.Examples {
  38. #if NEW_PREFAB_SYSTEM
  39. [ExecuteAlways]
  40. #else
  41. [ExecuteInEditMode]
  42. #endif
  43. [RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
  44. public class RenderExistingMesh : MonoBehaviour {
  45. public MeshRenderer referenceRenderer;
  46. bool updateViaSkeletonCallback = false;
  47. MeshFilter referenceMeshFilter;
  48. MeshRenderer ownRenderer;
  49. MeshFilter ownMeshFilter;
  50. [System.Serializable]
  51. public struct MaterialReplacement {
  52. public Material originalMaterial;
  53. public Material replacementMaterial;
  54. }
  55. public MaterialReplacement[] replacementMaterials = new MaterialReplacement[0];
  56. private Dictionary<Material, Material> replacementMaterialDict = new Dictionary<Material, Material>();
  57. private Material[] sharedMaterials = new Material[0];
  58. #if HAS_GET_SHARED_MATERIALS
  59. private List<Material> parentMaterials = new List<Material>();
  60. #endif
  61. #if UNITY_EDITOR
  62. private void Reset () {
  63. if (referenceRenderer == null) {
  64. if (this.transform.parent)
  65. referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
  66. if (referenceRenderer == null) return;
  67. }
  68. #if HAS_GET_SHARED_MATERIALS
  69. referenceRenderer.GetSharedMaterials(parentMaterials);
  70. int parentMaterialsCount = parentMaterials.Count;
  71. #else
  72. Material[] parentMaterials = referenceRenderer.sharedMaterials;
  73. int parentMaterialsCount = parentMaterials.Length;
  74. #endif
  75. if (replacementMaterials.Length != parentMaterialsCount) {
  76. replacementMaterials = new MaterialReplacement[parentMaterialsCount];
  77. }
  78. for (int i = 0; i < parentMaterialsCount; ++i) {
  79. replacementMaterials[i].originalMaterial = parentMaterials[i];
  80. replacementMaterials[i].replacementMaterial = parentMaterials[i];
  81. }
  82. Awake();
  83. LateUpdate();
  84. }
  85. #endif
  86. void Awake () {
  87. ownRenderer = this.GetComponent<MeshRenderer>();
  88. ownMeshFilter = this.GetComponent<MeshFilter>();
  89. if (referenceRenderer == null) {
  90. if (this.transform.parent != null)
  91. referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
  92. if (referenceRenderer == null) return;
  93. }
  94. referenceMeshFilter = referenceRenderer.GetComponent<MeshFilter>();
  95. // subscribe to OnMeshAndMaterialsUpdated
  96. SkeletonAnimation skeletonRenderer = referenceRenderer.GetComponent<SkeletonAnimation>();
  97. if (skeletonRenderer) {
  98. skeletonRenderer.OnMeshAndMaterialsUpdated -= UpdateOnCallback;
  99. skeletonRenderer.OnMeshAndMaterialsUpdated += UpdateOnCallback;
  100. updateViaSkeletonCallback = true;
  101. }
  102. InitializeDict();
  103. }
  104. #if UNITY_EDITOR
  105. // handle disabled scene reload
  106. private void OnEnable () {
  107. if (Application.isPlaying)
  108. Awake();
  109. }
  110. private void Update () {
  111. if (!Application.isPlaying)
  112. InitializeDict();
  113. }
  114. #endif
  115. void LateUpdate () {
  116. #if UNITY_EDITOR
  117. if (!Application.isPlaying) {
  118. UpdateMaterials();
  119. return;
  120. }
  121. #endif
  122. if (updateViaSkeletonCallback)
  123. return;
  124. UpdateMaterials();
  125. }
  126. void UpdateOnCallback (SkeletonRenderer r) {
  127. UpdateMaterials();
  128. }
  129. void UpdateMaterials () {
  130. #if UNITY_EDITOR
  131. if (!referenceRenderer) return;
  132. if (!referenceMeshFilter) Reset();
  133. #endif
  134. ownMeshFilter.sharedMesh = referenceMeshFilter.sharedMesh;
  135. #if HAS_GET_SHARED_MATERIALS
  136. referenceRenderer.GetSharedMaterials(parentMaterials);
  137. int parentMaterialsCount = parentMaterials.Count;
  138. #else
  139. Material[] parentMaterials = referenceRenderer.sharedMaterials;
  140. int parentMaterialsCount = parentMaterials.Length;
  141. #endif
  142. if (sharedMaterials.Length != parentMaterialsCount) {
  143. sharedMaterials = new Material[parentMaterialsCount];
  144. }
  145. for (int i = 0; i < parentMaterialsCount; ++i) {
  146. Material parentMaterial = parentMaterials[i];
  147. if (replacementMaterialDict.ContainsKey(parentMaterial)) {
  148. sharedMaterials[i] = replacementMaterialDict[parentMaterial];
  149. }
  150. }
  151. ownRenderer.sharedMaterials = sharedMaterials;
  152. }
  153. void InitializeDict () {
  154. replacementMaterialDict.Clear();
  155. for (int i = 0; i < replacementMaterials.Length; ++i) {
  156. MaterialReplacement entry = replacementMaterials[i];
  157. replacementMaterialDict[entry.originalMaterial] = entry.replacementMaterial;
  158. }
  159. }
  160. }
  161. }