Variants generator

Clone an animated character into multiple variants

Variants generator - Characters aligned

EngineUnity
LanguageC#

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_RedCharacter_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

Variants generator - Initial character

Base character (blue variant)

Variants generator - Menu item

Generate Character Variants menu item

Variants generator - Characters aligned

Generated characters with associated prefabs below

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 with Red_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.
Green variant animation

Green variant animation

Red variant animation

Red variant animation

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.