Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 almondyjelly · Apr 09 at 04:57 AM · eventsystemeventsevent triggeringdelegate

Cannot convert PointerEventData to BaseEventData

I'm trying to make a function that adds event triggers programmatically.

 private void AddEventTrigger(EventTriggerType type, UnityAction<PointerEventData> callback) {
     EventTrigger.Entry entry = new EventTrigger.Entry();
     entry.eventID = type;
     entry.callback.AddListener(callback); // LINE 1
     eventTrigger.triggers.Add(entry);
 }

The problem is, the compiler errors at // LINE 1 with "Cannot convert PointerEventData to BaseEventData." PointerEventData inherits BaseEventData, so shouldn't it be a valid input type?

The listeners implement e.g. UnityEngine.EventSystems.IDragHandler, so they require PointerEventData

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 · Apr 09 at 09:18 AM

Well, that's not possible directly because this is a matter of covariance / contravariance. It always depends on the direction the data flows.


Covariance applies for delegates which return a certain value value type. You can assign a more specific method to a delegate which returns a less specific but compatible type. This for example would apply to the generic IEnumerable<T> interface as it only returns values of a certain type. So for example if you have a variable of type IEnumerable<MonoBehaviour> you can assign an IEnumerable<YourSpecificMonoBehaviourClass> to it since an IEnumerable only "returns" a value. Since the user of the variable expects to get back a MonoBehaviour, any derived class would satisfy this condition. So data flows out of the object (return value).


Contravariance on the other hand works the other way round and applies to arguments of delegates and not return values. Since the dataflow is here in the opposite direction, the relationship between derived and base classes is also reversed. So for example a variable of type Action<YourSpecificMonoBehaviourClass> can be assigned a method that only expects a MonoBehaviour. That's because when invoking a delegate the user has to supply a compatible type based on the delegates argument. In this case our delegate type expects a YourSpecificMonoBehaviourClass instance as argument. Of course we can assign a method which a less defined argument since in the case of delegate arguments the data flows into the method.


In your specific case your "callback" is of type UnityEvent<BaseEventData> since that's the type used internally by the UI system. So you can only assign methods to this delegate that accepts "BaseEventData" or a less derived type. Imagine this would be possible. We now have a delegate that accepts "BaseEventData" but you assigned a method that expects a PointerEventData. Nobody stops you from invoking your delegate with any other argument that is derived from BaseEventData. This just doesn't work.


That's why Unity jumps through several hoops in the event system to properly resolve the arguments. For example the UI system uses the ExecuteEvents class to actually invoke the handlers. It essentially has specific implementations of the Execute method which does an argument validation and casts the argument properly.

The EventTrigger class is a monobehaviour which is meant to be used as a base class for your own class. So you're currently poking around in internally used classes by that belong to the EventTrigger component. So I'm not sure what you want to achieve here..


What you can / have to do if you want to register such a callback manually, you have to create two seperate implementations. Since the event system basically only uses either the "BaseEventData" or the "PointerEventData" having one implementation for each case should cover all your needs. In the case of the "PointerEventData" you have to create a closure that converts / validates the argument, just like the Event

  private void AddEventTrigger(EventTriggerType type, UnityAction<PointerEventData> callback) {
      EventTrigger.Entry entry = new EventTrigger.Entry();
      entry.eventID = type;
      entry.callback.AddListener((data)=>callback.Invoke(ExecuteEvents.ValidateEventData<PointerEventData>(data) ));
      eventTrigger.triggers.Add(entry);
  }

Something like that should work, I guess. Though the usage is still not really clear.

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 almondyjelly · Apr 10 at 04:02 AM 0
Share

Thank you for your incredibly detailed response! I've learned a lot. For future reference, I found this reference that's helpful for covariance/contravariance in c#: https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#C#_examples. To answer "I'm not sure what you want to achieve here.." I'm very new to unity and was following the example here: http://answers.unity.com/answers/1226869/view.html. I'm trying to set up triggers on a bunch of different prefab variants, where the handler function takes the variant's sprite as a parameter, because I wasn't sure how else to do it besides manually add all the event triggers and attach the Sprite as the parameter to the listener for each one. There's probably some better way haha...

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

138 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 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

UnityEngine.EventSystems.. Trying to figure it out .js 1 Answer

How to set passed param to event trigger callback in script? 1 Answer

Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers

EventTrigger / EventListener (what's the difference?) 0 Answers

Correct way to send an event to a specific enemy 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