RenderExistingMesh.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.Collections.Generic;
  33. using UnityEngine;
  34. namespace Spine.Unity.Examples {
  35. #if NEW_PREFAB_SYSTEM
  36. [ExecuteAlways]
  37. #else
  38. [ExecuteInEditMode]
  39. #endif
  40. [RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
  41. public class RenderExistingMesh : MonoBehaviour {
  42. public MeshRenderer referenceRenderer;
  43. bool updateViaSkeletonCallback = false;
  44. MeshFilter referenceMeshFilter;
  45. MeshRenderer ownRenderer;
  46. MeshFilter ownMeshFilter;
  47. [System.Serializable]
  48. public struct MaterialReplacement {
  49. public Material originalMaterial;
  50. public Material replacementMaterial;
  51. }
  52. public MaterialReplacement[] replacementMaterials = new MaterialReplacement[0];
  53. private Dictionary<Material, Material> replacementMaterialDict = new Dictionary<Material, Material>();
  54. private Material[] sharedMaterials = new Material[0];
  55. #if UNITY_EDITOR
  56. private void Reset () {
  57. if (referenceRenderer == null) {
  58. referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
  59. if (!referenceRenderer)
  60. return;
  61. }
  62. var parentMaterials = referenceRenderer.sharedMaterials;
  63. if (replacementMaterials.Length != parentMaterials.Length) {
  64. replacementMaterials = new MaterialReplacement[parentMaterials.Length];
  65. }
  66. for (int i = 0; i < parentMaterials.Length; ++i) {
  67. replacementMaterials[i].originalMaterial = parentMaterials[i];
  68. replacementMaterials[i].replacementMaterial = parentMaterials[i];
  69. }
  70. Awake();
  71. LateUpdate();
  72. }
  73. #endif
  74. void Awake () {
  75. if (referenceRenderer == null) {
  76. referenceRenderer = this.transform.parent.GetComponentInParent<MeshRenderer>();
  77. }
  78. // subscribe to OnMeshAndMaterialsUpdated
  79. var skeletonRenderer = referenceRenderer.GetComponent<SkeletonAnimation>();
  80. if (skeletonRenderer) {
  81. skeletonRenderer.OnMeshAndMaterialsUpdated -= UpdateOnCallback;
  82. skeletonRenderer.OnMeshAndMaterialsUpdated += UpdateOnCallback;
  83. updateViaSkeletonCallback = true;
  84. }
  85. referenceMeshFilter = referenceRenderer.GetComponent<MeshFilter>();
  86. ownRenderer = this.GetComponent<MeshRenderer>();
  87. ownMeshFilter = this.GetComponent<MeshFilter>();
  88. InitializeDict();
  89. }
  90. #if UNITY_EDITOR
  91. private void Update () {
  92. if (!Application.isPlaying) {
  93. InitializeDict();
  94. }
  95. }
  96. #endif
  97. void LateUpdate () {
  98. #if UNITY_EDITOR
  99. if (!Application.isPlaying) {
  100. UpdateMaterials();
  101. return;
  102. }
  103. #endif
  104. if (updateViaSkeletonCallback)
  105. return;
  106. UpdateMaterials();
  107. }
  108. void UpdateOnCallback (SkeletonRenderer r) {
  109. UpdateMaterials();
  110. }
  111. void UpdateMaterials () {
  112. ownMeshFilter.sharedMesh = referenceMeshFilter.sharedMesh;
  113. var parentMaterials = referenceRenderer.sharedMaterials;
  114. if (sharedMaterials.Length != parentMaterials.Length) {
  115. sharedMaterials = new Material[parentMaterials.Length];
  116. }
  117. for (int i = 0; i < parentMaterials.Length; ++i) {
  118. var parentMaterial = parentMaterials[i];
  119. if (replacementMaterialDict.ContainsKey(parentMaterial)) {
  120. sharedMaterials[i] = replacementMaterialDict[parentMaterial];
  121. }
  122. }
  123. ownRenderer.sharedMaterials = sharedMaterials;
  124. }
  125. void InitializeDict () {
  126. for (int i = 0; i < replacementMaterials.Length; ++i) {
  127. var entry = replacementMaterials[i];
  128. replacementMaterialDict[entry.originalMaterial] = entry.replacementMaterial;
  129. }
  130. }
  131. }
  132. }