Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rainbow_design · Feb 16, 2016 at 11:06 AM · unity 5eventimprove

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rainbow_design · Feb 16, 2016 at 02:53 PM 0
Share

I dont feel it is that easy sorry...

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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");
 });

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Feb 16, 2016 at 05:32 PM 0
Share

Good stuff, thanks for sharing.

avatar image
0

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Feb 16, 2016 at 02:00 PM 0
Share

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).

avatar image rainbow_design · Feb 16, 2016 at 02:23 PM 0
Share

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!

avatar image saschandroid rainbow_design · Feb 16, 2016 at 02:33 PM 0
Share

for c#

 public class YourClass : $$anonymous$$onoBehaviour, IPointerEnterHandler
 {
     public void OnPointerEnter(PointerEventData ped)
     {
         // your code here (change sprite, colors, text or whatever)
     }
 }

avatar image rainbow_design saschandroid · Feb 16, 2016 at 02:45 PM 0
Share

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;
     }
avatar image meat5000 ♦ rainbow_design · Feb 16, 2016 at 02:41 PM 0
Share

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.

PointerEventData

avatar image rainbow_design meat5000 ♦ · Feb 16, 2016 at 02:51 PM 0
Share

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.

avatar image rainbow_design · Feb 16, 2016 at 02:52 PM 0
Share

I have attached more informations, if anyone finds an EASY solution i will agree....

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges