Howdy
So I'm not sure what changed with my artists sprites (or maybe even the spine), but all of the sudden the SpriteAttacher script that used to work no longer works correctly: it's scaling the sprites it attaches. Here is a shot side by side of the original sprite and the oversizing thats occuring now:
It seems only the width is overscaled.
I tried checking the script to see if the width of the sprite is changing, but when I look at the attachment object being added, matches the base sprites size for width.
Do you know what could be happening that's causing the sprite to be scaled when it's attached?
I tried hacking the width smaller but it didn't change anything:
public void Attach()
{
if (spineSlot != null)
{
// HACKS I TRIED THAT DIDN"T WORK
attachment.width = 80;
attachment.regionWidth = 80;
attachment.regionOriginalWidth = 80;
/// END OF HACKS
spineSlot.Attachment = attachment;
}
}
I'm using a simplified version of the original Sprite Attacher posted below:
public class SpriteAttacher : MonoBehaviour
{
public const string DefaultPMAShader = "Spine/Skeleton";
public const string DefaultStraightAlphaShader = "Sprites/Default";
#region Inspector
public bool attachOnStart = true;
public bool overrideAnimation = true;
public Sprite sprite;
[SpineSlot] public string slot;
#endregion
RegionAttachment attachment;
Slot spineSlot;
bool applyPMA;
static Dictionary<Texture, AtlasPage> atlasPageCache;
static AtlasPage GetPageFor(Texture texture, Shader shader)
{
if (atlasPageCache == null) atlasPageCache = new Dictionary<Texture, AtlasPage>();
AtlasPage atlasPage;
atlasPageCache.TryGetValue(texture, out atlasPage);
if (atlasPage == null)
{
var newMaterial = new Material(shader);
atlasPage = newMaterial.ToSpineAtlasPage();
atlasPageCache[texture] = atlasPage;
}
return atlasPage;
}
void Start()
{
// Initialize slot and attachment references.
Initialize(false);
if (attachOnStart)
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 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));
}
}
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;
}
}
private static readonly Dictionary<T_EquipSlots, string> _SLOT_TYPE_TO_NAME = new Dictionary<T_EquipSlots, string>
{
{T_EquipSlots.ARMOR, "armor"},
{T_EquipSlots.HELMET, "helmet"},
{T_EquipSlots.OFF_HAND, "offHand"},
{T_EquipSlots.MAIN_HAND, "mainHand"},
};
public void Attach(Sprite sprite, T_EquipSlots equipSlot)
{
var skeletonComponent = GetComponent<ISkeletonComponent>();
Shader attachmentShader = Shader.Find(DefaultStraightAlphaShader);
attachment = sprite.ToRegionAttachmentPMAClone(attachmentShader);
skeletonComponent.Skeleton.FindSlot(_SLOT_TYPE_TO_NAME[equipSlot]).Attachment = attachment;
}
Upon further inspection, it looks like its the alpha on the texture. The artist added some alpha padding to the left and right, and the SpriteAttacher is Scaling the image to fit that padding.
Im curious if its the shader doing this. Any suggestions as to what is causing the image to stretch into its alpha space?
No suggestions? 🙁