[HOWTO] How To: Set Up Player-Selectable Portraits
Posted: Fri Feb 21, 2025 11:07 pm
If you allow players to select their character's portrait from a set list of sprites, you can put those sprites in a folder named "Resources".
When they choose a sprite from the list, use these two methods to set the player's portrait:
If the Dialogue Manager's Persistent Data Settings > Include All Actor Data is ticked, the Dialogue System Saver component will remember the choice in saved games.
Alternatively, you can let the player choose from the list of portrait sprites assigned to the Player actor in the Dialogue Editor. In this case, use:
---
If you want to allow players to provide their own images, loading images at runtime is outside the scope of the Dialogue System. Briefly: You'll need to load the bytes of the image into a byte[] array, create a new Texture2D object, and then call Texture2D.LoadImage() to load the bytes of the image into your new Texture2D. From there, you can use Sprite.Create() to create a Sprite object from the Texture2D.
Then you can assign the Sprite object to the actor's spritePortrait property, such as:
Note on loading images at runtime: If you're making a Standalone game (Windows, Mac, Linux), you can read the bytes directly from the file. If you're making a mobile game or Switch game, you will need to use UnityWebRequest to get the bytes.
When they choose a sprite from the list, use these two methods to set the player's portrait:
Code: Select all
DialogueLua.SetActorField(actorName, DialogueSystemFields.CurrentPortrait, sprite.name);
DialogueManager.instance.SetActorPortraitSprite(actorName, sprite);
Alternatively, you can let the player choose from the list of portrait sprites assigned to the Player actor in the Dialogue Editor. In this case, use:
Code: Select all
var sprite = playerActor.GetPortraitSprite(picNumber);
DialogueLua.SetActorField(actorName, DialogueSystemFields.CurrentPortrait, $"pic={picNumber}");
DialogueManager.instance.SetActorPortraitSprite(actorName, sprite);
If you want to allow players to provide their own images, loading images at runtime is outside the scope of the Dialogue System. Briefly: You'll need to load the bytes of the image into a byte[] array, create a new Texture2D object, and then call Texture2D.LoadImage() to load the bytes of the image into your new Texture2D. From there, you can use Sprite.Create() to create a Sprite object from the Texture2D.
Then you can assign the Sprite object to the actor's spritePortrait property, such as:
Code: Select all
var actor = DialogueManager.masterDatabase.GetActor("Player");
actor.spritePortrait = YourSprite;