Page 1 of 1

Showing Alert with Header and Body text from code.

Posted: Mon Aug 01, 2022 9:07 am
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?

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

Posted: Mon Aug 01, 2022 11:03 am
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().

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

Posted: Wed Aug 03, 2022 10:36 am
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.

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

Posted: Wed Aug 03, 2022 1:44 pm
by Tony Li
Glad to help!