Changing SubtitlePanel background color according to speaking Actor

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
LostTrainDude
Posts: 61
Joined: Wed Mar 21, 2018 2:14 pm

Changing SubtitlePanel background color according to speaking Actor

Post by LostTrainDude »

[Unity 2019.4.25f1 - DS v2.2.16]

Hi Tony,

In the game I'm working on, I'm trying to find a way to override the Image component's color of the StandardUISubtitlePanel, much like you would override the color of the Subtitle text by adding a DialogueActor component.

I am not sure how should I proceed, though. I've been looking into the DialogueActor script to get some inspiration, but I'm getting a bit lost in the structure.

For example, I noticed that StandardUISubtitlePanelControls uses DialogueActor.AdjustSubtitleColor() to override Subtitle text color. Therefore, I assume that I need to come up with an equivalent method that does that for the Image component, as well.

Should I create derived classes for both the DialogueActor and StandardUISubtitlePanelControls? I'm not sure this is the right way to proceed (probably doesn't mean anything but I already have a derived class from StandardDialogueUI that I use for my Dialogue UIs).

One thing I have tried to do is:
  • hold the Actor\Background color combination data in a separate GameObject to refer to when needed;
  • use the DialogueSystemEventBroadcaster to send OnConversationLine \ OnPrepareConversationLine to a custom component I added to the Subtitle Panels
Of course, the Subtitle Panels don't really receive the OnConversationLine \ OnPrepareConversationLine messages, so I also tried invoking a custom event to which I subscribed the panels to, but the message seems to get broadcasted before the according Panel gets enabled, therefore the event never seems to get triggered properly.

Any advice or pointers, or am I overcomplicating this? :roll:

As always, thanks a million in advance.

EDIT: Of course I also assume I could use separate custom UIPanel prefabs for each Actor, but I wondered if I could avoid doing that!
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by Tony Li »

Hi,

To specify the background color per speaker, you could make a subclass of Dialogue Actor. (Alternatively, you can put this info in the Actor in the dialogue database.)

Code: Select all

public class MyDialogueActor : DialogueActor
{
    public Color panelColor;
}
Then make a subclass of StandardUISubtitlePanel and override the SetContent(Subtitle) method. Something like:

Code: Select all

public class MySubtitlePanel : StandardUISubtitlePanel
{
    private Image panelImage;
    private Color originalPanelColor;
    
    protected override void Start()
    {
        panelImage = GetComponent<Image>();
        originalPanelColor = panelImage.color;
    }
    
    public override void SetContent(Subtitle subtitle)
    {
        base.SetContent(subtitle);
        var myDialogueActor = subtitle.speakerInfo.transform?.GetComponent<MyDialogueActor>();
        panelImage.color = (myDialogueActor != null) ? myDialogueActor.panelColor : originalPanelColor;
    }
}
Use this subclass on your panels in place of the base StandardUISubtitlePanel.
LostTrainDude
Posts: 61
Joined: Wed Mar 21, 2018 2:14 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by LostTrainDude »

Thank you, Tony!
This should be enough to get me going.

I'll update the thread if something else happens as I implement and tweak it!
cptscrimshaw
Posts: 105
Joined: Sun Sep 20, 2020 8:21 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by cptscrimshaw »

Hi Tony,

I'm attempting to implement this custom panel color, and for whatever reason the subclass isn't showing the new public variables in the inspector. Any idea what would be causing this? I followed the code example exactly.

Thanks!
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by Tony Li »

Hi,

StandardUISubtitlePanel has a custom editor script named StandardUISubtitlePanelEditor. You'll need to make a subclass in a folder named Editor. Example:

Code: Select all

public class MySubtitlePanel : StandardUISubtitlePanel
{
    public Color someExtraVariable;
    ...

Code: Select all

using UnityEngine;
using UnityEditor;
using PixelCrushers.DialogueSystem;

[CustomEditor(typeof(MySubtitlePanel), true)]
public class MySubtitlePanel : StandardUISubtitlePanelEditor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        serializedObject.Update();
        EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(MySubtitlePanel.someExtraVariable)), true);
        serializedObject.ApplyModifiedProperties();
    }
}
cptscrimshaw
Posts: 105
Joined: Sun Sep 20, 2020 8:21 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by cptscrimshaw »

[Edit: Nevermind. I forgot to add the new Subtitle Panels to the StandardDialogueUI component. :)]

Thanks Tony! Your support is always impeccable.

I got the color changing (on the name plate), but for some reason, it appears that On Close isn't being called anymore, so the portrait, name, and subtitle text is showing up for both NPC and the player (see images).

https://ibb.co/5s59ZwQ
https://ibb.co/zx1tq0S

Not getting any errors or warnings... maybe something is wrong with my code? Or is something else going on? I changed the debug level to info, but didn't see any issues.

Code: Select all

using PixelCrushers;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using UnityEngine.UI;

public class CustomSubtitlePanel : StandardUISubtitlePanel
{
    public Image namePlate;
    private string namePlateColorString;
    private Color namePlateColor;

    protected override void Start()
    {
        namePlate = GameObjectUtility.GameObjectHardFind("Name Panel").GetComponent<Image>();
    }

    public override void SetContent(Subtitle subtitle)
    {
        base.SetContent(subtitle);
        DialogueActor myDialogueActor = subtitle.speakerInfo.transform?.GetComponent<DialogueActor>();
        namePlateColorString = DialogueLua.GetActorField(myDialogueActor.name, "Name Plate Color").asString;

        if (ColorUtility.TryParseHtmlString(namePlateColorString, out namePlateColor))
        {
            namePlate.color = namePlateColor;
        }
    }
}
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by Tony Li »

Hi,

Just making sure -- is it working now?
cptscrimshaw
Posts: 105
Joined: Sun Sep 20, 2020 8:21 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by cptscrimshaw »

It is! Thanks again.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Changing SubtitlePanel background color according to speaking Actor

Post by Tony Li »

Great! Just wanted to make sure.
Post Reply