- Home /
Question by
whisker-ship · Jul 07, 2017 at 07:41 PM ·
c#editorsceneeditor-scriptingprogramming
How can I make function run in the editor only when I press a button in the inspector?
I'd like to create a script that has a button that shows in the inspector. When that button is pressed I want it to run a certain function (withing that same script) in the unity editor.
Kind of like [ExecuteInEditMode] but only when I press a button I created in the inspector instead of every frame.
Comment
Best Answer
Answer by CoryButler · Jul 08, 2017 at 03:35 AM
Creating a custom editor window may work well for you. Here is an example.
using UnityEngine;
using UnityEditor;
public class EditModeFunctions : EditorWindow
{
[MenuItem("Window/Edit Mode Functions")]
public static void ShowWindow()
{
GetWindow<EditModeFunctions>("Edit Mode Functions");
}
private void OnGUI()
{
if (GUILayout.Button("Run Function"))
{
FunctionToRun();
}
}
private void FunctionToRun()
{
Debug.Log("The function ran.");
}
}
Let this script compile in Unity. Then, select Window > Edit Mode Functions.
Now, just click the "Run Function" button to run your function in edit mode.
windowdropdown.png
(3.9 kB)
editmodefuncwindow.png
(11.3 kB)
Your answer
![](https://koobas.hobune.stream/wayback/20220612125741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Custom Editor Script resets values on Play 1 Answer
Z sorting of Handles 0 Answers