Background:
- I am using sprite sheets (GitHub - makrohn/Universal-LPC-spritesheet: An attempt to merge most character assets generated by the Liberated Pixel Cup into a single .xcf, where they can be mixed and matched.)
- There is a Body Sheet and then lots of hair, eyes, clothings, weapons sheets
- I do NOT want to manually overlay the sheets in Gimp, save lots of spritesheets (Goblin_Unarmed, Goblin_WithSword) and then attach those sheets to gameObjects in the editor.
- I DO want to (during runtime) build a character using those sheets.
What I am trying (and failing)
- In C# I can load the sprite sheets I want (Body_1, Eyes_1,Armor_12,etc…) and merge then all together as a new PNG file. I can manage the alpha transparency as well as layering them correctly. This gives me a new spritesheet that is exactly what I want.
- I TRIED to load the PNG as a new texture, splice it into 64x64 array and then change the Player GameObject Animators to use the new sprite.
- I simply can’t get it to work, I can load the texture, but I can’t figure out the Texture Importer on the newly created file (remember its new, so its not in Resources)
- Even if I got that working, I am not sure how to update the Sprites in memory loaded into the Player and the Players Animators.
Question:
How do other game creators do this? Imagine I have 1000’s of combinations of items and I want to let people customize their avatars. I want to lets mobs pick up weapons off the ground and equip armor. I don’t want 10000’s of variants of characters in the Resources and then load them when needed?
Ideas?
**Update 2 **
Damn, the code below works great, BUT, it requires UnityEditor which means no making builds Sigh back to the drawing board.
Update
I stumbled onto one key bit of the puzzle.
I figured out a (poor performance way) of changing a sliced sprite on the disk and updating it runtime.
Steps
- [UnityEditor] Add a SpritePage.PNG to a Resouces Folder
- [UnityEditor] Set the SpriteMod to Multiple and Splice it however you want.
- [FileSystem] Notice you now have the original PNG and a new .meta file.
- [UnityEditor] Drag one of the Sprite items onto your Scene (make sure you can see it) and rename to Player.
- [UnityEditor] Add a new script to that Player Object in the Scene
- [Script] Paste in the update() method below
- [GAME] Run the game
- [FileSystem] Copy and Replace the PNG on the filesystem with a new PNG that is a differnt spritesheet (obviously same dimensions)
- [GAME] Press the Space Bar (that’s the key that triggers the change)
- [SUCCESS] Notice that there is a game pause while the texture is updated, the Sprite automatically changes on the Player Object.
Animation Sprite Change Verified
I verified that this works with Animator using the sprites on the Player Objects. Look at this guys sprite animation tutorial. I am using his method to animate, then my method to swap avatar.
What this means
Imaging that we simply add ONE sprite sheet (called PCAppearance) for the player (naked). Each time the player changes their appearance, we use C# to take 15 PNGs (not referenced inside Unity) and merge them together (bosy, hair, eyes, shoes, weapons, armor) and save that ‘over the top of’ of the current PCAppearence.png. Then run the script below to update it all across the game.
void Update () {
if (Input.GetKeyDown ("space"))
{
Debug.Log("[SPACE] Reload Texture");
Texture2D myTexture = (Texture2D)AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Resources/Sprites/PCAppearance.png");
string path = AssetDatabase.GetAssetPath(myTexture);
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
ti.isReadable = true;
EditorUtility.SetDirty(ti);
ti.SaveAndReimport();
}
}