Jump to content

Looking for a better way to know if the UI is visible or not


Recommended Posts

I need to know whether the UI is being displayed or not. For now, I'm using the following code:



public class UICLASS: MonoBehaviour
{

private bool uiVisible = true;
private static KeyCode toggleUIKeycode = KeyCode.F2;

public void Update ()
{


if (Input.GetKeyDown (toggleUIKeycode))
uiVisible = !uiVisible;
}
}

I'm sure there is a better way, since this will not work if there is another mod which issues the following calls:


GameEvents.onHideUI.Fire ();

and


GameEvents.onShowUI.Fire ();

So far, I haven't found it, and was hoping someone could point me in the right direction.

Thanks in advance.

Link to comment
Share on other sites

This would be my suggestion:


public class UICLASS : MonoBehaviour
{
private bool uiVisible = true;

public void Awake ()
{
GameEvents.onShowUI.Add(onShowUI);
GameEvents.onHideUI.Add(onHideUI);
}

private void onShowUI ()
{
uiVisible = true;
}

private void onHideUI ()
{
uiVisible = false;
}

public void OnDestroy ()
{
GameEvents.onShowUI.Remove(onShowUI);
GameEvents.onHideUI.Remove(onHideUI);
}
}

This runs when the UI is activated or deactivated, either with F2, another key or by code. :)

Cheers!

Thanks.

I decided to keep this as a separate class to make it cleaner, so I had to add two functions to get and set the flag:


public bool isVisible()
{
return uiVisible;
}
public void setVisible(bool
{
uiVisible = b;
}

I had to add two functions, since my original code was copied from the main class of my mod.

One thing though, the Awake function isn't being called, I have to call it myself from inside the main Awake function.

Do I need to put something in front of this:


public class UICLASS: MonoBehaviour

Edited by linuxgurugamer
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...