- Home /
Any way to load a script when Unity is opened?
I wondered if perhaps there was an undocumented file location where Unity could load a script from as it starts up. Or… is there any hook of any kind that would let you do something upon startup?
Thanks
Steve
Thanks, that's going to be useful for so many things. Presumably it still means opening an existing project to which a startup script has already been added. It would be great if it could be added to a 'New Project' template somehow, so it's guaranteed to be there. $$anonymous$$y final goal is to be able to start a new project and have it load a set of essential assets. At the moment I have a script that I add first, then run that.
Answer by HarshadK · Sep 17, 2014 at 07:55 AM
Yes. Check -executeMethod argument from the Command line arguments manual page which states the usage of it as:
Execute the static method as soon as Unity is started, the project is open and after the optional asset server update has been performed. This can be used to do continous integration, perform Unit Tests, make builds, prepare some data, etc. If you want to return an error from the commandline process you can either throw an exception which will cause Unity to exit with 1 or else call EditorApplication.Exit with a non-zero code. If you want to pass parameters you can add them to the command line and retrieve them inside the method using System.Environment.GetCommandLineArgs.
To use -executeMethod, you need to place the enclosing script in an Editor folder. The method to be executed must be defined as static.
Example code from that page itself:
// C# example
using UnityEditor;
class MyEditorScript
{
static void PerformBuild ()
{
string[] scenes = { "Assets/MyScene.unity" };
BuildPipeline.BuildPlayer(scenes, ...);
}
}
Thanks, that very much answers my original question. It seems there is a chicken-and-egg situation though, where this can't work on a new project, only on an existing project to which you have added this script. (In which case, it was already possible to have one key press to invoke a startup script). Since reading yours and GameVortex's replies, I'm wondering if Unity exposes the details of a New Project template anywhere?
As long as I know, it does not. But one method is to create a package with your own project template and load that in your project at the startup using -importPackage command line argument.
Answer by diliupg · Jul 14, 2020 at 04:22 AM
You can duplicate a projectTemplate which is in the Editor\Data\Resources\PackageManager folder and edit the Json file to load your own scripts on any project startup. I have edited the 3D template and the URP template to load my own scripts on startup and also edited the script template to carry my own message.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
itween move to do not end(even it is) 0 Answers
Fps Aiming Script help 2 Answers