Response Menu Panel Buttons with different colors

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
rauljl1
Posts: 55
Joined: Fri Apr 15, 2022 7:40 pm

Response Menu Panel Buttons with different colors

Post by rauljl1 »

Hi Tony.
Like ShuffleResponsesScript, I would like to assign different colors to my buttons.

All my Dialogues have 4 Responses. So I would like to assign 4 different colors to each one, but knowing what color I'm assigning to each answer.

What i'm doing is:

buttons = new GameObject[4];
colors = new List<Color> { Color.red, Color.blue, Color.yellow, Color.green };
ShuffleListScript.Shuffle(colors);

I asign the Tag DialogueButton to my Response Button Template
buttons = GameObject.FindGameObjectsWithTag("DialogueButton");

buttons[0].GetComponent<Image>().color = colors[0];
buttons[1].GetComponent<Image>().color = colors[1];
buttons[2].GetComponent<Image>().color = colors[2];
buttons[3].GetComponent<Image>().color = colors[3];

But, in some cases I get this error:
Index out of bounds

Is there a better way to do this like ShuffleResponsesScript ?
User avatar
Tony Li
Posts: 21965
Joined: Thu Jul 18, 2013 1:27 pm

Re: Response Menu Panel Buttons with different colors

Post by Tony Li »

Hi,

Do you want to always associate a color with a specific response?

For example, say the responses are "Yes", "No", and "Maybe". Do you want "No" to always be the same color, such as red?

If so, then I recommend making a subclass of StandardUIResponseButton. Override the SetFormattedText() method to:

1. Start a coroutine that waits until end of frame.
2. In the coroutine, set the color.

The method could read a color value from a custom field in the response dialogue entry.

Example:

Code: Select all

public class MyResponseButton : StandardUIResponseButton
{
    public override void SetFormattedText(FormattedText formattedText)
    {
        base.SetFormattedText(formattedText);
        StartCoroutine(SetBackgroundColor());
    }
    
    IEnumerator SetBackgroundColor()
    {
        yield return WaitForEndOfFrame(); // The 'response' property will be set by the end of the frame.
        string responseColor = Field.LookupValue(response.destinationEntry.fields, "Response Color"); // Look up color field.
        var color = string.IsNullOrEmpty(responseColor) ? Color.darkGray : Tools.WebColor(responseColor);
        GetComponent<Image>().color = color; // Set button image to that color.
    }
}
Post Reply