Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
12
Question by drastick · Jan 29, 2011 at 06:35 PM · editorstart

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?

Comment
Add comment · Show 1
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 yoyo · Jan 29, 2011 at 09:51 PM 0
Share

(EDIT: add editor tag)

4 Replies

· Add your reply
  • Sort: 
avatar image
29
Best Answer

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;
     }
 }
Comment
Add comment · Show 13 · 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 yoyo · Jan 31, 2011 at 08:11 PM 0
Share

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

avatar image hellcats · Jan 31, 2011 at 11:11 PM 0
Share

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.

avatar image drastick · Feb 01, 2011 at 05:46 PM 0
Share

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.

avatar image drastick · Feb 01, 2011 at 05:49 PM 0
Share

One additional note, which I can work around, this auto init will be called again any time you add/remove a script file...

avatar image danieltranca drastick · Feb 06, 2016 at 03:55 PM 0
Share

What was the workaround? :)

avatar image EricCruden · May 26, 2020 at 10:50 AM 2
Share

@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.

avatar image Ian094 EricCruden · Dec 18, 2021 at 06:40 PM 0
Share

this is exactly what I needed! thank you :)

Show more comments
avatar image
3

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();
             }
         }
     }
 }
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
1

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

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 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.

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 drastick · Jan 29, 2011 at 09:05 PM 1
Share

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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