• RuntimesUnity
  • Runtime generated attachments & skins in Unity.

Almost have this working... The code is very similar to what I've done in spine_c and rust.

This is a test:

            var skeletonAsset = handle.Result;
            var skeletonAnimation = SkeletonAnimation.AddToGameObject(characterGO, skeletonAsset);
            skeletonAnimation.Initialize(true);
            // skeletonAnimation.AnimationState.SetAnimation(0, "idle", true);

            var attachmentRegion = skeletonAsset.atlasAssets[0].GetAtlas().FindRegion("human_body_01");
            Assert.IsNotNull(attachmentRegion);
            Debug.Log("region: " + attachmentRegion.name);

            var regionAttachment = new RegionAttachment("human_body_01")
            {
                Region = attachmentRegion,
                Width = 1f,
                Height = 1f,
                X = 0f,
                Y = 0f,
                ScaleX = 1f,
                ScaleY = 1f,
                Rotation = 0f
            };
            regionAttachment.UpdateRegion();

            var skeleton = skeletonAnimation.Skeleton;
            var slot = skeleton.FindSlot("body");
            Assert.IsNotNull(slot);
            var slotIndex = slot.Data.Index;
            Debug.Log("Body slot index: " + slotIndex);
            var characterSkin = new Skin("character-base");
            characterSkin.SetAttachment(slotIndex, "human_body_01", regionAttachment);
            skeleton.SetSkin(characterSkin);
            skeleton.SetSlotsToSetupPose();
            skeletonAnimation.AnimationState.Apply(skeleton);
            skeleton.UpdateWorldTransform(Skeleton.Physics.Update);

There are no errors, but the new region attachment doesn't appear. I know the character is there due to a reference region attachment that's exported which shows up.

Is there a step I'm missing? Maybe re-generating the skeleton data in some way?

Related Discussions
...

It seems like you are on the right track with creating and setting a new region attachment in Unity using Spine. However, there are a few things you might want to check or try to ensure that the attachment appears correctly:

  1. Ensure the Region is Correctly Set: Make sure that the attachmentRegion is correctly set and that it matches the region name in your atlas. Double-check the atlas to ensure the region name is exactly "human_body_01".

  2. Attachment Size and Position: The Width, Height, X, and Y properties of the RegionAttachment should be set to match the size and position of the region in the atlas. If these values are incorrect, the attachment might not appear where you expect it.

  3. Slot and Attachment Names: Ensure that the slot name "body" and the attachment name "human_body_01" are correct and match those defined in your Spine project.

  4. Repacking Skins: If you are combining skins or using multiple materials, consider using the Skin.GetRepackedSkin() method to repack the skin into a single texture, which can help with rendering issues.

  5. Set Slots to Setup Pose: You are already calling SetSlotsToSetupPose(), which is good. This ensures that the slots are reset to their setup pose, which should display the new attachment.

  6. Apply Animation State: You are applying the animation state, which is also correct. This ensures that any animations are applied to the skeleton.

If the attachment still doesn't appear, you might want to check if there are any other factors in your scene or setup that could be affecting the visibility of the attachment, such as camera settings, layer visibility, or other rendering issues.

One thing I discovered is

            var regionAttachment = attachmentRegion.ToRegionAttachment("human_body_01");

The AttachmentRegionExtensions. However, it doesn't change the visibility.

Adding repacking shows the "repacked skin" contains the correct region! Progress! There's some conflict on PMA settings I'll have to figure out later, but I still don't see the new quad.

The Mesh info says 2 triangles, so it looks like the quad isn't being generated for the new attachment, only the reference one that's exported in a different slot.

  • Изменено

Oh! Is the problem that region attachments only map to existing meshes? Do I need to create my own mesh for the new attachment? -- Ah, no, I see the LateUpdate mesh gen code. Should do it automatically...

I was overthinking it. No need to generate a skin. I just needed to set the attachment directly on the slot. Works perfectly!