- Home /
How to animate 2d characater Avatar (Portrait) whit dynamic changes.
I'm implementing a dialogue system in unity 5. The system shows:
Avatar/Portrait of the character (UI. Image)
Text
...
Like this:
My problem is with the Avatar of the player.
At this point I manage to dynamically change the avatar depending of whom says the dialogue. I created a XML with the text and who says what. So, if in the XML Character1 has text, I will change the sprite of the UI. Image (SourceSprite) of the Canvas with the corresponding sprite (in this case the one from Character1). So far so good.
Now, I want to make pre-made animation for those sprites. For example, Charcater1 moving the lips. Character2 moving eyes and so on.
I would like to have a modular approach.
I tried to do the following:
In the UI.Image CharAvatar (which is part of a Canvas and all the related components) I added an Animator component.
I did the animation using the sequence of sprites (moving lips, for example): Sprite1, Sprite2, Sprite3. This generates an AnimationController that is attached to that instance of the Sprites that I putted in. Notice that that Image component used for the animation is not the CharAvatar Image component (of my dialogue system), because I can have X amount of different avatars. I can't attach the specific moving lips animation, for example, to that placeholder CharAvatar Image. If the dialogue changes character i would have a different animation (in a different controller, etc.)
In code, when a new character appears, assign to the Animator the correspondent Controller:
myAnimator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("CharactersAnimations/Character1/Charcater1Controller") as RuntimeAnimatorController; //I saved them in the Resoruces folder
Is not working because the Controller is associated to the specific instance of the Sprite. Notice that is not the Sprite alone (is not possible to add a component to the Sprite alone). I need to create an object in Unity, them put the Sprite whit a Sprite Render component or an Image component with all the canvas components associated and then do the animation.
So, to make it work I would have to assign all that GameObject to the canvas somehow (with all the components). As you can see is not good.
Would like to ask for a better solution.
Thanks
Your answer
Follow this Question
Related Questions
Playing a different sprite prefab on collission. 1 Answer
Creating avatar and avatar masks for 'non-fbx' in the editor 1 Answer
Changing sprites with animations 0 Answers
Switching sprite sheets at runtime using Unityscript 0 Answers
Changing how my character looks depending on his current lifes. 2 Answers