SkeletonGhostRenderer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Contributed by: Mitch Thompson
  30. using System.Collections;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class SkeletonGhostRenderer : MonoBehaviour {
  34. static readonly Color32 TransparentBlack = new Color32(0, 0, 0, 0);
  35. const string colorPropertyName = "_Color";
  36. float fadeSpeed = 10;
  37. Color32 startColor;
  38. MeshFilter meshFilter;
  39. MeshRenderer meshRenderer;
  40. MaterialPropertyBlock mpb;
  41. int colorId;
  42. void Awake () {
  43. meshRenderer = gameObject.AddComponent<MeshRenderer>();
  44. meshFilter = gameObject.AddComponent<MeshFilter>();
  45. colorId = Shader.PropertyToID(colorPropertyName);
  46. mpb = new MaterialPropertyBlock();
  47. }
  48. public void Initialize (Mesh mesh, Material[] materials, Color32 color, bool additive, float speed, int sortingLayerID, int sortingOrder) {
  49. StopAllCoroutines();
  50. gameObject.SetActive(true);
  51. meshRenderer.sharedMaterials = materials;
  52. meshRenderer.sortingLayerID = sortingLayerID;
  53. meshRenderer.sortingOrder = sortingOrder;
  54. meshFilter.sharedMesh = Instantiate(mesh);
  55. startColor = color;
  56. mpb.SetColor(colorId, color);
  57. meshRenderer.SetPropertyBlock(mpb);
  58. fadeSpeed = speed;
  59. if (additive)
  60. StartCoroutine(FadeAdditive());
  61. else
  62. StartCoroutine(Fade());
  63. }
  64. IEnumerator Fade () {
  65. Color32 c = startColor;
  66. Color32 black = SkeletonGhostRenderer.TransparentBlack;
  67. float t = 1f;
  68. for (float hardTimeLimit = 5f; hardTimeLimit > 0; hardTimeLimit -= Time.deltaTime) {
  69. c = Color32.Lerp(black, startColor, t);
  70. mpb.SetColor(colorId, c);
  71. meshRenderer.SetPropertyBlock(mpb);
  72. t = Mathf.Lerp(t, 0, Time.deltaTime * fadeSpeed);
  73. if (t <= 0)
  74. break;
  75. yield return null;
  76. }
  77. Destroy(meshFilter.sharedMesh);
  78. gameObject.SetActive(false);
  79. }
  80. IEnumerator FadeAdditive () {
  81. Color32 c = startColor;
  82. Color32 black = SkeletonGhostRenderer.TransparentBlack;
  83. float t = 1f;
  84. for (float hardTimeLimit = 5f; hardTimeLimit > 0; hardTimeLimit -= Time.deltaTime) {
  85. c = Color32.Lerp(black, startColor, t);
  86. mpb.SetColor(colorId, c);
  87. meshRenderer.SetPropertyBlock(mpb);
  88. t = Mathf.Lerp(t, 0, Time.deltaTime * fadeSpeed);
  89. if (t <= 0)
  90. break;
  91. yield return null;
  92. }
  93. Destroy(meshFilter.sharedMesh);
  94. gameObject.SetActive(false);
  95. }
  96. public void Cleanup () {
  97. if (meshFilter != null && meshFilter.sharedMesh != null)
  98. Destroy(meshFilter.sharedMesh);
  99. Destroy(gameObject);
  100. }
  101. }
  102. }