Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DrSpritz · Feb 21, 2018 at 03:20 PM · inspectoreditor-scriptingserializationeventeventsystem

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!

Comment
Add comment · Show 5
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 TreyH · Feb 21, 2018 at 03:30 PM 1
Share

So you want it subscribed and receiving event notifications by runtime? Have you tried just hooking that up into some helper behavior's Awake()?

avatar image DrSpritz TreyH · Feb 21, 2018 at 03:53 PM 0
Share

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.

avatar image DrSpritz TreyH · Feb 21, 2018 at 03:55 PM 0
Share

Or you're talking about an automated system that uses System.Reflection?

avatar image RobAnthem · Feb 21, 2018 at 03:33 PM 1
Share

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.

avatar image DrSpritz RobAnthem · Feb 21, 2018 at 04:02 PM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

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

87 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

Related Questions

BeginChangeCheck & EndChangeCheck not working? 1 Answer

Multiple scripts reference to the same SerializeField 1 Answer

Inspector foldout with inheritance 0 Answers

Adding custom BaseEventData to UnityEngine.EventSystems NOT Unity.Events 1 Answer

Help with editor serialization 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