Any way to tell Unity to generate the .sln file via script or command line?

Is there any way to tell Unity to generate the solution files through script (perhaps some undocumented API) or through the command line?

For context - I’d like to run some code analysis tools on my C# code, on a build machine, from the command line. Many of these tools operate on solution files rather than directories of source files.

I don’t want to check in the solution files - that way madness lies.

As far as I can see they’re only generated upon opening a script or the solution from within the Unity editor. Running a command line build doesn’t seem generate any solution files, even in Library/ or Temp/.

2 Likes

Bump!

Isn’t this something anyone else would find useful?

It’s a bit late for this reply, but I found this thread while investigating this problem for similar purposes here.

It’s a slightly hacky solution, but you can use EditorApplication.ExecuteMenuItem to call Assets/Create C# Project to create .sln and .csproj files.

EditorApplication.ExecuteMenuItem("Assets/Open C# Project");

This works at least in 5.4.2f2, but it’s likely it’ll work in older versions of Unity. In much older versions, that same option was called something different, but it might work in those too.

2 Likes

That’s a great idea Chippit, thanks! Do you know if this works from a build script called using batch mode and ‘-executeMethod’?

I ended up writing a Python script to generate solution and project files - it’s not perfect (as it requires some knowledge of the Unity project structure) but it’s enough to generate a solution file that many command-line tools (e.g. ReSharper) are happy with.

This is precisely how I have used it, so I can confirm that that works. Unlike when clicking the menu item from a normal running instance of Unity, it also does not attempt to shell execute the file so it won’t open it in VS/Monodev/whatever editor you use when you do it from batch mode.

1 Like

Incredible - thanks again, this is a great tip! I’ll switch over to your method as soon as my Python version gets out of date as I’m certain it will :slight_smile:

It works! :smile:

Could someone please elaborate on where does “EditorApplication.ExecuteMenuItem(“Assets/Open C# Project”);” need to go in order to generate solution file from command line?

Also, has anyone tried this in headless Unix environment (I’m using docker image for builds)?

Hi Ivan,

You need to put this code in a static function inside a class then call it like so from your terminal:

"/Applications/Unity2017.3.1p4/Unity.app/Contents/MacOS/Unity" -batchmode -logFile -quit -projectPath "/Users/Developer/Documents/MyUnityProject" -executeMethod "MyClass.MyFunction"

1 Like

This generates a “Library” folder, but doesn’t seem to be generating any .csproj or .sln files for me. I’m on Win10.

I put this in a .cs class in my project:

public static void createSLN()
    {
        UnityEditor.EditorApplication.ExecuteMenuItem("Assets/Open C# Project");
    }

and then used this to call the method:

"C:\Program Files\Unity\Editor\Unity.exe" -batchmode -logFile -quite -projectPath <Project/Path>" -executeMethod "Game.createSLN"
1 Like

Is your class definitely called ‘Game’? Is it available to the editor, i.e. in an ‘Editor’ folder? You’ve misspelled ‘-quit’.

Also something catching me out recently is that sometimes VSTU doesn’t connect properly; I had to hook it up by choosing the external script editor in the preferences screen of Unity (find and choose devenv.exe).

On our CI we use the Unity Test Runner to run some tests:
https://docs.unity3d.com/Manual/PlaymodeTestFramework.html

Unity.exe -runTests -projectPath PATH_TO_YOUR_PROJECT -testResults C:\temp\results.xml -testPlatform editmode

To run the tests Unity creates a .sln used to build the project code.
I think it is pretty convenient to run CA&SA after that.

I hope this may help someone.

2 Likes

It seems like it doesn’t generate solution file anymore. OR it generates it in Temp folder which is deleted after the test is done

You may call -executeMethod with copy of this method.

For Unity ~2019.2+ this will change to some new api like CodeEditor.Editor.Current.SyncAll();

3 Likes

Turns out, you don’t even need a wrapper function. You can directly call UnityEditor.SyncVS.SyncSolution() from commandline. :slight_smile:

Unity.exe -projectPath PROJECT_PATH -batchmode -quit -nographics -logFile - -executeMethod "UnityEditor.SyncVS.SyncSolution"

10 Likes

This requires your project to compile. We have an issue with auto generated code (from Entitas). The auto-generated code is gitignored, so I wanted to generate it as the first step of my CI/CD build. However this requires a .csproj to be generated first.
Unfortunately, running UnityEditor.SyncVS.SyncSolution from command line fails, as the project doesn’t compile. This is strange, because it’s possible from Unity’s menu item Assets/Open C# Project.

Any ideas?

@mdab121 I suppose the correct workaround for this is to ensure you code compile when calling SyncSolution from command line.

I purposely made a error in my c#:

Scripts have compiler errors.```
Unity wont even call SyncSolution from command line (so no .sln or .csproj will be generated).
I dont know about any workaroud to avoid unity to abort batchmode.

Unity already has a suitable static method. Generating the solution is just this one command, no need to add editor script

Unity.exe  -batchmode -nographics -logFile - -executeMethod UnityEditor.SyncVS.SyncSolution -projectPath . -quit
4 Likes

But how to do it with already opened unity. I use sublime text and now 2019.3 ignores creation of sln files.

You can’t unity can’t have two open editor to the same project.