• 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 /
  • Help Room /
avatar image
1
Question by agiro · Feb 07, 2017 at 05:34 PM · c#editorfunctionevent

Exposing a function to the inspector

I wish to expose a script's function to the inspector, but I have no clue how to. I found [t$$anonymous$$s][1] here, w$$anonymous$$ch was a start, but it isn't clear how to finish.

My code for the class:

 [System.Serializable]
     class MoveAction : UnityEvent<MonoBehaviour>, IUIAnimationEvent
     {
         #region Move
         public MoveAction()
         {
 
         }
         //to ensure only one mover coroutine can be active.
         IEnumerator moveRoutine = null;
         #region Solution 2: using fields and not parameters
         Transform from;
         Transform to;
         float overTime;
         public delegate void UIchain(MonoBehaviour mono);
         public event UIchain NEXT_FUNCTION;
         
         public MoveAction(Transform from, Transform to, float overTime, IUIAnimationEvent chain)
         {
             t$$anonymous$$s.from = from;
             t$$anonymous$$s.to = to;
             t$$anonymous$$s.overTime = overTime;
 
         }
         public void Move(MonoBehaviour mono)
         {
             if (moveRoutine != null)
             {
                 mono.StopCoroutine(moveRoutine);
             }
             moveRoutine = _Move(from, to, overTime, mono);
             mono.StartCoroutine(moveRoutine);
             Invoke(mono);
         }
         IEnumerator _Move(Transform from, Transform to, float overTime, MonoBehaviour mono)
         {
             Vector2 original = from.position;
             float timer = 0.0f;
             w$$anonymous$$le (timer < overTime)
             {
                 float step = Vector2.Distance(original, to.position) * (Time.deltaTime / overTime);
                 from.position = Vector2.MoveTowards(from.position, to.position, step);
                 timer += Time.deltaTime;
                 yield return null;
             }
             if(NEXT_FUNCTION != null)
             {
                 NEXT_FUNCTION(mono);
             }
         }

T$$anonymous$$s is a lot of code, but the interesting part is the Move(MonoBehaviour mono) function. I want to expose that to the editor. How? [1]: http://answers.unity3d.com/questions/998183/select-monobehaviour-function-from-inspector.html

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
2

Answer by hexagonius · Feb 07, 2017 at 10:51 PM

you cannot expose a function (it's called method in C#). you can however expose a unity event (like in your posted question). all you need is

 [System.Serializable]
      public class MoveAction : UnityEvent<MonoBehaviour>{}

and in a different class

 public MoveAction moveAction;
Comment
Add comment · Show 1 · 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 agiro · Feb 08, 2017 at 10:34 AM 0
Share
avatar image
2

Answer by ChristianLaLarsson · Oct 13, 2019 at 07:20 PM

I found the answer I was looking for here: https://gamedev.stackexchange.com/questions/136996/unity-exposing-a-function-to-the-inspector?newreg=f40cf113944e4884b47fb09e2f16dc41

what you are looking for is the Unity Event class. See t$$anonymous$$s example below. T$$anonymous$$s will appear in the editor exactly like your screenshot.

 using UnityEngine;
 using UnityEngine.Events;
 using System.Collections;
 
 public class InvokeOnAwake : MonoBehaviour {
 
     public UnityEvent invokeMethod;//set in editor
 
     void Awake(){
         invokeMethod.Invoke();
     }
 
 }

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
avatar image
0

Answer by UnityCoach · Feb 08, 2017 at 10:41 AM

If what you want is a button that you can click in the inspector to do some editor work, you need to make a custom inspector.

 [CustomEditor(typeof(YourClassName))]
 [CanEditMultipleObjects] // only if you handle it properly
 public class YourClassNameEditor : UnityEditor.Editor
 {
     public override void OnInspectorGUI()
     {
         if (GUILayout.Button("DO THAT", EditorStyles.miniButton))
         {
             (YourClassName)t$$anonymous$$s.target).YourMethod (parameters);
         }
         DrawDefaultInspector ();
     }
 }

You will also need to handle Undo then.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

315 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 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 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 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

How do I make a credits button? 1 Answer

Unity 2020.1 changing Script triggers complete reimport? 0 Answers

How to Have a ReoderableList in a Custom Editor Window 1 Answer

UnityAction Vs UnityEvent 1 Answer

Use a class like a function 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