- Home /
How would this EventTrigger look in c#
Hello, i want to do this from scripting side:
A) Take my GameObject Buttontop add an event Pointer Enter. Call a function RectTransform.parent to set the Parent to Panel2
B) Not call RectTransform but my own function Myfunc with the argument Myarg
May someone write me a short example script please.
Here in the picture you see what i want to do from scriptside:
Answer by DiegoSLTS · Sep 17, 2015 at 06:04 PM
First, remove the Event Trigger component, then add a new script that implements the IPointerEnterHandler interface. This interface forces you to implement OnPointerEnter. You can do whatever you want inside that method, you can call a method in the same script or a method in another script. If the method is in another script just add a member to set that reference in the inspector or find that however you want.
Something like this should work:
sing UnityEngine;
using UnityEngine.EventSystems;
public class GameController : MonoBehaviour, IPointerEnterHandler {
[SerializeField]
OtherScript otherComponent; //set this in the inspector
void PrivateMethod() {
//do something
}
void OnPointerEnter() {
PrivateMethod();
otherComponent.AnyPublicMethod();
}
}
The event system will detect when the pointer enters the game object and will call OnPointerEnter on any script that implements it.
If you want to keep the EventTrigger to add events dynamically at runtime it gets a litter more complex, so if this works for you just do it this way.
Hello, i want to create buttons in a loop which on hover do something. I need something i can easily adapt. For clicks i use right now, quickly translated to c# i do:
foreach (var pair in collection) { button.onClick.AddListener(Delegate(myfunction(pair))); }
And i have no idea to adapt your code :(
Your answer
Follow this Question
Related Questions
Make rolling sound when ball moves 2 Answers
How to create a trigger stopping audio in Unity 1 Answer
Attach to other coordinat SpringJoint2D from code. 0 Answers
script type variables 1 Answer