SpriteAttacher.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // Contributed 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 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 apma;
  48. if (skeletonRenderer != null) {
  49. apma = skeletonRenderer.pmaVertexColors;
  50. } else {
  51. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  52. apma = skeletonGraphic != null && skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  53. }
  54. if (apma) {
  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. bool applyPMA;
  67. static Dictionary<Texture, AtlasPage> atlasPageCache;
  68. static AtlasPage GetPageFor (Texture texture, Shader shader) {
  69. if (atlasPageCache == null) atlasPageCache = new Dictionary<Texture, AtlasPage>();
  70. AtlasPage atlasPage;
  71. atlasPageCache.TryGetValue(texture, out atlasPage);
  72. if (atlasPage == null) {
  73. var newMaterial = new Material(shader);
  74. atlasPage = newMaterial.ToSpineAtlasPage();
  75. atlasPageCache[texture] = atlasPage;
  76. }
  77. return atlasPage;
  78. }
  79. void Start () {
  80. if (attachOnStart) Attach();
  81. }
  82. public void Attach () {
  83. var skeletonComponent = GetComponent<ISkeletonComponent>();
  84. var skeletonRenderer = skeletonComponent as SkeletonRenderer;
  85. if (skeletonRenderer != null)
  86. this.applyPMA = skeletonRenderer.pmaVertexColors;
  87. else {
  88. var skeletonGraphic = skeletonComponent as SkeletonGraphic;
  89. if (skeletonGraphic != null)
  90. this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
  91. }
  92. Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
  93. attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
  94. skeletonComponent.Skeleton.FindSlot(slot).Attachment = attachment;
  95. }
  96. }
  97. public static class SpriteAttachmentExtensions {
  98. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true) {
  99. return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA);
  100. }
  101. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true) {
  102. return skeletonData.AddUnitySprite(slotName, sprite, skinName, Shader.Find(shaderName), applyPMA);
  103. }
  104. public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, Shader shader, bool applyPMA) {
  105. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader) : sprite.ToRegionAttachment(new Material(shader));
  106. skeleton.FindSlot(slotName).Attachment = att;
  107. return att;
  108. }
  109. public static RegionAttachment AddUnitySprite (this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA) {
  110. RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader) : sprite.ToRegionAttachment(new Material(shader));
  111. var slotIndex = skeletonData.FindSlotIndex(slotName);
  112. Skin skin = skeletonData.defaultSkin;
  113. if (skinName != "")
  114. skin = skeletonData.FindSkin(skinName);
  115. skin.AddAttachment(slotIndex, att.Name, att);
  116. return att;
  117. }
  118. }
  119. }