Page 1 of 1

Access portrait image in C# code

Posted: Wed Sep 13, 2017 8:05 pm
by Franky
Hi,

I need to access the actor's portrait from code, but I'm not sure how it's stored in the database. Getting the Portraits field as a sprite array doesn't work. I perused the documentation, but couldn't find anything. How are the portraits stored and how can I access them?

Re: Access portrait image in C# code

Posted: Wed Sep 13, 2017 8:19 pm
by Tony Li
Hi!

The DialogueDatabase class has a list of actors. Each Actor has a portrait and an array of alternatePortraits which are Texture2Ds.

Let's say you want to get the first alternate portrait for an actor named "Bob". (You can tell a dialogue entry to show this portrait by using the [pic=2] tag.) You can use this code:

Code: Select all

var bob = DialogueManager.MasterDatabase.GetActor("Bob");
var pic2 = bob.alternatePortraits[0];
Note that these are Texture2Ds, which is the native format used by legacy Unity GUI, 2D Toolkit, NGUI, etc. If you're using Unity UI, you might want to get a Sprite instead by using this code:

Code: Select all

var sprite = UITools.CreateSprite(pic2);
PixelCrushers.DialogueSystem.UITools.CreateSprite() caches the value, so it will only actually create a new Sprite the first time you call it for a given Texture2D. After that, it will return the cached value.