Changing Backgrounds for different NPC conversations

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
ericlevalley
Posts: 4
Joined: Sun Feb 11, 2024 9:28 am

Changing Backgrounds for different NPC conversations

Post by ericlevalley »

I'm sorry if this is a dumb question but I'm trying to figure out how to change the background image depending on who the player is talking to.

As you can see in the attached images, the background is blue because I put a blue image titled "Background Color" in the dialogue panel.

So how could I make it switch to a different color or image when talking to somebody else? Would I have to use a completely different UI every time and if so, how??
Attachments
panel1.png
panel1.png (20.82 KiB) Viewed 204 times
Last edited by ericlevalley on Sun Feb 11, 2024 12:04 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Backgrounds for different NPC conversations

Post by Tony Li »

Hi,

Here are two ways you could do that:

1. Make the portrait image UI element cover the entire screen. Incorporate the actor's background into the portrait image. This is relatively simple to set up, but it only works if the actor always has the same background.

2. Or add the backgrounds to the Dialogue Manager's Canvas. Give them unique GameObject names such "Rosi Background" and "Diego Background". Set them all inactive. You can use SetActive() sequencer commands in your dialogue entries to activate a background, such as: SetActive(Rosi Background) -- or you can automate it like this:

a. Add a custom field such as "Background" to each actor (e.g., using the Dialogue Editor's Templates section) and set it to the name of the actor's background image (e.g., "Rosi Background").

b. Add a script to the Dialogue Manager that has an OnConversationLine(Subtitle) method. Something like:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
public class ActorBackgroundImageManager : MonoBehaviour
{
    private GameObject currentBackgroundGameObject = null;
    
    void OnConversationLine(Subtitle subtitle)
    {
        if (string.IsNullOrEmpty(subtitle.formattedText.text)) return;
        
        // Look up the currently-speaking actor:
        var actor = DialogueManager.masterDatabase.GetActor(subtitle.speakerInfo.id);
        if (actor == null) return;
        
        // Get the actor's background image name:
        var actorBackgroundName = actor.LookupValue("Background");
        if (string.IsNullOrEmpty(actorBackgroundName)) return;
        
        // Find the background image GameObject:
        var actorBackgroundGameObject = GameObjectUtility.GameObjectHardFind(actorBackgroundName);
        if (actorBackgroundGameObject == null) return;
        
        // Change the background image:
        if (currentBackgroundImage != null) currentBackgroundImage.SetActive(false);
        actorBackgroundGameObject.SetActive(true);
        currentBackgroundImage = actorBackgroundGameObject;
    }
}
Technically these GameObjects don't need to be in the Dialogue Manager's hierarchy. If they're only used in one scene, you can put them in a canvas in that scene. Make sure that canvas's Sort Order is lower than the dialogue UI's sort order so the background image will appear behind the dialogue UI.
ericlevalley
Posts: 4
Joined: Sun Feb 11, 2024 9:28 am

Re: Changing Backgrounds for different NPC conversations

Post by ericlevalley »

Wow thank you so much for the detailed and quick response! This helps a ton!
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing Backgrounds for different NPC conversations

Post by Tony Li »

Glad to help!
Post Reply