- Home /
Need help with Editor script
Hey all,
I'm needing to make an editor script which a) starts automatically to init some stuff when I open my project (possible?) b) accepts some input (I know how to make a Wizard, is that the only option?) and c) periodically runs something (like Update, doesn't have to be every frame though).
Is that all possible?
Answer by numberkruncher · Jan 16, 2013 at 01:59 AM
a) starts automatically to init some stuff when I open my project (possible?)
Yes this is possible:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class SomeEditorScript {
static SomeEditorScript() {
// Do your stuff here...
}
}
See: http://docs.unity3d.com/Documentation/Manual/RunningEditorCodeOnLaunch.html
b) accepts some input (I know how to make a Wizard, is that the only option?)
You can create your own custom interfaces by creating your very own editor windows. As of the time of writing you cannot automatically display a window when your script first loads, but you can add a custom menu item which displays your interface:
using UnityEngine;
using UnityEditor;
public static class SomeEditorWindow : EditorWindow {
[MenuItem("Your Stuff/Some Editor Window")]
static void Show() {
GetWindow<SomeEditorWindow>();
}
void OnEnable() {
title = "My Editor Window";
}
void OnGUI() {
GUILayout.Label("Place your controls here!");
if (GUILayout.Button("Foo")) {
// Do something here!
GUIUtility.ExitGUI();
}
}
}
See: http://docs.unity3d.com/Documentation/ScriptReference/EditorWindow.html
c) periodically runs something (like Update, doesn't have to be every frame though).
There are several ways to do this depending upon what you are trying to achieve. The most generic solution is to attach an event handler as demonstrated below. This will frequently invoke your custom event handler. You can manually limit how often your logic is executed using the .NET DateTime
class.
void SomeFunctionToInitializeEvent() {
EditorApplication.update += delegate {
// Do your stuff here!
};
}
// or alternatively you can use this syntax:
void SomeFunctionToInitializeEvent() {
EditorApplication.update += new CallbackFunction(MyEventHandler);
}
void MyEventHandler() {
// Do your stuff here!
}
See: http://docs.unity3d.com/Documentation/ScriptReference/EditorApplication-update.html
The following documentation includes references to other callbacks that may be of use to you: http://docs.unity3d.com/Documentation/ScriptReference/EditorApplication.html
It is also worth noting that you can use the Update
message in your custom EditorWindow
which is called at regular intervals so long as the window is open.
I hope that this is of help to you.
@web No problem, glad that I could help :-) Happy coding!
Your answer
![](https://koobas.hobune.stream/wayback/20220613091015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem scale ui custom font text 0 Answers
One button doesn't cycle through an array when pressed. 1 Answer
Perform action on save/load in editor 2 Answers
Custom Asset Icons? 2 Answers
Editor script - any callback when target is first instantiated? 1 Answer