Exemplo n.º 1
0
        void Start()
        {
            var skeletonAnimation = GetComponent <SkeletonAnimation>();
            var skeleton          = skeletonAnimation.Skeleton;

            // All attachment changes will be applied to the skin. We use a clone so other instances will not be affected.
            var newSkin = skeleton.UnshareSkin(true, false, skeletonAnimation.AnimationState);

            // Case 1: Create an attachment from an atlas.
            RegionAttachment newHand = handSource.GetAtlas().FindRegion(handRegion).ToRegionAttachment("new hand");

            newHand.SetPositionOffset(newHandOffset);
            newHand.Rotation = newHandRotation;
            newHand.UpdateOffset();
            int handSlotIndex = skeleton.FindSlotIndex(handSlot);

            handTexture = newHand.GetRegion().ToTexture();
            newSkin.AddAttachment(handSlotIndex, handAttachmentName, newHand);

            // Case 2: Create an attachment from a Unity Sprite (Sprite texture needs to be Read/Write Enabled in the inspector.
            RegionAttachment newWeapon = dagger.ToRegionAttachmentPMAClone(Shader.Find("Spine/Skeleton"));

            newWeapon.SetScale(1.5f, 1.5f);
            newWeapon.UpdateOffset();
            int weaponSlotIndex = skeleton.FindSlotIndex(weaponSlot);

            newSkin.AddAttachment(weaponSlotIndex, daggerName, newWeapon);

            // Case 3: Change an existing attachment's backing region.
            if (applyHeadRegion)
            {
                AtlasRegion spineBoyHead  = headSource.GetAtlas().FindRegion(headRegion);
                int         headSlotIndex = skeleton.FindSlotIndex(headSlot);
                var         newHead       = newSkin.GetAttachment(headSlotIndex, headAttachmentName).GetClone(true);
                newHead.SetRegion(spineBoyHead);
                newSkin.AddAttachment(headSlotIndex, headAttachmentName, newHead);
            }

            // Case 4: Repacking a mixed-and-matched skin to minimize draw calls.
            // Repacking requires that you set all source textures/sprites/atlases to be Read/Write enabled in the inspector.
            if (repack)
            {
                newSkin = newSkin.GetRepackedSkin("repacked", repackedShader, out runtimeMaterial, out runtimeAtlas);
            }

            skeleton.SetSkin(newSkin);
            skeleton.SetToSetupPose();
            skeleton.SetAttachment(weaponSlot, daggerName);

            Resources.UnloadUnusedAssets();
        }