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?
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.
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.
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).
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.
Thank you very much, works perfectly! This will save me a lot of time in the long run, fantastic!
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.
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).
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.
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?
SessionState is better though because unlike EditorPrefs the session state has the lifetime of the current editor process.