SpriteAttacher.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // Original Contribution by: Mitch Thompson
  30. using Spine.Unity.AttachmentTools;
  31. using System.Collections.Generic;
  32. using UnityEngine;
  33. namespace Spine.Unity.Examples {
  34. public class SpriteAttacher : MonoBehaviour {
  35. public const string DefaultPMAShader = "Spine/Skeleton";
  36. public const string DefaultStraightAlphaShader = "Sprites/Default";
  37. #region Inspector
  38. public bool attachOnStart = true;
  39. public bool overrideAnimation = true;
  40. public Sprite sprite;
  41. [SpineSlot] public string slot;
  42. #endregion
  43. #if UNITY_EDITOR
  44. void OnValidate () {
  45. var skeletonComponent = GetComponent<ISkeletonComponent>();
  46. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  47. bool applyPMA;
  48. if (skeletonRenderer != null) {
  49. applyPMA = skeletonRenderer.pmaVertexColors;
  50. } else {
  51. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  52. applyPMA = skeletonGraphic != null && skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  53. }
  54. if (applyPMA) {
  55. try {
  56. if (sprite == null)
  57. return;
  58. sprite.texture.GetPixel(0, 0);
  59. } catch (UnityException e) {
  60. Debug.LogFormat("Texture of {0} ({1}) is not read/write enabled. SpriteAttacher requires this in order to work with a SkeletonRenderer that renders premultiplied alpha. Please check the texture settings.", sprite.name, sprite.texture.name);
  61. UnityEditor.EditorGUIUtility.PingObject(sprite.texture);
  62. throw e;
  63. }
  64. }
  65. }
  66. #endif
  67. RegionAttachment attachment;
  68. Slot spineSlot;
  69. bool applyPMA;
  70. static Dictionary<Texture, AtlasPage> atlasPageCache;
  71. static AtlasPage GetPageFor (Texture texture, Shader shader) {
  72. if (atlasPageCache == null) atlasPageCache = new Dictionary<Texture, AtlasPage>();
  73. AtlasPage atlasPage;
  74. atlasPageCache.TryGetValue(texture, out atlasPage);
  75. if (atlasPage == null) {
  76. var newMaterial = new Material(shader);
  77. atlasPage = newMaterial.ToSpineAtlasPage();
  78. atlasPageCache[texture] = atlasPage;
  79. }
  80. return atlasPage;
  81. }
  82. void Start () {
  83. // Initialize slot and attachment references.
  84. Initialize(false);
  85. if (attachOnStart)
  86. Attach();
  87. }
  88. void AnimationOverrideSpriteAttach (ISkeletonAnimation animated) {
  89. if (overrideAnimation && isActiveAndEnabled)
  90. Attach();
  91. }
  92. public void Initialize (bool overwrite = true) {
  93. if (overwrite || attachment == null) {
  94. // Get the applyPMA value.
  95. var skeletonComponent = GetComponent<ISkeletonComponent>();
  96. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  97. if (skeletonRenderer != null)
  98. this.applyPMA = skeletonRenderer.pmaVertexColors;
  99. else {
  100. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  101. if (skeletonGraphic != null)
  102. this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  103. }
  104. // Subscribe to UpdateComplete to override animation keys.
  105. if (overrideAnimation) {
  106. var animatedSkeleton = skeletonComponent as ISkeletonAnimation;
  107. if (animatedSkeleton != null) {
  108. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  109. animatedSkeleton.UpdateComplete += AnimationOverrideSpriteAttach;
  110. }
  111. }
  112. spineSlot = spineSlot ?? skeletonComponent.Skeleton.FindSlot(slot);
  113. Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
  114. if (sprite == null)
  115. attachment = null;
  116. else
  117. attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
  118. }
  119. }
  120. void OnDestroy () {
  121. var animatedSkeleton = GetComponent<ISkeletonAnimation>();
  122. if (animatedSkeleton != null)
  123. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  124. }
  125. /// <summary>Update the slot's attachment to the Attachment generated from the sprite.</summary>
  126. public void Attach () {
  127. if (spineSlot != null)
  128. spineSlot.Attachment = attachment;
  129. }
  130. }
  131. public static class SpriteAttachmentExtensions {
  132. [System.Obsolete]
  133. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  134. return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA, rotation: rotation);
  135. }
  136. [System.Obsolete]
  137. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  138. return skeletonData.AddUnitySprite(slotName, sprite, skinName, Shader.Find(shaderName), applyPMA, rotation: rotation);
  139. }
  140. [System.Obsolete]
  141. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, Shader shader, bool applyPMA, float rotation = 0f) {
  142. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation: rotation);
  143. skeleton.FindSlot(slotName).Attachment = att;
  144. return att;
  145. }
  146. [System.Obsolete]
  147. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA, float rotation = 0f) {
  148. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation);
  149. var slotIndex = skeletonData.FindSlot(slotName).Index;
  150. Skin skin = skeletonData.DefaultSkin;
  151. if (skinName != "")
  152. skin = skeletonData.FindSkin(skinName);
  153. if (skin != null)
  154. skin.SetAttachment(slotIndex, att.Name, att);
  155. return att;
  156. }
  157. }
  158. }