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 /
  • 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 [this][1] here, which 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)
         {
             this.from = from;
             this.to = to;
             this.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;
             while (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);
             }
         }

This 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

@hexagonius Thanks, it worked - as long as my function doesn't accept more parameters than 1. However, I have a collection of functions where I use at least 3 parameters. How to make it work for 3 or more parameters?

avatar image
1

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 this example below. This 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)this.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.

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

Change Function On Button Press? 0 Answers

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Unity 2020.1 changing Script triggers complete reimport? 0 Answers

Unity editor toggle Sceneviews skybox and effects 0 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