From db36661be16177f13af33be9b73e53b9636e6f86 Mon Sep 17 00:00:00 2001 From: pharan Date: Wed, 14 Dec 2016 04:13:11 +0800 Subject: [PATCH] [unity] SpriteAttacher texture settings editor warning. --- .../Modules/AttachmentTools/SpriteAttacher.cs | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Modules/AttachmentTools/SpriteAttacher.cs b/spine-unity/Assets/spine-unity/Modules/AttachmentTools/SpriteAttacher.cs index bfcec6cda..e17622d22 100644 --- a/spine-unity/Assets/spine-unity/Modules/AttachmentTools/SpriteAttacher.cs +++ b/spine-unity/Assets/spine-unity/Modules/AttachmentTools/SpriteAttacher.cs @@ -45,11 +45,37 @@ namespace Spine.Unity.Modules { [SpineSlot] public string slot; #endregion + #if UNITY_EDITOR + void OnValidate () { + var skeletonComponent = GetComponent(); + var skeletonRenderer = skeletonComponent as SkeletonRenderer; + bool apma; + + if (skeletonRenderer != null) { + apma = skeletonRenderer.pmaVertexColors; + } else { + var skeletonGraphic = skeletonComponent as SkeletonGraphic; + apma = skeletonGraphic != null && skeletonGraphic.SpineMeshGenerator.PremultiplyVertexColors; + } + + if (apma) { + try { + sprite.texture.GetPixel(0, 0); + } catch (UnityException e) { + 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); + UnityEditor.EditorGUIUtility.PingObject(sprite.texture); + throw e; + } + } + } + #endif + RegionAttachment attachment; bool applyPMA; - Dictionary atlasPageCache = new Dictionary(); - AtlasPage GetPageFor (Texture texture, Shader shader) { + static Dictionary atlasPageCache; + static AtlasPage GetPageFor (Texture texture, Shader shader) { + if (atlasPageCache == null) atlasPageCache = new Dictionary(); AtlasPage atlasPage; atlasPageCache.TryGetValue(texture, out atlasPage); if (atlasPage == null) { @@ -76,7 +102,7 @@ namespace Spine.Unity.Modules { } Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader); - attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(GetPageFor(sprite.texture, attachmentShader)); + attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader)); skeletonComponent.Skeleton.FindSlot(slot).Attachment = attachment; } }