- Home /
How to change the sprite and the animator in script to make a character selection?
Hello everyone,
I need your help to know how to make a character selection in my game like the character selection of crossy road game.
My characters will use the same animator controler I only change the sprite and the animation when you change the character.
So please somebody know how to do this in script?
P.S:I using the unity 5.
Thanks for all.
Answer by Tomer-Barkan · Sep 28, 2015 at 03:23 PM
Up to 5.1 you are not able to change animations in an animation controller during runtime. I think I read they might have added this capability in 5.2, but I'm not sure.
Anyway, the alternative, which might even be cleaner, is to use AnimationOverrideController. You can create these objects with right click in your project, and they are simple objects in which you select an original animation controller, and then for each animation of the original controller you provide a different animation (or leave empty to use the original).
This allows you to set an animator controller for selected objects while keeping all the state machine logic of the original controller. The replacement animation will have all the "selected" sprites. Then in runtime, all you have to do is switch one animation controller from the original to the selected version, instead of replacing all the animations from code.
Hey $$anonymous$$er is exactly i waht i need, but i have some trouble to implements in the script, please can you tell me how the animator override controler knows how to substitute example "run1" for "run2" and not "run1" for "jump2" i didn´t fount this in the documentation
Answer by retry_entry · Oct 18, 2017 at 10:30 AM
There is a better solution. Use this script - https://github.com/RetryEntry/UnityAnimatorChangeSprite Please give thumbs up, cause I have spent two days searching through useless solutions like one that soulburner suggected. If you have 15 gameobjects with 15 sprites in animation chain you will spend up to 225 loops every frame searching for sprites to swap to... With such approach you CAN'T create for instance charactes with different clothes animated by one animator, cause you will spend 12000 loops every second on useless things like searching. With this script you will not spend time... It uses shaders. You can change it as you'l like, Just star please. I have spent two days... :/
Yo, this is nice :) i had to tinker with it a little bit to work but the solution works very well.
(i removed everything from the awake funktion as i dont need it and also added a public field shader so i dont need to place it into a resource folder.)
Well done!
edit: for those interested, here is my customised script (sorry i dont get the code to be displayed appropriately):
public class SwappedTextures : $$anonymous$$onoBehaviour {
public Texture2D swappedTexture = null;
public Shader swapShader;
private SpriteRenderer renderer;
private void Awake()
{
renderer = GetComponent<SpriteRenderer>();
}
public void SwapTexture(Texture2D texture)
{
swappedTexture = texture;
$$anonymous$$aterial new$$anonymous$$aterial = new $$anonymous$$aterial(swapShader);
renderer.material = new$$anonymous$$aterial;
renderer.material.SetTexture("_$$anonymous$$ainTex2", swappedTexture);
}
public void Update()
{
if (swappedTexture != null)
{
SwapTexture(swappedTexture);
swappedTexture = null;
}
}
}