Page 1 of 1
Change Image.sprite with alternatePortraits
Posted: Mon Feb 19, 2018 8:56 am
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.
Re: Change Image.sprite with alternatePortraits
Posted: Mon Feb 19, 2018 3:02 pm
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":
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:
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.
)
Re: Change Image.sprite with alternatePortraits
Posted: Wed Feb 21, 2018 1:15 am
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.
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
, but I have no idea how to get the picture from the alternate portraits. In my case it will be portrait 11/12.
Thanks
Re: Change Image.sprite with alternatePortraits
Posted: Wed Feb 21, 2018 11:46 am
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:
Re: Change Image.sprite with alternatePortraits
Posted: Thu Feb 22, 2018 3:30 am
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?
Re: Change Image.sprite with alternatePortraits
Posted: Thu Feb 22, 2018 8:45 am
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];
Re: Change Image.sprite with alternatePortraits
Posted: Fri Feb 23, 2018 1:16 am
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.
Re: Change Image.sprite with alternatePortraits
Posted: Fri Feb 23, 2018 8:04 am
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);
Re: Change Image.sprite with alternatePortraits
Posted: Fri Feb 23, 2018 10:34 am
by hapciupalit
Thanks for the help, Tony
Have a nice day