Forum rules - please read before posting.

How to Refresh UI elements after Loading?

edited January 2021 in Technical Q&A

So when I save my game, it works and it saves the Data. And when I load it back, it does load the data back however since its tied to my UI as well, it doesn't update the UI.

So my question is, how do you edit the Load function both in script and in the editor if its possible. So I could edit it in a way that whenever I load the file, it will also call the for example UpdateUI() Method. That way ill load back in with data that also updates all my Ui elements like Texts etc.

Or maybe is there a better way to do this with AC when it comes to the UI elements?

Thanks

Comments

  • What is it about the UI that isn't updating correctly? AC should restore the visibility of your elements, but I'm not clear if you're talking about something that should be updated by AC, or something unique that needs custom code.

    It's possible, however, to run custom code after loading a save file by hooking into the OnFinishLoading custom event:

    using UnityEngine;
    using AC;
    
    public class CustomLoadingHook : MonoBehaviour
    {
    
        void OnEnable () { EventManager.OnFinishLoading += OnFinishLoading; }
        void OnDisable () { EventManager.OnFinishLoading -= OnFinishLoading; }
    
        void OnFinishLoading ()
        {
            // Custom behaviour after loading goes here
        }
    
    }
    
  • Apologies if I didn't explain it properly. I don't like dumping everything in the Update method so this is the way I do it for example:

    `public class Test : MonoBehaviour
    {

    public Text moneyText;
    
    public void AddMoney()
    {
        AC.GlobalVariables.GetVariable("Money").IntegerValue += 100;
        UpdateUI();
    }
    
    public void UpdateUI()
    {
        moneyText.text = AC.GlobalVariables.GetVariable("Money").IntegerValue.ToString();
    }    
    

    }`

    So whenever, I use the AddMoney() method, it will update the UI. If I saved and loaded the file, in my AC Game editor tab under global variables, I would see the data being loaded. But it requires an extra step to run the UpdateUI() right after it loads up.

    I think what you suggested is what I needed, so ill have to try it out Thanks. I can use that to lets say add my own methods into the loading system right?

  • I have tested it out and what you suggested did the trick thank you.

  • For the specific case above: Do be aware that an AC Label menu element can be linked to a Global Variable - either by setting the "Label type" to "Global Variable", or to "Normal" and inserting the [var:X] token into it's "Label text" field.

    See the tutorial here for more details.

  • edited January 2021

    Oh wow, I learn something new for AC every time. I will definitely move to that method. Didn't even know there was such a thing, thank you again

  • edited January 2021

    Sorry Chris, just wanted to ask couple more things, after testing the AC ways of updating UI using menus, the labels linked with Unity UI objects are working well but with buttons it doesn't seem to work. I have tried linking the AC Menu's button with the Unity button and I created a simple actionlist where after pressing the AC Menu button it would disable a UI panel in my canvas.

    I have couple of UI panels for different things under my main canvas and I would like to be able to activate and deactivate them. But with buttons it doesn't seem to work. For the Menu setting I have set the source as "Unity UI in Scene".

    I think the Object functionalities in the actionlist editor don't seem to work, I select Object > Visibility > visibility: invisible and I have dragged one of my UI panel within the canvas as reference but it doesn't do anything if I use that actionlist thats used on the AC's Menu button I created.

    Or I might not be linking the AC button element with Unity UI button correctly

  • You can always test an ActionList's behaviour by running it manually - click "Run now" in its Inspector at runtime to see if it has the intended effect.

    Though, the Object: Visibility Action is for Unity Renderer components - not UI elements.

    To show/hide AC Menu Elements, use the Menu: Change state Action. To control the enabled state of UI elements that aren't linked as elements to the AC Menu Manager, I recommend using the Object: Animate Action.

    If you attach an Animator to your UI's root, you can create animations that control the state of your UI panels etc - it's a safe way of doing so without losing references to them due to e.g. disabled GameObjects. The "Object: Animate" Action can then be used to control that Animator's playback, and in turn the state of the UI.

    Regarding your UI's Source, I recommend relying on prefabs unless you have a specific need for the UI to be part of the scene (i.e. a computer screen displayed in world space).

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.