SpriteAttacher.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. // Original Contribution by: Mitch Thompson
  31. using UnityEngine;
  32. using System.Collections.Generic;
  33. using Spine.Unity.Modules.AttachmentTools;
  34. namespace Spine.Unity.Modules {
  35. public class SpriteAttacher : MonoBehaviour {
  36. public const string DefaultPMAShader = "Spine/Skeleton";
  37. public const string DefaultStraightAlphaShader = "Sprites/Default";
  38. #region Inspector
  39. public bool attachOnStart = true;
  40. public bool overrideAnimation = true;
  41. public Sprite sprite;
  42. [SpineSlot] public string slot;
  43. #endregion
  44. #if UNITY_EDITOR
  45. void OnValidate () {
  46. var skeletonComponent = GetComponent<ISkeletonComponent>();
  47. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  48. bool apma;
  49. if (skeletonRenderer != null) {
  50. apma = skeletonRenderer.pmaVertexColors;
  51. } else {
  52. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  53. apma = skeletonGraphic != null && skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  54. }
  55. if (apma) {
  56. try {
  57. sprite.texture.GetPixel(0, 0);
  58. } catch (UnityException e) {
  59. 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);
  60. UnityEditor.EditorGUIUtility.PingObject(sprite.texture);
  61. throw e;
  62. }
  63. }
  64. }
  65. #endif
  66. RegionAttachment attachment;
  67. Slot spineSlot;
  68. bool applyPMA;
  69. static Dictionary<Texture, AtlasPage> atlasPageCache;
  70. static AtlasPage GetPageFor (Texture texture, Shader shader) {
  71. if (atlasPageCache == null) atlasPageCache = new Dictionary<Texture, AtlasPage>();
  72. AtlasPage atlasPage;
  73. atlasPageCache.TryGetValue(texture, out atlasPage);
  74. if (atlasPage == null) {
  75. var newMaterial = new Material(shader);
  76. atlasPage = newMaterial.ToSpineAtlasPage();
  77. atlasPageCache[texture] = atlasPage;
  78. }
  79. return atlasPage;
  80. }
  81. void Start () {
  82. // Initialize slot and attachment references.
  83. Initialize(false);
  84. if (attachOnStart)
  85. Attach();
  86. }
  87. void AnimationOverrideSpriteAttach (ISkeletonAnimation animated) {
  88. if (overrideAnimation && isActiveAndEnabled)
  89. Attach();
  90. }
  91. public void Initialize (bool overwrite = true) {
  92. if (overwrite || attachment == null) {
  93. // Get the applyPMA value.
  94. var skeletonComponent = GetComponent<ISkeletonComponent>();
  95. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  96. if (skeletonRenderer != null)
  97. this.applyPMA = skeletonRenderer.pmaVertexColors;
  98. else {
  99. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  100. if (skeletonGraphic != null)
  101. this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  102. }
  103. // Subscribe to UpdateComplete to override animation keys.
  104. if (overrideAnimation) {
  105. var animatedSkeleton = skeletonComponent as ISkeletonAnimation;
  106. if (animatedSkeleton != null) {
  107. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  108. animatedSkeleton.UpdateComplete += AnimationOverrideSpriteAttach;
  109. }
  110. }
  111. spineSlot = spineSlot ?? skeletonComponent.Skeleton.FindSlot(slot);
  112. Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
  113. attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
  114. }
  115. }
  116. void OnDestroy () {
  117. var animatedSkeleton = GetComponent<ISkeletonAnimation>();
  118. if (animatedSkeleton != null)
  119. animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
  120. }
  121. /// <summary>Update the slot's attachment to the Attachment generated from the sprite.</summary>
  122. public void Attach () {
  123. if (spineSlot != null)
  124. spineSlot.Attachment = attachment;
  125. }
  126. }
  127. public static class SpriteAttachmentExtensions {
  128. [System.Obsolete]
  129. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  130. return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA, rotation: rotation);
  131. }
  132. [System.Obsolete]
  133. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true, float rotation = 0f) {
  134. return skeletonData.AddUnitySprite(slotName, sprite, skinName, Shader.Find(shaderName), applyPMA, rotation: rotation);
  135. }
  136. [System.Obsolete]
  137. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, Shader shader, bool applyPMA, float rotation = 0f) {
  138. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation: rotation);
  139. skeleton.FindSlot(slotName).Attachment = att;
  140. return att;
  141. }
  142. [System.Obsolete]
  143. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA, float rotation = 0f) {
  144. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader, rotation: rotation) : sprite.ToRegionAttachment(new Material(shader), rotation);
  145. var slotIndex = skeletonData.FindSlotIndex(slotName);
  146. Skin skin = skeletonData.DefaultSkin;
  147. if (skinName != "")
  148. skin = skeletonData.FindSkin(skinName);
  149. skin.AddAttachment(slotIndex, att.Name, att);
  150. return att;
  151. }
  152. }
  153. }