Page 1 of 1

How to set a custom avatar for each dialogue node?

Posted: Tue Jan 17, 2023 11:44 am
by yurijh
I'd like to set a custom avatar for the actor talking in a particular node.
Using the Actors list options is not viable in my situation, the best solution to me would be to have a field in the inspector where put a sprite in while selecting a node.

What do you suggest to do to obtain this?

Thank you in advance!

Re: How to set a custom avatar for each dialogue node?

Posted: Tue Jan 17, 2023 11:55 am
by Tony Li
The Portrait Sprites list in the dialogue database's Actors section is generally the best way, but since it's not viable in your specific situation you can try this:

1. Put your portrait images in a folder named Resources.

2. Use the SetPortrait() sequencer command in the dialogue entry's Sequence field.

For example, say you've put a portrait image named Yuri_Happy in a folder named "Assets/Portraits/Yuri/Resources". To show this portrait in a dialogue entry spoken by Yuri, set the dialogue entry's Sequence to:

Code: Select all

SetPortrait(Yuri, Yuri_Happy);
{{default}}  // Also play the Dialogue Manager's Default Sequence.

Re: How to set a custom avatar for each dialogue node?

Posted: Tue Jan 17, 2023 12:43 pm
by yurijh
Thanks for your answer. The problem is that I need to give the designers a comfortable way to drag an image inside the inspector relative to that node.
By the way I think I've solved in this way.

1. Created a script named SpriteEvent

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

[CreateAssetMenu(fileName = "SpriteEvent", menuName = "Scriptables/SpriteEvent")]
public class SpriteEvent : ScriptableObject
{
    public UnityEvent<Sprite> EventRaised;

    public void Raise(Sprite rSprite) {
        EventRaised?.Invoke(rSprite);
    }
}
2. Created the SpriteEvent asset in the Project tab with Right click -> Scriptables -> SpriteEvent.

3. Created a DialogueManager wrapper class in which is referenced the asset created at point 2.

Code: Select all

[SerializeField] private SpriteEvent m_rSpriteEvent;
4. Attatched a listener for the event in the Awake

Code: Select all

m_rSpriteEvent.EventRaised.AddListener(SetAvatar);
5. Handled the avatar setting in the SetAvatar function.

Now I can put the SpriteEvent asset in the Events field of the dialogue node, then choose the Raise function selecting the sprite I want.

I hope I explained myself :)

Re: How to set a custom avatar for each dialogue node?

Posted: Tue Jan 17, 2023 1:59 pm
by Tony Li
Hi,

That's fine. One thing to keep in mind, which may not be relevant to your project: OnExecute() UnityEvents are not exported if you export your dialogue database to an external text format such as Chat Mapper XML.

You can still export your dialogue database text to CSV using the Localization Export/Import feature. This will not affect OnExecute() events; they will be fine.

Re: How to set a custom avatar for each dialogue node?

Posted: Wed Jan 18, 2023 4:30 am
by yurijh
Yes, I will probably need to Import/Export in the future, so if I use CSV I won't need to set the events again after import?

Re: How to set a custom avatar for each dialogue node?

Posted: Wed Jan 18, 2023 9:21 am
by Tony Li
If you export and import for localization using the Localization Export/Import feature, you will not need to set events again.

On the other hand, if you export your entire database using Export Database > CSV, this is not compatible with events. You will need to set events again.

So make sure to use Localization Export/Import if you must export to CSV (e.g., to add translations to other languages).

Re: How to set a custom avatar for each dialogue node?

Posted: Wed Jan 18, 2023 2:41 pm
by yurijh
Ok, thanks a lot for your support!

Re: How to set a custom avatar for each dialogue node?

Posted: Wed Jan 18, 2023 2:51 pm
by Tony Li
Glad to help!