• Hi Guest!

    Please be aware that we have released a critical security patch for VaM. We strongly recommend updating to version 1.22.0.7 using the VaM_Updater found in your installation folder.

    Details about the security patch can be found here.

[C#] How can I check if current screen is on [Top Menu]? (Done)

14mhz

Well-known member
Messages
87
Reactions
677
Points
83
Currently, I m developing a plugin using Canvas, and I want to disable it when the current screen is [Top Menu].
1.jpg
2.jpg

How can I create a script that checks if the current screen is the [Top Menu]?
C#:
public class DetectTopMenu : MVRScript
{
    void OnGUI()
    {
        if (SuperController.singleton.activeUI == SuperController.ActiveUI.MainMenu)
        {// This is a good working [MainMenu] check example.
            SuperController.LogMessage("[INF] I'm here [MainMenu] ... ");
        }

       /* is TopMenu check Pseudo Code Here !!!
        if (SuperController.singleton.activeUI == brahbrah~.ActiveTopMenu???)
        {
           SuperController.LogMessage("[INF] I'm here [TopMenu] ... ");
        }
        */
    }
}
Thanks you, all.
 
Last edited:
I think the top menu Transform is SuperController.singleton.topWorldUI, so you can probably just check if its gameObject is activeSelf.

An alternative solution to OnGUI - there's a generic way to handle cases where you need a callback for a gameobject being enabled or disabled (among other possible MonoBehaviour event functions):

C#:
class UnityEventsListener : MonoBehaviour
{
    public Action enabledHandlers;
    public Action disabledHandlers;

    void OnEnable()
    {
        enabledHandlers?.Invoke();
    }

    void OnDisable()
    {
        disabledHandlers?.Invoke();
    }
}

That can be added as a component to the topWorldUI gameobject, and you can then disable the canvas when the topWorldUI gameobject is enabled and enable when it is disabled:

C#:
Canvas _myCanvas;
UnityEventsListener _topWorldUIListener;

public override void Init()
{
    _topWorldUIListener = SuperController.singleton.topWorldUI.gameObject.AddComponent<UnityEventsListener>();
    _topWorldUIListener.enabledHandlers += OnTopWorldUIEnabled;
    _topWorldUIListener.disabledHandlers += OnTopWorldUIDisabled;
}

void OnTopWorldUIEnabled()
{
    SuperController.LogMessage("[INF] I'm here [TopWorldUI] ... ");
    if(this.enabled) // if the plugin is enabled
    {
        _myCanvas.enabled = false;
    }
}

void OnTopWorldUIDisabled()
{
    SuperController.LogMessage("[INF] [TopWorldUI] closed... ");
    if(this.enabled) // if the plugin is enabled
    {
        _myCanvas.enabled = true;
    }
}

void OnDestroy()
{
    if(_topWorldUIListener != null)
    {
        UnityEngine.Object.Destroy(_topWorldUIListener);
    }
}
 
Back
Top Bottom