- Home /
Event System - AddListener and Callbacks
Hello, i found something i perceive as a very serve flaw in the event system.
onclick is the only function that has an addlistener. on enter/exit as example has this not which might end in very complicated code for me to make it working.
How can i get unity to improve this and improve the scripting event documentation as well?
Edit More Details:
It is not that easy. I am sure i will have to write quite some code to work around this here is the background of my request: ;
http://forum.unity3d.com/threads/onhover.386290/#post-2512721
If you know how to do this in one line then be free to share it but if i have to write 20+ lines to do what onclick.addlistener can do in one line there is definitively somthing to improve!
Edit2 since you disagree with the complicated part here is my code, if its so simple just replace the line : fmitem.GetComponent ().onClick.AddListener (delegate { mapclicked (sub); });
public GameObject buildmenuitem(String name, GameObject menuprefab, Vector2 scales, string sub) {
GameObject fmitem = h.Instantiate2 (menuprefab);
name = name.Replace ("\\", "");
String image2= sub.Replace (".bui", "");
fmitem.GetComponent<Image> ().sprite = Resources.Load<Sprite> (image2.Replace (g.pgamesetpath, ""));
fmitem.name = name;
fmitem.GetComponent<Button> ().onClick.AddListener (delegate { mapclicked (sub); }); //todo right button handling
//fmitem.GetComponent<EventTrigger> ().OnPointerEnter.Invoke (delegate { mapclicked (sub); }); //todo right button handling
fmitem.transform.FindChild ("Text").GetComponent<Text> ().text = name;
fmitem.transform.localScale = (scales);
fmitem.transform.SetParent (g.uicanvas.transform, false);
return fmitem;
}
I attached my code. This is the main concern:
fmitem.GetComponent ().onClick.AddListener (delegate { mapclicked (sub);
I want to call on hover mapclicked with the argument sub. However the next line would add a different call with a different argument.
All this is done called by a loop so sub will change with every loop. What is more fmitem is a instance of a prefab.
Answer by rainbow_design · Feb 16, 2016 at 04:38 PM
Well it took a while but i got a good answer on this from xXGrime in the forum the code i have to add looks like this:
EventTrigger pointerHoverTrigger = fmitem.GetComponent<EventTrigger> ();
EventTrigger.Entry yourNewEntry = new EventTrigger.Entry ();
yourNewEntry.eventID = EventTriggerType.PointerEnter;
pointerHoverTrigger.triggers.Add (yourNewEntry);
yourNewEntry.callback.AddListener ((eventData) =>
{
//Do something when mouse is over button
print("Mouse over button");
});
Answer by phil_me_up · Feb 16, 2016 at 01:51 PM
Look at the Event Trigger component - this might give you everything you need.
API link below but basically, just add that component to the object you're interested in and you get a lot more options. Really, the 'onclick' is there more for convenience then anything else I think.
API: http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
Phil is right. It should be everything you need.
For things like OnDrag and OnPointerDown I have been 'Implementing' the interfaces which enable these callbacks to receive data in the script. This method does not need to Add a Listener.
In JS you add 'implements YourWantedInterface' after the 'extends monoBehaviour'. In C# I think you simply add ',YourWantedInterface' after monoBehaviour (I could be wrong here).
Sorry but it is not that easy. I am sure i will have to write quite some code to work around this here is the background of my request: ; http://forum.unity3d.com/threads/onhover.386290/#post-2512721
If you know how to do this in one line then be free to share it but if i have to write 20+ lines to do what onclick.addlistener can do in one line there is definitively somthing to improve!
for c#
public class YourClass : $$anonymous$$onoBehaviour, IPointerEnterHandler
{
public void OnPointerEnter(PointerEventData ped)
{
// your code here (change sprite, colors, text or whatever)
}
}
Here is my code for the mouseclick:
public GameObject buildmenuitem(String name, GameObject menuprefab, Vector2 scales, string sub) {
GameObject fmitem = h.Instantiate2 (menuprefab);
name = name.Replace ("\\", "");
String image2= sub.Replace (".bui", "");
fmitem.GetComponent<Image> ().sprite = Resources.Load<Sprite> (image2.Replace (g.pgamesetpath, ""));
fmitem.name = name;
fmitem.GetComponent<Button> ().onClick.AddListener (delegate { mapclicked (sub); }); //todo right button handling
//fmitem.GetComponent<EventTrigger> ().OnPointerEnter.Invoke (delegate { mapclicked (sub); }); //todo right button handling
fmitem.transform.FindChild ("Text").GetComponent<Text> ().text = name;
fmitem.transform.localScale = (scales);
fmitem.transform.SetParent (g.uicanvas.transform, false);
return fmitem;
}
The PointerEvents are CallBack functions. They work in a similar fashion to OnCollisionEnter(Collision col) where you do not invoke the function manually and the inclusion on the Argument is simply providing a container for the CallBack to populate with data.
Its really not much code.
public class $$anonymous$$yClass : $$anonymous$$onoBehaviour, IPointerEnterHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
//Access PointerEventData members here
if(eventData.pointerEnter == SomeGameObject)
{} //etc
}
}
This isnt a separate script. You simply add the interface and the callback to your script. Easy.
I attached my code. This is the main concern:
fmitem.GetComponent ().onClick.AddListener (delegate { mapclicked (sub);
I want to call on hover mapclicked with the argument sub. However the next line would add a different call with a different argument.
All this is done called by a loop so sub will change with every loop. What is more fmitem is a instance of a prefab.
I have attached more informations, if anyone finds an EASY solution i will agree....
Your answer
Follow this Question
Related Questions
EventTrigger PointerEnter 1 Answer
UnityEditor on Tag Update event 0 Answers
onpointerdown eventsystem 1 Answer
Initialize UnityEvent via script 1 Answer
Some help for "animation event triggers multiple times" 0 Answers