- Home /
How to change target of UnityEvent
I am using UnityEvents to prepare scripts that will run later. My level designer sets a prefab in them and selects the correct method to run later. At runtime, I now need to accept arguments.
I used to simple get the name (string) of a method and Invoke that on the new object. Unfortunately I can't get the parameters this way. The real goal is to change the target, but there are no setters for UnityEvents. I could easily call the right thing using reflection, but there isn't a getter for parameters either.
Anyone has faced a similar problem with UnityEvents? Are there extra methods that I missed? Maybe some stuff in UnityInternal?
Answer by Socapex · Mar 13, 2016 at 09:31 PM
Use black magic...
Ok, jokes aside, you can retrieve the parameters but I wouldn't recommend it. I wont post my code here because it is ugly and dangerous and will get trashed by experienced users. I also wouldn't want anyone to think this is OK, it is evil.
I did the responsible thing and opened up a feature request. You can vote it up if you need this, but we all know these requests never get implemented so don't waste your time. https://feedback.unity3d.com/suggestions/unityevents-add-setter-for-m-target-and-slash-or-getter-for-m-arguments
Answer by YJack · Mar 13, 2016 at 08:03 PM
The method you described, uses a Hashtable. You can implement a similar solution like that using the official tutorial here.
You can also see more about that on the official page here or on that awesome thread here.
About the arguments... you probably are looking for some custom UnityEvents like the ones below:
using UnityEngine;
using UnityEngine.Events;
using System;
[Serializable]
public class UnityEventGameObject : UnityEvent<GameObject> { }
[Serializable]
public class UnityEventTransform : UnityEvent<Transform> { }
[Serializable]
public class UnityEventVector3 : UnityEvent<Vector3> { }
[Serializable]
public class UnityEventVector2 : UnityEvent<Vector2> { }
[Serializable]
public class UnityEventString : UnityEvent<string> { }
[Serializable]
public class UnityEventFloat : UnityEvent<float> { }
[Serializable]
public class UnityEventInt : UnityEvent<int> { }
[Serializable]
public class UnityEventBool : UnityEvent<bool> { }
Hello, thanks for trying to help :) But, no and no ^_^ $$anonymous$$y question might not be easy to instantly grasp.
I am using UnityEvent PersistentEvents, which means none of it is created at runtime (or I would've used simple Actions or delegates). It also means you can't use RemoveListener or AddListener (http://docs.unity3d.com/ScriptReference/Events.UnityEvent.html). UnityActions are non-persistent listeners.
It is an interface for my level designer. He drags a prefab in a scriptable object, and when an editor script is run I execute his selected method to a newly created object. These objects are not available when setting the unityevent PersistentListeners.
The real solution would be to code my own UnityEvents and their respective EditorGUI, but this is a pet project I work on w-e for free, which means I don't have 2 weeks to spend on that.
I have a somewhat close to working solution, but the code is scary, black magic, wrong, dangerous, non-production-ready and will easily break on Unity updates. Because of that, I wont post it here, but what I want to do is definitely hackable :)
BTW, that Unity Event$$anonymous$$anager tutorial is garbage. You shouldn't use it. Just use normal C# delegates, and if you want a messaging system, register the listeners on a singleton with public Events(C#). Done, XD
Thank you again for helping! Really appreciated :)
Ok. Now I got it.
I really misunderstand your question.
Yes, the Event$$anonymous$$anager tutorial is really an introduction. I like to use something like the relay patterns from JoeStrout for instantiated stuff. For In-scene stuff, I like to use a more direct approach like the one you are using. That`s the best way I found so far in order to reduce dependence injection.
I'm not sure if there`s an easy way to set UnityEvent at editor time by code. Eventually, I will test that this week and comment here but I don`t recall of any previous experience on that.
Please, let us know if you advance on that.
Yes it's a little weird. In a way, I'm mainly interested in the great UI of UnityEvents. Traditional delegates usually work fine for programmatic stuff in my experience. This time though, I am making tools for someone else, which is a really fun challenge in and of itself. Using an inspector that he is comfortable with is a big bonus.
I got it working quite well actually, but I am hacking around internal stuff :) You can even change the target if you want (though you can't set scene objects inside a ScriptableObject
).
All you need is a little reflection magic, nothing is really private in C# [wink wink]. I'm basically accessing the parameters inside UnityEvent. They're stored in a variant/union thing, but with an enum for types ( http://docs.unity3d.com/ScriptReference/Events.PersistentListener$$anonymous$$ode.html ). Then I use traditional reflection to call my methods on newly created objects.
Cheers
Answer by rumaniel · Apr 07, 2016 at 11:29 AM
I use UnityEditor.Events.UnityEventsTools.AddPersistentListener();
Check this docs.
http://docs.unity3d.com/ScriptReference/Events.UnityEventTools.AddPersistentListener.html
Your answer
Follow this Question
Related Questions
Correct way to send an event to a specific enemy 0 Answers
How to handle multiples instances of an object raising the same event? 0 Answers
Make prefab that generated more than once listener in Unity Events 0 Answers
Animation Event not working properly 0 Answers
Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers