Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Paparakas · Nov 13, 2013 at 01:50 PM · eventevent-handling

Does unity support eventargs?

Something like this: http://msdn.microsoft.com/en-us/library/system.eventargs(v=vs.110).aspx

Googling doesn't show anything about it.

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

2 Replies

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

Answer by Loius · Nov 13, 2013 at 02:44 PM

Yes-ish.

EventArgs works, but as far as I know it's not really related to Unity, in that Unity uses delegates instead of events for its subscribable events (at least, I code like it does... maybe they just work the same way). You can make your own event-args things without any worry though.

 public delegate void EventPrototype(System.EventArgs args);
 public event EventPrototype onHit;
 
 public void Slap() {
   if ( null != onHit ) onHit(new System.EventArgs());
 }
 
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 Paparakas · Nov 13, 2013 at 03:57 PM 0
Share

Did this and it worked, thanks!

 public class ArgsTest : $$anonymous$$onoBehaviour {
     
         private event HitEventHandler Slap;
         private void Start()
         {
             Slap += new HitEventHandler(OnSlap);
             Slap(new HitEventArgs(1000));
         }
 
         private void OnSlap(HitEventArgs SlapParemeters)
         {
             Debug.Log("Auch");
         }
 }
 
 public class HitEventArgs : System.EventArgs
 {
     public HitEventArgs(float Explosionforce)
     {
         this.ExplosionForce = Explosionforce;
     }
     public float ExplosionForce{get;private set;}
 }
 
 public delegate void HitEventHandler(HitEventArgs args);
avatar image
0

Answer by wichmannoa · Dec 16, 2017 at 01:25 AM

Add : using System;

So I was just watching this one tutorial "C# events and delegates made simple", its called. The instructor uses (object source, EventArgs args). Naturally I wanted to use it too. But unity wasn't budging in. Eventually I noticed that he also has at the very top of his file "using System". I'm only that far in the experimentation process but adding "using System" allowed me to add [...] EventArgs args to my delegate signature. Hope that helps

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 Bunny83 · Dec 16, 2017 at 02:00 AM 0
Share

Well, in the end there's no good reason to actually use EventArgs at all in Unity. $$anonymous$$any .NET framework components use specifically classes derived from EventArgs as information containers. Events can actually have any number of parameters which can have any type you want. The main reason why most .NET components use the signature (object, EventArgs) is assignment compatibility which is necessary for the visual form designer in VisualStudio. It's more a philosophie within the .NET framework.


In Unity it's not recommended to include a using statement with the System namespace as the System namespace as several "collisions" with the UnityEngine namespace. $$anonymous$$ost notably the Random class. Usually you only need single classes from the system namespace so it's actually better to just use the full qualified class name like Paparakas showed in his comment above:

 System.EventArgs


Though as i said the pure .NET things are pretty irrelevant for Unity. Unity has it's own event classes (UnityEvent) which are actually serializable in the inspector.

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

19 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

Related Questions

How can I replace the behaviour of a GUILayout.scrollView on MouseWheel event? 2 Answers

checking for keyboard input with exceptions 0 Answers

Event is not being implemented from interface 0 Answers

Casting and receiving non static event from two instances problem 1 Answer

OnPointerDown vs OnMouseDown 1 Answer


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