Automatically open Rider / IDE when opening a Unity project?

When I open my Unity project, I always need to click around to find a script and double click that to open it and that way for Rider to get sorted. Is there someway that Rider will simply open as soon as I open the Unity project?

1 Like

Seeing as AssetDatabase.OpenAsset can be used to open a script in an IDE, Iā€™m sure thereā€™s some way to concoct an editor script to do this.

2 Likes

Nice one! Thanks for the inspiration.

Since Iā€™m currently hacking away at the AssetDatabase to modernize the API and I just happen to have ā€œOpenAssetā€ next on my todo list (also a Rider user with the same ā€˜issueā€™ kind of) Iā€™ll definitely give this a shot tomorrow. :wink:

Note: Iā€™m kinda thinking this would actually be a job for Application.OpenURL()

So hereā€™s my minimally over-engineered solution with help by @ (see below) and now available as a package.

#if UNITY_EDITOR
using System;
using Unity.CodeEditor;
using UnityEditor;

namespace CodeSmile.Editor.Tools.IDE
{
    /// <summary>
    ///     Will launch the IDE associated with .sln files when the Unity project is opened.
    /// </summary>
    public class OpenSolutionOnStartup
    {
       private const String SessionStateKey = "CodeSmile.Editor.Tools.IsProjectLaunching";

       [InitializeOnLoadMethod] private static void OnLoad()
       {
          if (IsProjectLaunching())
             TryOpenSolution();
       }

       [MenuItem("CodeSmile/Open Solution")]
       private static void TryOpenSolution() => CodeEditor.Editor.CurrentCodeEditor.OpenProject();

       private static Boolean IsProjectLaunching()
       {
          var launching = SessionState.GetBool(SessionStateKey, true);
          if (launching)
             SessionState.SetBool(SessionStateKey, false);

          return launching;
       }
    }
}
#endif

Put this script in an ā€œEditorā€ folder or editor assembly. Every time you open this project it will open the solution, even if the solution file had been deleted. You also get a Menu Item to ā€œOpen Solutionā€ under ā€˜CodeSmileā€™.

The application that opens is the one that has registered ā€˜.slnā€™ as its associated extension (meaning: it works with Rider, VS, VSCouldButNoThanks).

2 Likes

In theory you donā€™t need more than this if you donā€™t provide callbacks and whatnotā€¦
OpenIdeOnStart.cs in an Editor folder.

using Unity.CodeEditor;
using UnityEditor;

namespace YourNameSpace
{
    public static class OpenIdeOnStart
    {
        [InitializeOnLoadMethod]
        private static void OpenIdeOnStartMethod() =>
                CodeEditor.Editor.CurrentCodeEditor.OpenProject();
    }
}

ps: if you want to open a concrete file every time, the OpenProject method has three parameters.

1 Like

Thank you very much, works perfectly! This will save me a lot of time in the long run, fantastic!

2 Likes

With InitializeOnLoad the issue is that this runs after every domain reload. I assume its the same for the Method attribute.

This means you change a script in the IDE, tab into Unity, which recompiles, and then the IDE pops into foreground because the solution gets opened. :wink:

But perhaps this CodeEditor behaves differently, didnā€˜t know about that.

Well, it does not do it on my machine, if the IDE is already opened, it does not pull it into the foreground (although I only tested extensively with Rider).

1 Like

I gave it a go and updated my script accordingly. On my system, Rider kept popping into foreground after every script change so I let the session state check in there.

The good thing about CodeEditor is that it works even if there is no solution file, so thereā€™s no delayCall necessary, and it also works if Unity is loading the project in the background.

1 Like

Strange, my Rider doesnā€™t pop up. But tested on VSC and that pops up. So here is a slightly more complicated solution, but still on the same path:

using Unity.CodeEditor;
using UnityEditor;

namespace LurkingNinja.Editor.IDE
{
    public static class OpenIdeOnStart
    {
        private const string STATE_KEY = "LN.IDE_OPENED";

        private static bool ProjectAlreadyOpenedState
        {
            get => SessionState.GetBool(STATE_KEY, false);
            set => SessionState.SetBool(STATE_KEY, value);
        }
        
        [InitializeOnLoadMethod]
        private static void OpenIdeOnStartMethod()
        {
            if (ProjectAlreadyOpenedState) return;
            CodeEditor.Editor.CurrentCodeEditor.OpenProject();
            ProjectAlreadyOpenedState = true;
        }
    }
}

ps: SessionState it is thenā€¦ also now that I installed VSC, Rider too started to pop-up with the previous code. Strange. Maybe Windows 10 quirk?

1 Like

SessionState is better though because unlike EditorPrefs the session state has the lifetime of the current editor process.

1 Like