Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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
9
Question by NateJC · Aug 03, 2015 at 10:52 PM · editorinspectorfunctiondelegatepointer

Unity editor inspector delegate / function pointer?

I would like to be able to add a public variable to my script that can accept a game object and then select a public function to execute. Basically, I want to do something exactly like this screenshot in my own custom script, but I'm not sure how to accomplish it:

Inspector delegate

inspector-delegate.png (3.9 kB)
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

2 Replies

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

Answer by zach-r-d · Aug 04, 2015 at 11:24 AM

This can be accomplished with UnityEvents. The usage is nice and simple too! There is a little bit of weirdness to deal with if the event takes parameters, though.

 using UnityEngine;
 using UnityEngine.Events;
 
 public class ExecuteStuffOnClick : MonoBehaviour {
     public UnityEvent myUnityEvent;
 
     void Awake() {
         if (myUnityEvent == null)
             myUnityEvent = new UnityEvent();
     }
 
     public void OnMouseDown() {
         myUnityEvent.Invoke();
     }
 
 }


Comment
Add comment · Show 9 · 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 NateJC · Aug 04, 2015 at 11:06 PM 1
Share

This is exactly what I was looking for, thanks! If you have more than one function added to the inspector list, will myUnityEvent.Invoke() call all of them at once?

avatar image zach-r-d · Aug 04, 2015 at 11:15 PM 0
Share

Yep! It's just like a C#/mono event, but with a nice inspector GUI. :)

avatar image Leon700 · Jun 25, 2016 at 06:11 PM 1
Share

Thanks. This helped a lot ^^

avatar image AdamBL · Jul 26, 2017 at 05:01 PM 2
Share

Hey I know I'm a couple years late but thanks for this answer. Seems like a relatively simple/important thing to be able to do, and indeed it is very simple I just didn't know about it. Thanks again!

avatar image Andrew_Zim · Nov 03, 2017 at 09:33 AM 0
Share

Yes, this way is good and simple, but it lets to serialize only methods with one, or without arguments. Did somebody know how to serialize methods with two or more args?

avatar image zach-r-d Andrew_Zim · Nov 03, 2017 at 04:34 PM 0
Share

@Andrew_Zim : UnityEvent works with up to 4 arguments, see https://docs.unity3d.com/ScriptReference/Events.UnityEvent_4.html Beyond 4, you'll have to roll your own custom solution.

Show more comments
avatar image
0

Answer by DerDicke · Aug 03, 2018 at 07:22 AM

This bugged me too. My solution is to use an interface. Implement your function using the interface, than in your 'sender' use GetComponent(); or like I did, make your Inspector field take a Monobehavior instead of a GameObj for even more control. In both cases you can write an custom Inspector and check if interface is present. If not, draw a messageBox and reject the dragged obj.

Has other advantages. You can communicate both ways (return values) and have more than one function.

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

32 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

Related Questions

Delegates in the Inspector 1 Answer

What should be handled in the custom inspector version of a script? 1 Answer

How to record hideFlags for Undo/Redo 0 Answers

Exposing Enums in Custom Inspector 1 Answer

How to make a serialized editor only field 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