• Hi Guest!

    We are extremely excited to announce the release of our first Beta1.1 and the first release of our Public AddonKit!
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. For access to the Public AddonKit you must be a Creator tier member. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.
  • Hi Guest!

    VaM2 Resource Categories have now been added to the Hub! For information on posting VaM2 resources and details about VaM2 related changes to our Community Forums, please see our official announcement here.

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

14mhz

Well-known member
Joined
Oct 23, 2024
Messages
167
Solutions
7
Reactions
1,524
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