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
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;
@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?
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();
}
}
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.