This commit is contained in:
badlogic 2017-07-17 14:18:33 +02:00
commit c40df2367c

View File

@ -28,7 +28,7 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
// Contributed by: Mitch Thompson // Original Contribution by: Mitch Thompson
using UnityEngine; using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
@ -41,6 +41,7 @@ namespace Spine.Unity.Modules {
#region Inspector #region Inspector
public bool attachOnStart = true; public bool attachOnStart = true;
public bool overrideAnimation = true;
public Sprite sprite; public Sprite sprite;
[SpineSlot] public string slot; [SpineSlot] public string slot;
#endregion #endregion
@ -71,6 +72,7 @@ namespace Spine.Unity.Modules {
#endif #endif
RegionAttachment attachment; RegionAttachment attachment;
Slot spineSlot;
bool applyPMA; bool applyPMA;
static Dictionary<Texture, AtlasPage> atlasPageCache; static Dictionary<Texture, AtlasPage> atlasPageCache;
@ -87,24 +89,58 @@ namespace Spine.Unity.Modules {
} }
void Start () { void Start () {
if (attachOnStart) Attach(); // Initialize slot and attachment references.
Initialize(false);
if (attachOnStart)
Attach();
} }
public void Attach () { void AnimationOverrideSpriteAttach (ISkeletonAnimation animated) {
var skeletonComponent = GetComponent<ISkeletonComponent>(); if (overrideAnimation && isActiveAndEnabled)
var skeletonRenderer = skeletonComponent as SkeletonRenderer; Attach();
if (skeletonRenderer != null) }
this.applyPMA = skeletonRenderer.pmaVertexColors;
else { public void Initialize (bool overwrite = true) {
var skeletonGraphic = skeletonComponent as SkeletonGraphic; if (overwrite || attachment == null) {
if (skeletonGraphic != null) // Get the applyPMA value.
this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors; var skeletonComponent = GetComponent<ISkeletonComponent>();
var skeletonRenderer = skeletonComponent as SkeletonRenderer;
if (skeletonRenderer != null)
this.applyPMA = skeletonRenderer.pmaVertexColors;
else {
var skeletonGraphic = skeletonComponent as SkeletonGraphic;
if (skeletonGraphic != null)
this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors;
}
// Subscribe to UpdateComplete to override animation keys.
if (overrideAnimation) {
var animatedSkeleton = skeletonComponent as ISkeletonAnimation;
if (animatedSkeleton != null) {
animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
animatedSkeleton.UpdateComplete += AnimationOverrideSpriteAttach;
}
}
spineSlot = spineSlot ?? skeletonComponent.Skeleton.FindSlot(slot);
Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
} }
Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader));
skeletonComponent.Skeleton.FindSlot(slot).Attachment = attachment;
} }
void OnDestroy () {
var animatedSkeleton = GetComponent<ISkeletonAnimation>();
if (animatedSkeleton != null)
animatedSkeleton.UpdateComplete -= AnimationOverrideSpriteAttach;
}
/// <summary>Update the slot's attachment to the Attachment generated from the sprite.</summary>
public void Attach () {
if (spineSlot != null)
spineSlot.Attachment = attachment;
}
} }
public static class SpriteAttachmentExtensions { public static class SpriteAttachmentExtensions {