Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by wontonst · Jun 11, 2015 at 10:35 AM · onclickevent-handling

Custom Event Handler in Inspector?

So in a UI Button, there's a nice and handy OnClick field where you can add objects and a function attached to the object to call, so that when the button is pressed all of the functions in the OnClick will be called.

Is there a way where I can use this feature to store functions and have it so I can call a function that calls all of the stored functions? Something like

 this.eventhandler.callAll();
Comment
Add comment
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

3 Replies

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

Answer by nockieboy · Sep 03, 2015 at 05:41 PM

@wontonst Crikey - I hope it's not too late to answer this, but I was hunting around to remind myself how to do this as it's been a while and couldn't remember the magic words. :)

Anyway, it's easily done:

 using UnityEngine.Events;
 
 public UnityEvent yourCustomEvent;
 
 public void Foo() {
     // Trigger the event!
     yourCustomEvent.Invoke();
 }

Here's more info.

Comment
Add comment · Show 5 · 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 wontonst · Sep 03, 2015 at 11:29 PM 0
Share

perfect, thanks!

avatar image sotirosn · Feb 15, 2016 at 12:41 AM 1
Share

Unfortunately this doesn't work if the object is being handle by a custom inspector. I imagine there is a EditorGUILayout.EventField(yourCustomEvent) but I cannot find one.

avatar image zhuchun · Jun 16, 2016 at 08:50 AM 0
Share

Thank you! This is super handy for level designer :)

avatar image Simplexws · Dec 05, 2016 at 11:57 AM 0
Share

Thank you very much! Just wonderful.. :))))))))

avatar image Talmagett · Dec 18, 2020 at 02:55 PM 0
Share

It's very useful, and makes easier to work Thanks man

avatar image
5

Answer by Pitrizzo · Aug 16, 2016 at 05:04 PM

@sotirosn It works using Custom inspector. You just have to use serialized property EditorGUILayout.PropertyField (eventTest_Prop);

The Editor Script should looks like this:

     using UnityEngine;
     using UnityEditor;
         
         [CustomEditor(typeof(YourScriptName)), CanEditMultipleObjects]
         public class YourScriptNameEditor : Editor {
         
             public SerializedProperty yourCustomEvent_Prop;
         
                 void OnEnable () {
                      yourCustomEvent_Prop = serializedObject.FindProperty ("yourCustomEvent");
                 }
     
                 public override void OnInspectorGUI () {
                      EditorGUILayout.PropertyField (yourCustomEvent_Prop);
 
                      serializedObject.ApplyModifiedProperties ();
                }
             }








Comment
Add comment · Show 2 · 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 AdmiralThrawn · Jan 03, 2018 at 10:04 AM 0
Share

Works like a charm. For some other examples from Unity check this out: property-drawers-custom-inspectors

avatar image Tommy4421 · Feb 22, 2020 at 10:54 AM 0
Share

Don't to forget to call base.OnInspectorGUI(); if you do the above. Else all the other SerializeFields are not exposed to the editor. So:

 public override void OnInspectorGUI() {
     base.OnInspectorGUI();
     EditorGUILayout.PropertyField(onPublishProperty);
     serializedObject.Apply$$anonymous$$odifiedProperties();
 }
avatar image
0

Answer by DSivtsov · Nov 20, 2020 at 05:33 AM

In my case works correctly the variant

 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();
     //EditorGUILayout.PropertyField(yourCustomEvent_Prop);
     serializedObject.ApplyModifiedProperties();
 }

In other case the field in Inspector double. And Don't forget:

  • The script "YourScriptNameEditor" must be in folder Assets\Editor and named as "YourScriptNameEditor.cs"

  • In "typeof(YourScriptName)" - YourScriptName - name of script where you use the "yourCustomEvent"

  • "yourCustomEvent" - It's the name of your event

all other can be unchangeable

Comment
Add comment · 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

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

10 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

Related Questions

How do you make an animation start from a location dynamically? 2 Answers

Unity 3d 4.6b New GUI system doesn't take touch when already a touch event is occurring in 3d scene 0 Answers

How to add onPointerDown event on SpritRenderer GameObject 2 Answers

How do i use 2 GUITexture as buttons in C# 1 Answer

Having issues determining a way to use buttons in a script. 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