[unity] Update SpriteAttacher example code.

This commit is contained in:
John 2017-07-17 18:48:04 +08:00 committed by GitHub
parent c11309893f
commit c6f362f6d7

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,10 +89,21 @@ 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) {
if (overrideAnimation && isActiveAndEnabled)
Attach();
}
public void Initialize (bool overwrite = true) {
if (overwrite || attachment == null) {
// Get the applyPMA value.
var skeletonComponent = GetComponent<ISkeletonComponent>(); var skeletonComponent = GetComponent<ISkeletonComponent>();
var skeletonRenderer = skeletonComponent as SkeletonRenderer; var skeletonRenderer = skeletonComponent as SkeletonRenderer;
if (skeletonRenderer != null) if (skeletonRenderer != null)
@ -101,12 +114,35 @@ namespace Spine.Unity.Modules {
this.applyPMA = skeletonGraphic.MeshGenerator.settings.pmaVertexColors; 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); Shader attachmentShader = applyPMA ? Shader.Find(DefaultPMAShader) : Shader.Find(DefaultStraightAlphaShader);
attachment = applyPMA ? sprite.ToRegionAttachmentPMAClone(attachmentShader) : sprite.ToRegionAttachment(SpriteAttacher.GetPageFor(sprite.texture, attachmentShader)); 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 {
public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true) { public static RegionAttachment AttachUnitySprite (this Skeleton skeleton, string slotName, Sprite sprite, string shaderName = SpriteAttacher.DefaultPMAShader, bool applyPMA = true) {
return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA); return skeleton.AttachUnitySprite(slotName, sprite, Shader.Find(shaderName), applyPMA);