- Home /
Can I auto run a script when editor launches or a project finishes loading?
Basicly I have a function that needs to run for my project every time I start the editor. It is too much work to haveing to manually select a menu command every time.
static public class StartUp { static public void StartUpTask(){ ...; } }
I know that I can use a command line to launch the editor with "-executeMethod Startup.StartUpTask" but this too is annyoning to launch that particular project via a shortcut everytime as well!
Is there anywhere I can hook Startup.StartUpTask(); To run reguadless of the scene or having to select a menu command? Is there some editor class I can derive from that will post notifications?
Answer by yoyo · Jan 31, 2011 at 08:05 PM
(All code in C#)
This works great ... save this as Autorun.cs in your Editor folder. The InitializeOnLoad attribute is the special sauce that makes it work. (I've deprecated my previous answer with the custom editor for Transform, this is a much better approach.)
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class Autorun
{
static Autorun()
{
Debug.Log("Autorun!");
}
}
If you want the scene to be fully loaded before your startup operation, for example to be able to use Object.FindObjectsOfType, you can defer your logic until the first editor update, like this:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class Autorun
{
static Autorun()
{
EditorApplication.update += RunOnce;
}
static void RunOnce()
{
Debug.Log("RunOnce!");
EditorApplication.update -= RunOnce;
}
}
p.s. InitializeOnLoad is undocumented, I learned about it in this answer from a Unity dev ... http://answers.unity3d.com/questions/32898/how-do-i-get-a-callback-every-frame-in-edit-mode
Perfect! Exactly what I was looking for. The only problem with Unity is a matter of documentation - it can do anything, if you just keep looking.
Amazing! That is absolutly what I needed, great find! Even better, when I edit scripts it re-inits which is even better for my case.
One additional note, which I can work around, this auto init will be called again any time you add/remove a script file...
@yoyo Since 5.6 you can use SessionState.GetBool
and SessionState.SetBool
ins$$anonymous$$d of EditorApplication.timeSinceStartup
. This a more robust way to make the code run only once at Unity Editor startup.
Answer by roroco · Aug 30, 2018 at 03:16 AM
InitializeOnLoad is not perfect, it will make editor script rerun on each "enter Play mode" or "exit Play mode", it will cause "Enter Play Mode" or "Exit Play Mode" very slow, so I use following if (!EditorApplication.isPlayingOrWillChangePlaymode)
to ignore this case and make script only run on unity editor startup
using UnityEditor;
namespace EditorScript.Ro.Startup
{
[InitializeOnLoad]
public class StartupMain
{
static StartupMain()
{
// InitializeOnLoad will cause run each InitializeOnLoad on enter or exit play mode, it will cause unity editor slow, so ignore this case
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
new AutoSave();
new ChangeUnityEditorWindowTitle();
new DisableAutoRefresh();
new NotifyMoveLibScriptsToStandardAssets();
new ReceiveRiderCmd();
}
}
}
}
Answer by Pascal1234 · Nov 21, 2013 at 11:14 AM
Hey guys, I have a little problem which is similar to this one. I did such an autorun class to view a menu in the scene view. My main Problem is, not every line of the code is executed at the beginning. If I open up a new scene then every part of the code which has an if statement it is not executed. something like:
//Create Skybox if there is none
Debug.Log("Look if there is a skybox");
if (RenderSettings.skybox == null)
{
Debug.Log("There is no skybox, I ll create it");
RenderSettings.skybox = skybox;
}
So the first Debug.log is shown and there is really no skybox but the second one isnt shown and it will not create a skybox until i edit something in the code, save that change and go back to unity. Then the script is called again and the skybox will be created
Answer by DaveA · Jan 29, 2011 at 07:15 PM
Look into this: http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html
Maybe you can make a Startup or Awake function with that.
I've seen that... However in order for that to execute I would have to add the script component to an object in a scene correct? I would like this to run no matter what scene loads up without having add anything to that scene.
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
run editor script when just returning from play mode 1 Answer
The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers
Tracking down causes of Editor errors. 1 Answer
Unity Editor crashing when editing scripts referenced from GameObjects 1 Answer