- Home /
Enable and Disable PointerEnter EventTrigger on number of Buttons present in a single GameObject MenuButtons
In this picture u can see i have list of buttons under the MenuButtons GameObject. I want them to be non interactive + i dont want them to trigger OnPointer events as well when i enable my pannel to SetActiveTrue. And once the panel is SetActive false i want them to go interactive again.
public void SettingsPanel()
{
PanelSettings.SetActive(true);
foreach(Transform MenuButton in MenuButtons)
{
MenuButton.GetComponent<Button>().interactable = false; //all button are non interactive at this point but i don't know how to disable them from firing those OnPointer events i have assigned to them in inspector.
}
}
Answer by jstopyraIGG · Feb 13, 2019 at 02:30 AM
You can override the EventTrigger class and have it not call the event if the button is not intractable You cannot have any variables exposed to the inspector if you're overriding the EventTrigger, so you need to find the button with code.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class TriggerOverride : EventTrigger
{
public Button _button = null;
public Button button { get { if (_button == null) { _button = Get(); } return _button; } }
//find the button on the object
private Button Get()
{
return GetComponent<Button>();
}
//override on pointer enter so it only gets called when button is interactable
public override void OnPointerEnter(PointerEventData eventData)
{
if (!button.interactable)
return;
base.OnPointerEnter(eventData);
Debug.Log("On Enter");
}
}
Thanksssssssssssssssssssssss man finally done it with your help. Can you please explain to me all the lines you wrote in this code. I mean the reason for writing these lines in this. I can understand uptil the line 16 but i wanna know about that method. I am still learning would be appreciated if you please explain the working of this code. what is base.OnPointerEnter(event data) , i don't even know how these methods get their parameters that are in their methods.
what I did there, is called Inheritance. I took an existing class and made a child class from it, that can access all of it public and protected variables and functions. When you do that, you can tell a function to act differently than the base class (in this case EventTrigger) tells it to act. What you can also do is call that same function on the base class, and that will call the original class, and you do it by calling base.Function();. This specific function OnPointerEnter() gets called automatically by the base class EventTrigger. If any of it does not make sense, feel free to comment and I can try explaining more. Also l, read up on Inheritance, it's a very important thing to know when coding.
thanks i know inheritence and the method overiding but just confused about the parameters in OnPointerEnter method, i suppose we are passing the instance of PointerEventData class but what is going afterwards gets over my head. I am not sure what is eventData holding.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making Everything Public 0 Answers
Build auto id for any object in inspector 1 Answer
Add Special Events for Skills 1 Answer