- Home /
How can I get a UI Canvas to hide/appear on 'esc' button press?
I've been tackling with this script for a while and I don't have much understanding of C# so that probably doesn't help...
Still, this is the script I have so far (Has undergone MANY changes since the original, which made more sense but still didn't work, so please excuse any painfully obvious problems with the script.)
using UnityEngine;
using System.Collections;
public class MenuAppearScript : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("escape"))
{
GameObject.MenuCanvas = SetActive(true) ;
}
}
}
Also, I'm using the Canvas that comes with 4.6.
Answer by Kiwasi · Dec 09, 2014 at 09:42 AM
Put this script on some other GameObject in the scene. I like to use an input manager or the canvas.
using UnityEngine;
using System.Collections;
public class MenuAppearScript : MonoBehaviour {
public GameObject menu; // Assign in inspector
private bool isShowing;
void Update() {
if (Input.GetKeyDown("escape")) {
isShowing = !isShowing;
menu.SetActive(isShowing);
}
}
}
Hit the tick. And check out my profile for a link to some UI tutorials.
Sorry about this, may have gotten a bit over-excited about that fact that it worked, it's taking 3 button presses for the menu to actually show up. How can I fix this?
Now I've added a couple of Time.timescale's to it it now takes 5 button presses to appear...
That's odd. As written it should toggle the menu every time you press escape. Try putting a debug.log inside the if statement and check if it is firing every time you press escape.
Answer by jhsu · Jul 13, 2015 at 01:41 PM
I know this is an old question, but it kept coming up in my search and I found a much better solution that I will share for reference.
This solution is in C# and the script needs to be attached to the canvas GameObject itself. It uses .enabled which is essentially ticking on and off the checkbox next to the name in the inspector panel.
MenuToggle.cs:
using UnityEngine;
using System.Collections;
public class MenuToggle : MonoBehaviour {
private Canvas CanvasObject; // Assign in inspector
void Start()
{
CanvasObject = GetComponent<Canvas> ();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Escape))
{
CanvasObject.enabled = !CanvasObject.enabled;
}
}
}
With .SetActive I was having to press the key multiple times as the OP mentioned in the comments to the other solution.
Thank you jhsu! This worked great.
What would be the script to make a UI button toggle the canvas?
Thanks!
Hey, have you found a solution to your problem? Could you share? Thanks.
Hey, this worked a treat for what I wanted, except like m_truly I would also like to trigger this with a button press from a button on another UI element (rather than the escape key).
I am working on it myself (mostly trial and error), but any feedback on how to do it would be most welcome! :)
create a public void which you call with the unity button and you can call the same void when pressing the escape button
I found a way, using the following.
It has to be attached to the button, not to the canvas.
The Start section finds the relevant Canvas (YourNamedCanvas). The ToggleCanvas section does the toggling.
When you go to your button after you attach the script, drag the script on to the None (object) area, and then use the drop down menu to go to YourScriptName and select ToggleCanvas()
So, pressing your button in the game will toggle the canvas.
{
private Canvas CanvasObject;
void Start ()
{
CanvasObject = GameObject.Find("YourNamedCanvas").GetComponent<Canvas>();
CanvasObject.GetComponent<Canvas> ().enabled = false;
}
public void ToggleCanvas ()
{
if (CanvasObject.enabled == true) {
CanvasObject.GetComponent<Canvas> ().enabled = false;
} else {
CanvasObject.GetComponent<Canvas> ().enabled = true;
}
}
Answer by Mmmpies · Dec 09, 2014 at 08:52 AM
Add a CanvasGroup to the Canvas if not there already and then set the Alpha to 0 (for invisible) or 1 (for visible), Also set the Interactive and BlocksRaycast false (when invisible) and true if visible.
Note the Alpha is a float so you can fade in or out of being visible if you want.
see CanvasGroups
EDIT:
Easiest way is to create a public CanvasGroup so...
public CanvasGroup myCanvasGroup;
and reference that as myCanvasGroup.Alpha = x;
But you also need using reference at the top of the script:
using UnityEngine.UI;
Then in the inspector when the myCanvasGroup appears and is empty drag the canvas onto it.
I'm doing this in my head as I don't currently have access to Unity, it should work but may contain typos!
Good luck.
I altered my script to accommodate and yet still I don't really understand what I'm doing fully, how would I go about fixing the script for it?
using UnityEngine;
using System.Collections;
public class $$anonymous$$enuAppearScript : $$anonymous$$onoBehaviour
{
public float alpha;
void Update()
{
if (Input.Get$$anonymous$$eyDown("escape"))
{
CanvasGroup.alpha (1);
CanvasGroup.Interactive(true);
CanvasGroup.BlocksRaycast(true);
}
}
}
You don't call the functions on the class but rather on the instance. You need a reference to the canvasGroup you want to enable
public CanvasGroup canvasGroup;
...
canvasGroup.alpha = 1;
canvasGroup.interactable = true;
canvasGroup.blocksRaycasts = true;
Then set this canvasGroup to the Gameobject with a CanvasGroup Component you wish to activate.
And as $$anonymous$$mmpies mentioned, you need to add
using UnityEngine.UI
at the top of your script, so the computer knows what a CanvasGroup acutally is.
Not sure I understand Bored$$anonymous$$ormon, can you elaborate?
So, I changed my script again as told to:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class $$anonymous$$enuAppearScript : $$anonymous$$onoBehaviour
{
public float alpha;
public CanvasGroup canvasGroup;
void Update()
{
if (Input.Get$$anonymous$$eyDown("escape"))
{
CanvasGroup.alpha = 1;
CanvasGroup.interactable = true;
CanvasGroup.blocksRaycasts = true;
}
}
}
But I'm getting 3 errors that all seem to be the same problem.
Answer by Kramboja · Dec 09, 2014 at 09:45 AM
I guess this should work.
//makes canvas visible by default
private bool canvasOn = true;
void Update()
{
if(Input.GetKeyDown("escape"))
{
//if canvas is visible you have to hide it
if(canvasOn)
canvasOn = false;
canvas.enabled = false;
}
else
{
canvasOn = true;
canvas.enabled = true;
}
}
}
Hamacam, change CanvasGroup to canvasGroup in the if statement and careful with those braces/brackets looks like you might have too many }
Actually the braces are probably O$$anonymous$$ just out of alignment.