Showing Alert with Header and Body text from code.

Announcements, support questions, and discussion for Quest Machine.
Post Reply
toficor
Posts: 13
Joined: Tue Apr 19, 2022 10:19 am

Showing Alert with Header and Body text from code.

Post by toficor »

Hello,
as title said , I'm trying to show alert as in example, when quest started (data for Alert stored in Quest asset) but from code.

ShowAlert method have 3 overloads, I saw that when I'm use method with only string parameter code flow bypassing instatiation of Container Template, which messing with my UI design. I was trying using ShowAlert(string questID, List<QuestContent>) but all data nescesary should be Scriptable Object, tryied to ScriptableObject.CreateInstance<HeadingTextQuestContent>() but it's null.

So there is my question, is there any way to use your already written code to show alert from my own code like Quest does or I have to write my own solution?
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Showing Alert with Header and Body text from code.

Post by Tony Li »

Hi,

Here are two ways to do that:

1. Make a subclass of UnityUIQuestAlertUI. Add your own method to show header and body text. Here's a rough example. I just typed it in here, so it might have typos.
Spoiler

Code: Select all

public class MyAlertUI : UnityUIQuestAlertUI
{
    public void ShowAlert(string header, string body, float duration)
    {
        var container = Instantiate<UnityUIContainerTemplate>(alertContainerTemplate);
        contentManager.Add(container, currentContentContainer);
        var mainContentManager = contentManager; // Add sub-contents to sub-container, not main content manager 
        contentManager = new UnityUIInstancedContentManager();
        
        // Add header:
        var instance = Instantiate<UnityUITextTemplate>(GetHeadingTemplate(headingContent.headingLevel));
        currentContentManager.Add(instance, currentContentContainer);
        instance.Assign(header);
        container.AddInstanceToContainer(contentManager.GetLastAdded());

        // Add body:
        var instance = Instantiate<UnityUITextTemplate>(currentBodyTemplate);
        currentContentManager.Add(instance, currentContentContainer);
        instance.Assign(bodyContent.runtimeText);
        container.AddInstanceToContainer(contentManager.GetLastAdded());
        
        contentManager = mainContentManager;
        StartCoroutine(TimedDespawn(container, duration));
    }
}
2. Or create ScriptableObjects and pass them to ShowAlert(). Make sure to destroy them after calling ShowAlert().
toficor
Posts: 13
Joined: Tue Apr 19, 2022 10:19 am

Re: Showing Alert with Header and Body text from code.

Post by toficor »

I've tried your second solution with scriptable objects and it works perfectly.

Thank you for your fast response, you are the best.
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Showing Alert with Header and Body text from code.

Post by Tony Li »

Glad to help!
Post Reply