Looking at your issue, the problem is likely related to timing — when you call MatchRectTransformWithBounds() immediately after setting the skin, the mesh hasn't been updated yet, so the bounds are either zero or incorrect, causing the skeleton to disappear or scale incorrectly.
Here's what's happening step by step:
- You set the skin and call
SetupPoseSlots() ✓
- You call
UpdateWorldTransform() ✓
- You call
MatchRectTransformWithBounds() — but at this point the SkeletonGraphic mesh hasn't been regenerated yet, so the bounds are stale or empty.
The fix is to force the SkeletonGraphic to update its mesh before calling MatchRectTransformWithBounds(). You can do this by calling sg.UpdateMesh() first:
public static void FitToRect(this SkeletonGraphic sg) {
sg.Skeleton.UpdateWorldTransform(Physics.Pose);
sg.UpdateMesh(); // Force mesh update so bounds are correct
sg.MatchRectTransformWithBounds();
sg.rectTransform.pivot = new Vector2(0.5f, 0f);
}
And your SetSkinAndFitToRect should also make sure the animation state is applied before fitting, to ensure attachments from the current animation are reflected in the bounds:
public static void SetSkinAndFitToRect(this SkeletonGraphic sg, Skin skin) {
sg.Skeleton.SetSkin(skin);
sg.Skeleton.SetSlotsToSetupPose();
sg.AnimationState.Apply(sg.Skeleton); // Apply current animation state
sg.FitToRect();
}
Note the use of SetSlotsToSetupPose() instead of SetupPoseSlots() — these are equivalent but make sure you're using the correct one for your runtime version.
Also, if you're setting the skin before the SkeletonGraphic has been fully initialized (e.g. in Awake or before the first frame), you may need to defer the call using a coroutine:
public static IEnumerator SetSkinAndFitToRectNextFrame(this SkeletonGraphic sg, Skin skin) {
sg.Skeleton.SetSkin(skin);
sg.Skeleton.SetSlotsToSetupPose();
sg.AnimationState.Apply(sg.Skeleton);
sg.UpdateMesh();
yield return null; // Wait one frame
sg.MatchRectTransformWithBounds();
sg.rectTransform.pivot = new Vector2(0.5f, 0f);
}
As a general rule for skin changes on SkeletonGraphic, the safe sequence is:
skeleton.SetSkin(newSkin)
skeleton.SetSlotsToSetupPose()
animationState.Apply(skeleton)
sg.UpdateMesh()
sg.MatchRectTransformWithBounds()
This ensures all attachments are resolved and the mesh is up to date before the bounds are measured.