SkeletonGraphicMirror.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class SkeletonGraphicMirror : MonoBehaviour {
  34. public SkeletonRenderer source;
  35. public bool mirrorOnStart = true;
  36. public bool restoreOnDisable = true;
  37. SkeletonGraphic skeletonGraphic;
  38. Skeleton originalSkeleton;
  39. bool originalFreeze;
  40. Texture2D overrideTexture;
  41. private void Awake () {
  42. skeletonGraphic = GetComponent<SkeletonGraphic>();
  43. }
  44. void Start () {
  45. if (mirrorOnStart)
  46. StartMirroring();
  47. }
  48. void LateUpdate () {
  49. skeletonGraphic.UpdateMesh();
  50. }
  51. void OnDisable () {
  52. if (restoreOnDisable)
  53. RestoreIndependentSkeleton();
  54. }
  55. /// <summary>Freeze the SkeletonGraphic on this GameObject, and use the source as the Skeleton to be rendered by the SkeletonGraphic.</summary>
  56. public void StartMirroring () {
  57. if (source == null)
  58. return;
  59. if (skeletonGraphic == null)
  60. return;
  61. skeletonGraphic.startingAnimation = string.Empty;
  62. if (originalSkeleton == null) {
  63. originalSkeleton = skeletonGraphic.Skeleton;
  64. originalFreeze = skeletonGraphic.freeze;
  65. }
  66. skeletonGraphic.Skeleton = source.skeleton;
  67. skeletonGraphic.freeze = true;
  68. if (overrideTexture != null)
  69. skeletonGraphic.OverrideTexture = overrideTexture;
  70. }
  71. /// <summary>Use a new texture for the SkeletonGraphic. Use this if your source skeleton uses a repacked atlas. </summary>
  72. public void UpdateTexture (Texture2D newOverrideTexture) {
  73. overrideTexture = newOverrideTexture;
  74. if (newOverrideTexture != null)
  75. skeletonGraphic.OverrideTexture = overrideTexture;
  76. }
  77. /// <summary>Stops mirroring the source SkeletonRenderer and allows the SkeletonGraphic to become an independent Skeleton component again.</summary>
  78. public void RestoreIndependentSkeleton () {
  79. if (originalSkeleton == null)
  80. return;
  81. skeletonGraphic.Skeleton = originalSkeleton;
  82. skeletonGraphic.freeze = originalFreeze;
  83. skeletonGraphic.OverrideTexture = null;
  84. originalSkeleton = null;
  85. }
  86. }
  87. }