SpriteAttacher.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, 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. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. // Original Contribution by: Mitch Thompson
  30. using UnityEngine;
  31. using System.Collections.Generic;
  32. using Spine.Unity.Modules.AttachmentTools;
  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. sprite.texture.GetPixel(0, 0);
  57. } catch (UnityException e) {
  58. 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);
  59. UnityEditor.EditorGUIUtility.PingObject(sprite.texture);
  60. throw e;
  61. }
  62. }
  63. }
  64. #endif
  65. RegionAttachment attachment;
  66. Slot spineSlot;
  67. bool applyPMA;
  68. static Dictionary<Texture, AtlasPage> atlasPageCache;
  69. static AtlasPage GetPageFor (Texture texture, Shader shader) {
  70. if (atlasPageCache == null) atlasPageCache = new Dictionary<Texture, AtlasPage>();
  71. AtlasPage atlasPage;
  72. atlasPageCache.TryGetValue(texture, out atlasPage);
  73. if (atlasPage == null) {
  74. var newMaterial = new Material(shader);
  75. atlasPage = newMaterial.ToSpineAtlasPage();
  76. atlasPageCache[texture] = atlasPage;
  77. }
  78. return atlasPage;
  79. }
  80. void Start () {
  81. // Initialize slot and attachment references.
  82. Initialize(false);
  83. if (attachOnStart)
  84. Attach();
  85. }
  86. void AnimationOverrideSpriteAttach (ISkeletonAnimation animated) {
  87. if (overrideAnimation && isActiveAndEnabled)
  88. Attach();
  89. }
  90. public void Initialize (bool overwrite = true) {
  91. if (overwrite || attachment == null) {
  92. // Get the applyPMA value.
  93. var skeletonComponent = GetComponent<ISkeletonComponent>();
  94. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  95. if (skeletonRenderer != null)
  96. this.applyPMA = skeletonRenderer.pmaVertexColors;
  97. else {
  98. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  99. if (skeletonGraphic != null)
  100. this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  101. }
  102. // Subscribe to UpdateComplete to override animation keys.
  103. if (overrideAnimation) {
  104. var animatedSkeleton = skeletonComponent as ISkeletonAnimation;
  105. if (animatedSkeleton != null) {
  106. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  107. animatedSkeleton.UpdateComplete += AnimationOverrideSpriteAttach;
  108. }
  109. }
  110. spineSlot = spineSlot ?? skeletonComponent.Skeleton.FindSlot(slot);
  111. Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
  112. attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
  113. }
  114. }
  115. void OnDestroy () {
  116. var animatedSkeleton = GetComponent<ISkeletonAnimation>();
  117. if (animatedSkeleton != null)
  118. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  119. }
  120. /// <summary>Update the slot's attachment to the Attachment generated from the sprite.</summary>
  121. public void Attach () {
  122. if (spineSlot != null)
  123. spineSlot.Attachment = attachment;
  124. }
  125. }
  126. public static class SpriteAttachmentExtensions {
  127. [System.Obsolete]
  128. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  129. return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA, rotation: rotation);
  130. }
  131. [System.Obsolete]
  132. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  133. return skeletonData.AddUnitySprite(slotName, sprite, skinName, Shader.Find(shaderName), applyPMA, rotation: rotation);
  134. }
  135. [System.Obsolete]
  136. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, Shader shader, bool applyPMA, float rotation = 0f) {
  137. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation: rotation);
  138. skeleton.FindSlot(slotName).Attachment = att;
  139. return att;
  140. }
  141. [System.Obsolete]
  142. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA, float rotation = 0f) {
  143. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation);
  144. var slotIndex = skeletonData.FindSlotIndex(slotName);
  145. Skin skin = skeletonData.DefaultSkin;
  146. if (skinName != "")
  147. skin = skeletonData.FindSkin(skinName);
  148. skin.SetAttachment(slotIndex, att.Name, att);
  149. return att;
  150. }
  151. }
  152. }