Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Muravu · Aug 11, 2015 at 01:59 AM · guieditorinspectoreventcustomeditor

How to add UnityEvent using Custom Inspector?

Hi, I'm new to custom inspectors so please be patient. Let's say my code looks like this:

 public class ExampleClass : MonoBehaviour
 {
     public UnityEvent myEvent;
 }
 
 [CustomEditor(typeof(ExampleClass))]
 public class EventEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("Add event"))
         {
             ...
         }
     }
 }


So I have 1 UnityEvent and what I'm trying to do is add another one when I press a button in my custom inspector. I've been googling for hours now and I just can't get it to work.

I've tried something like this:

 [CustomEditor(typeof(ExampleClass))]
 public class EventEditor : Editor
 {
     SerializedObject _example;
 
     void OnEnable()
     {
         SerializedObject _example= new SerializedObject((ExampleClass)target);
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("Add event"))
         {
             SerializedProperty sprop = _example.FindProperty("myEvent");
             EditorGUILayout.PropertyField(sprop);
             _example.ApplyModifiedProperties();
         }
     }
 }

..but no luck :(

Help!

Comment
Add comment · Show 2
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 Nerevar · Aug 11, 2015 at 09:32 AM 0
Share

But what kind of event you would like to use? and what is the final aim? if it is not a top secret project, please give us the context so we can try to think of the best solution for your situation.

avatar image Muravu · Aug 11, 2015 at 10:27 AM 0
Share

The final aim is for my game/lvl designer to be able to manipulate objects using unityevents and trigger them as he pleases using colliders. Now he does have one unity event which he uses pretty often but now we need more. So i wanted to give him the ability to create his own with just one button and then figure out how to add public methods which would invoke these events... but i'm stuck on step one :)

1 Reply

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

Answer by stepan-stulov · Aug 11, 2015 at 09:56 AM

Seems like someone solved it:

http://answers.unity3d.com/questions/802786/46-how-do-i-draw-unityevent-in-custom-inspector.html

Comment
Add comment · Show 3 · 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 Muravu · Aug 11, 2015 at 10:35 AM 0
Share

Thank you for your reply but it doesn't do anything. Here is what my code looks right now:

  [CustomEditor(typeof(ExampleClass))]
  public class EventEditor : Editor
  {
  
      public override void OnInspectorGUI()
      {
          base.OnInspectorGUI();
  
          if (GUILayout.Button("Add event"))
          {
              SerializedProperty sprop = serializedObject.FindProperty("myEvent");
              
              EditorGUIUtility.LookLikeControls();
              EditorGUILayout.PropertyField(sprop);
 
              serializedObject.Apply$$anonymous$$odifiedProperties();
          }
      }
  }

When I press the button, really nothing happens. Unless I put some debug.log just so I know the button works...

avatar image stepan-stulov · Aug 11, 2015 at 11:34 AM 0
Share

Let me simply describe what you are doing:

You are showing the event field in the inspector only in one frame (milliseconds long) when the button gets clicked. Then you make no modifications to anything whatsoever. Then you ask to apply these (which) modifications.

Are you trying to add a new event on button click? Then do that on the button click, but move the actually rendering of the event out. I assume what you want is to show the event all the time unconditionally, not just for 2 milliseconds in the frame. Here is some pseudo-code of what I mean:

 if (GUILayout.Button("Add event"))
 {
     // add event to some list
 }
 
 foreach (UnityEvent unityEvent in unityEvents)
     EditorGUILayout.PropertyField(sprop);

If that is the task then I have a much more simple solution for. Simply declare a serializable field whose type is list of events. Unity already has ui logic for manipulating the lists without any extra editor code:

 [SerializeField] private List<UnityEvent> _events;
avatar image Muravu · Aug 11, 2015 at 03:08 PM 0
Share

Thanks! That's exactly what I needed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector Invocation behaviour? 1 Answer

Is there an event being fired off when the Inspector is being resized? 1 Answer

Are there any Editor fields which are Drag and Drop? 0 Answers

OnGUI Event 0 Answers

EditorGUILayout.TextField text get's deleted instantly. 2 Answers


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