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 Fewpwew130 · Aug 14, 2019 at 08:03 PM · componenteventsystemevent triggeringhover

[BUG maybe] [picture acctached] Event Trigger component does not display the Event Type added from script

Hey!

I was looking how to hover over the UI button in Unity 5 (or Unity 2019). Found the perfect answer here:

https://answers.unity.com/questions/854251/how-do-you-add-an-ui-eventtrigger-by-script.html

by @Phles


It works: it adds the event trigger via script, adds the event type via script, chooses pointer enter via script and even the function to execute - all that can be done manually by dragging in the inspector.


However, though functionally everything is working, the inspector shows a pure component - an empty list - i.e. without the Event Type or the Function chosen via the script. I assume this is a bug, if no, could you please tell me how to make the inspector display this. Thank you!

The image:alt text The script is below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; //UI 
 using UnityEngine.EventSystems; //hover
 
 
 
 public class abc_abc : MonoBehaviour {
 
     // Start is called before the first frame update
     void Start() {
         this.transform.parent.gameObject.AddComponent<EventTrigger>();
         EventTrigger.Entry entry = new EventTrigger.Entry();
         entry.eventID = EventTriggerType.PointerEnter;
         entry.callback.AddListener((eventData) => { do_debug_log2(); });
         this.transform.parent.gameObject.GetComponent<EventTrigger>().triggers.Add(entry);
     }
 
     public void do_debug_log2() {
         Debug.Log("mouse is over the object");
     }
 }

 

 


image.jpg (69.6 kB)
Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Aug 14, 2019 at 09:28 PM

There is no bug. A UnityEvent (which is used by the EventTrigger) has two seperate internal lists:

  • The serialized "persistent calls" list

  • The dynamic callback list.

The inspector will only show serialized data which is the persistent list. How would you even display a event delegate that represents a closure. Specifically this one:

 (eventData) => { do_debug_log2(); }

The created closure object can not be serialized by Unity. Anyways dynamically added listeners (AddListener / RemoveListener) will only work on the dynamic list. The persistent list is managed by the Unity editor and the property drawer for the UnityEvent type. The Unity editor has tools to add / remove methods to persistent calls. However this can only be done through editor code, not at runtime. Also persistent calls have several limitations. For example the method must be part of a UnityEngine.Object derived type since the reference to the object need to be serialized.


To write editor tools which can add / remove / modify persistent listeners, Unity provides the UnityEventTools class. As I said this is an editor class and can not be used at runtime.


So dynamically added listeners will still be called just fine, but there's no way to display them in the inspector unless you somehow visualize them yourself with an editor script.


Note that the "event type" is displayed properly. It's the header of that section: "Pointer Enter". Though this event type does not have any persistent listeners and therefore the list is empty.

Comment
Add comment · Show 2 · 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 Fewpwew130 · Aug 15, 2019 at 06:34 PM 0
Share

0) Pardon me, you are right, the "event type" is Indeed, displayed properly. I apologize.

1) I remeber your username, you helped me sometime ago, thank you again!

2) Thank you for a careful expalantion and a quick reply.

3) I am sorry, my knowledge of program$$anonymous$$g is not enough to comprehend your answer, I think I understood the basics, but the details are not clear: "Though this event type does not have any persistent listeners and therefore the list is empty." So am I correct, that: There is a way to make a listener "persistent" via script and in that case it will be shown in the inspector?

If you could clarify, please do. Thank you anyway, you already helped!

avatar image Bunny83 Fewpwew130 · Aug 26, 2019 at 12:19 AM 0
Share

Sorry for the late reply but I was on vacation. Did you actually read my answer carefully? I said:

The persistent list is managed by the Unity editor and the property drawer for the UnityEvent type. The Unity editor has tools to add / remove methods to persistent calls. However this can only be done through editor code, not at runtime. Also persistent calls have several limitations. For example the method must be part of a UnityEngine.Object derived type since the reference to the object need to be serialized.

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

112 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Event trigger i put on an empty game Object isn't responding even though i have an Event System and everything ,Unity Event trigger isn't responding to clicks 1 Answer

2D Animation does not start 1 Answer

Can't fire a Custom Event on UI Button Touch OnClick 1 Answer

get object that uses the event trigger 1 Answer

Find and set the object of an event type with script? 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