Access portrait image in C# code

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Franky
Posts: 5
Joined: Fri Mar 24, 2017 10:16 pm

Access portrait image in C# code

Post 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?
User avatar
Tony Li
Posts: 22061
Joined: Thu Jul 18, 2013 1:27 pm

Re: Access portrait image in C# code

Post 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.
Post Reply