Change Image.sprite with alternatePortraits

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
hapciupalit
Posts: 24
Joined: Tue Nov 28, 2017 10:04 am

Change Image.sprite with alternatePortraits

Post by hapciupalit »

I think my title it's quite intuitive. I'm trying to change the sprite of an Image with one of the alternate portraits.
I really have no idea how to do this, I tried several solutions, but none of them worked out. I would really need your help.

What I'm trying to do?
I'm trying to create a Dialog over the conversation where I present the character for the first time when the player interact with the NPC. For now I managed to do a custom function and I'm able to set some of the fields with

Code: Select all

DialogueLua.GetActorField(actor.Name,"___").AsString
however in the case of the picture I'm really stucked.
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change Image.sprite with alternatePortraits

Post by Tony Li »

Hi,

For the portrait name, use the actor's Display Name.

For example, let's say you have an actor "Protagonist". When his identity is unknown, the portrait should show "Dread Pirate Roberts". When his identity is known, the portrait should show "Wesley".

To do this, tick Use Display Name. Then set the Display Name field to "Dread Pirate Roberts":

Image

For the portrait image, assign images to the Portraits section. Assign his default image first. Then assign his mysterious image as #2.

In the conversation below, look at the left node:

Image

The Dialogue Text uses "[pic=2]" to show image #2 when his identity is unknown. The [pic=#] tag overrides the default image only for this dialogue entry node. If you want to permanently change the default image, use the SetPortrait() sequencer command instead.

When you want to change the actor's Display Name, set it as shown in the right node above. You can use the "..." dropdown menu. You don't have to type it manually.

(I definitely do not own the copyright on the portrait images above. ;) )
hapciupalit
Posts: 24
Joined: Tue Nov 28, 2017 10:04 am

Re: Change Image.sprite with alternatePortraits

Post by hapciupalit »

Thank you for the info, it will sure come in handy, but I'm trying to create something else.
I created a custom script to create a modal with some descriptions.
Image
So I got this modal that comes over the conversation whenever I meet a person for the first time in the game. For our hero it's a known person, someone who he knows for some time, but for the player it's not. So I show this modal on top of the conversation just to bring the player in the picture of the game. So I managed to get from my database :
- name
- age
- occupation
- about
using

Code: Select all

DialogueLua.GetActorField
, but I have no idea how to get the picture from the alternate portraits. In my case it will be portrait 11/12.

Thanks
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change Image.sprite with alternatePortraits

Post by Tony Li »

Hi,

In a script, you can also use DialogueLua.GetActorField(). For example:

Code: Select all

int age = DialogueLua.GetActorField("Protagonist", "Age").AsInt;
To get the player's portrait, you'll need to access the actor entry in the dialogue database:

Code: Select all

Actor actor = DialogueManager.MasterDatabase.GetActor("Protagonist");
Texture2D portrait11 = actor.alternatePortraits[9]; // Fixed typo.
API References:
hapciupalit
Posts: 24
Joined: Tue Nov 28, 2017 10:04 am

Re: Change Image.sprite with alternatePortraits

Post by hapciupalit »

Well... I tried this before creating the post and I was getting the
"`PixelCrushers.DialogueSystem.Actor.alternatePortraits' is inaccessible due to its protection level" error. So I taught that maybe I'm doing something wrong. Do you have any idea what could cause this?
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change Image.sprite with alternatePortraits

Post by Tony Li »

Hi,

I had a typo above: "(9)" instead of "[9]". But actor.alternatePortraits is a public list, so you should be able to read it:

Code: Select all

Texture2D portrait11 = actor.alternatePortraits[9];
hapciupalit
Posts: 24
Joined: Tue Nov 28, 2017 10:04 am

Re: Change Image.sprite with alternatePortraits

Post by hapciupalit »

Thank you. I manage to get the Texture, now I only need to find the way to put it on an Image Object, but it should not be a problem.
So thank you very much.
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change Image.sprite with alternatePortraits

Post by Tony Li »

Use UITools.CreateSprite(Texture2D). This will return a Sprite version of a Texture2D. (It doesn't create a new Sprite each time. It's more efficient than that. It caches the Sprite for re-use.)

Example:

Code: Select all

Texture2D portrait11 = actor.alternatePortraits[9];
GetComponent<UnityEngine.UI.Image>().sprite = UITools.CreateSprite(portrait11);
hapciupalit
Posts: 24
Joined: Tue Nov 28, 2017 10:04 am

Re: Change Image.sprite with alternatePortraits

Post by hapciupalit »

Thanks for the help, Tony
Have a nice day
Post Reply