arenos-nexus/Arenos Nexus/Library/PackageCache/com.unity.2d.animation@34e0443c58ed/Documentation~/ex-runtime-swap.md
Daniel 2e704cae70 init
Init Commit Unity
2025-09-25 22:01:28 +02:00

1.7 KiB

Runtime Swap

This sample demonstrates how you can use the Sprite Library API to override a specific Entry. Note that the sample requires the PSD Importer installed. Open the 6 Runtime Swap Scene to see the sample in action.

A knight character, and a selection of alternate sprites for his head, arms, and legs that convert him into a skeleton. The graphic Assets are located in Assets/Samples/2D Animation/[X.Y.Z]/Samples/5 SpriteSwap/Sprites:

  • Knight.psb
  • Skeleton.psb

The Skeleton.psb uses the .skeleton Asset from the Knight.psb for its rigging. It also references the Knight.spriteLib Sprite Library Asset located in Assets/Samples/2D Animation/[X.Y.Z]/Samples/5 SpriteSwap/Sprites.

Runtime Swap script

A custom MonoBehaviour script called the RuntimeSwap is attached to the KnigtboyRig GameObject. The script is located in Assets/Samples/2D Animation/[X.Y.Z]/Samples/5 SpriteSwap/Scripts/Runtime/RuntimeSwap.cs

Pressing a button (in the sample Scene) with a Sprite from the Skeleton.psb causes the script to use the override API from the Sprite Library to override that Sprite Entry.

m_SpriteLibraryTarget.AddOverride(entry.sprite, entry.category, entry.entry);

Pressing a button with a Sprite from the Knight.psb causes the script to use the override rest API from the Sprite Library to remove the Sprite Entry override.

m_SpriteLibraryTarget.RemoveOverride(entry.category, entry.entry);