- Home /
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.
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());
}
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);
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
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

Follow this Question
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