Properly showing/hiding a view in Unity with state changes
Posted: 01 Mar 2021, 17:54
I am having some goofy problems reusing my main menu view. The first problem I think was that I was recreating it each time I was trying to show it. So now I'm preserving the setup and just enabling and disabling it. However, there's a second layer to the problem where it looks like button enable states aren't propagating or something that's going to be a hard problem to show in detail and will take some effort to simplify and see what could be wrong. But for now, I was hoping somebody could check my work for showing and hiding a main menu:
The button enable/disable logic is trickier because that goes between multiple files, involves the view model, and property change notifications. It works in a standalone test but doesn't in Unity. Is the a gotcha with affecting a viewmodel while the view is disabled or something?
Code: Select all
private NoesisXaml mainMenuXaml;
private NoesisView mainMenuView;
public void Show()
{
if (mainMenuView == null)
{
mainMenuView = GlobalState.Instance.MainCamera.gameObject.GetComponent<NoesisView>();
mainMenuXaml = (NoesisXaml)UnityEngine.Resources.Load(MainMenuXamlPath, typeof(NoesisXaml));
// I'm keeping this canary here in case something screws up with loading the main menu later. At least
// this will point to the crux of the problem.
if (mainMenuXaml == null)
{
Debug.LogError("Could not load the main menu XAML from " + MainMenuXamlPath);
}
mainMenuView.Xaml = mainMenuXaml;
mainMenuView.LoadXaml(true);
}
else
{
mainMenuView.enabled = true;
}
}
public void Hide()
{
mainMenuView.enabled = false;
}