Variants generator
Clone an animated character into multiple variants
Engine | Unity |
---|---|
Language | C# |
Description
I created a menu command to clone a selected base character object into 4 color variants, changing the name, script parameters and animation sprites of each variant accordingly. A prefab is created for each variant, with the color name appended.
For instance, a game object named Character_Blue
will be cloned into 3 prefabs Character_Red
, Character_Yellow
and Character_Green
. Their character data will point to the corresponding colors and player numbers, and their sprite animations will match their colors.
Screenshots
Process
The script execution is done is 3 steps:
- Get resource spritesheets for each variant. Each spritesheet is imported as a Unity Multiple Sprite asset where each sprite is rigorously named, e.g.
Blue_Walk_Down_1
. - For each variant, create copies of the animations of the base character, replacing each sprite reference at each key frame with the corresponding variant, e.g. on frame 1 of animation Walk Down of the Red variant, replace
Blue_Walk_Down_1
withRed_Walk_Down_1
- For each variant, create an Animator Override Controller that replaces each animation reference with the corresponding variant animation created in the previous step.
Context
I wrote the script because each time I was modifying one of the game characters, I wanted to propagate the changes on other characters. Since they were all using the same scripts but differed by their color and player number, I decided to create a command to generate 3 variants from one.
Update
Since Unity 2018.3, the new Prefab workflow allows to create Prefab variants, which covers most of my scripts’ purposes. The part that generates animations from sprite variants is still useful, although I now consider it easier to name all the sub-sprites identically in each variant (instead of prefixing them with the Color name), then swap the entire Multiple Sprite asset for each variant.
References