- Home /
How i can subscribe to an Event through Inspector
Hi there!
I want to implement a simple feature for the editor, that able subscribe to an event through inspector object field. I will try to explain as much detail as I can.
Here is my code:
[Serializable]
public class SomeField
{
public UnityEvent onEvent = new UnityEvent();
public void Raise()
{
onEvent.Invoke();
}
}
[CreateAssetMenu]
public class SomeExternalField : ScriptableObject
{
public SomeField property;
public void Raise()
{
property.Raise();
}
}
public class SomeMonoBehaviour : MonoBehaviour
{
public SomeExternalField externalPropertyReference;
public void OnExternalEvent()
{
Debug.Log("OnExternalEvent");
}
}
Here is workflow
1. Create a SomeExternalField scriptable object;
2. Add SomeMonoBehaviour to a GameObject in a scene;
3. Select externalPropertyReference field in the SomeMonoBehaviour and choose the SomeExternalField from the assets;
4. In this moment i need subscribe SomeMonoBehaviour. ExternalStart() method to SomeExternalField.SomeField.onEvent event;
5. Enter Play Mode
6. Call SomeExternalField.Raise() and see "OnExternalEvent" in the console;
How i tried to find a solution
1. First that i do it was an Editor works. I wrote a PropertyDrawer, that used a UnityEvent.AddListener to subscribe. All works perfectly while Editor does not enter play mode, after that all subscribers were lost;
2. Yes, now i know that is AddListener is only works runtime and for editor need to use UnityEventTools.AddPersistentListener, but in my case it is impossible, because SomeField is not inherit a UnityEngine.Object;
3. I try to use standard C# events with delegates, but it works similar as 1st point;
4. Only one thing is working - initialise events on Awake(), but if some class will have a 10 or 20 "SomeFields", i will need to each initialise, this is ugly and so not practical;
Guys, I know my English is not very good and I hope that reading this topic will not cause you too much inconvenience. This problem has captured my mind and I can't get out of it.
May be someone faced a sort of problem and knows how to solve it or say that it is very difficult to implement and it is not worth it, that I would not get stuck at this point and continued to work further.
Thank you!
So you want it subscribed and receiving event notifications by runtime? Have you tried just hooking that up into some helper behavior's Awake()?
I want it subscribed in editor mode and then receive notifications by runtime. Yes i tried using Awake(), but all my solutions is required a personal approach to each field, that is hard when some class have 10 - 20 fields.
Or you're talking about an automated system that uses System.Reflection?
By default a UnityEvent is exposed in the editor, allowing you to assign event actions from file-system files OR if it is in a scene, scene objects. Since you state that your object is not a UnityObject, then I''d have to say that using UnityEvents won't work, you'll need to either make SomeField's object a Scriptable Data Object which can't be serialized at run-time, but depending on your uses, it may work. Otherwise I suggest using an indepth event/delegate system and create something that works for you.
I'm afraid that my knowledge about editor serialisation is not enough to write an in-depth system of events/delegates. Anyway, I'll think about how I can rebuild my "SomeField" to using UnityEngine.Object. Thank you for the answer!