- Home /
Unity UI 4.6 Canvas Enable/Disable make Accessible
Greetings . I need some help about canvas . My player needs to jump on pressing space or whatever i add to player . But in pause Button of my game i set If button click then enable the pause canvas with option like go to main menu and reload , now on resume button i set to enable = false the canvas but when i press 'space' after enabling and disabling it i got that some functions from pause canvas like reload or music on/off will execute instead of doing jump of player or any other function .
I think with canvas i need to disable button script option . I think it works i am going to try this . But if there any other idea about this please post it.
Answer by Mmmpies · Feb 03, 2015 at 10:52 AM
Select the EventSystem in the hierarchy and un-tick send Navigation Events. That'll stop the keyboard interactions with the UI.
This sounds easy and effective . But how can i declare eventsystem variable . Is this Just 'Event' ?
The EventSystem is just a script component so it is like getting reference to a component attached to a gameobject. Like:
EventSystem eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
Now you can access the different properties of EventSystem script through this eventSystem variable.
One drawback of disabling EventSystem Send Navigation Event is that if there are any UI elements on your Gameplay Screen then they will stop receiving the input.
Yes just as @Harshad$$anonymous$$ says, I've set a button to switch the EventSystem off and on via the UI itself. Probably fairly useless but it was just a test:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class EventsOnOff : $$anonymous$$onoBehaviour {
private EventSystem eventSystem;
void Start()
{
eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
}
public void ChangeEvents()
{
eventSystem.sendNavigationEvents = !eventSystem.sendNavigationEvents;
}
}
O$$anonymous$$ thanks guys , now i got it . First i need to gave the reference using event system Library file name,I was thinking first that it can be access by using .'`using UnityEngine.UI;`'I was totally wrong Event system has it's own defined file .
Yep, I should really have pointed that bit out but for anyone else that needs this you need this adding in the using section:
using UnityEngine.EventSystems;
Answer by HarshadK · Feb 03, 2015 at 10:56 AM
It is not possible to provide provide exact solution without looking at your structure and code but looks like your scripts for handling the Canvas input contains code that looks for a keypress (like Space) and then reacts to it. So even when you disable your canvas your scripts for handling the canvas input are active and are taking that input.
One way is as you said to disable the scripts when you disable the canvas but to be on safer side you can also include a check in your input scripts for canvas that checks if the canvas is enabled before performing the action this way even if your script is not disabled (but your canvas is) it will not perform the action. And you can even disable the script once you found out that canvas is disabled but the script was not.
That's also a great Idea! Awesome .
1st my input method checks if canvas enables then perform action , otherwise it doesn't perform . Now that's like programmer .