Skip to content

Instantly share code, notes, and snippets.

@nicetrysean
Created August 18, 2022 12:10
Show Gist options
  • Save nicetrysean/84829707394079661d31a37d632c867c to your computer and use it in GitHub Desktop.
Save nicetrysean/84829707394079661d31a37d632c867c to your computer and use it in GitHub Desktop.
Manage domain reloads. Useful when Rider triggers a domain reload, and then Unity redundantly triggers one upon entering playmode
public static class CustomDomainReloadTiming
{
private const string MenuPath = "Tools/Manage Domain Reload";
private const int PlaysAllowedWithoutDomainReload = 1;
private static int _playCountWithoutDomainReload = 0;
[MenuItem(MenuPath, true)]
static bool ValidateLogSelectedTransformName()
{
Menu.SetChecked(MenuPath, IsEnabled);
return true;
}
[MenuItem(MenuPath)]
private static void EnableDelayedReload()
{
bool value = false;
IsEnabled = value = !IsEnabled;
if (!value)
{
EditorSettings.enterPlayModeOptionsEnabled = false;
EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.None;
}
}
private static bool IsEnabled
{
get => EditorPrefs.GetBool("ReloadAssetOnExit");
set => EditorPrefs.SetBool("ReloadAssetOnExit", value);
}
[RuntimeInitializeOnLoadMethod]
private static void Init()
{
if (IsEnabled)
{
EditorSettings.enterPlayModeOptionsEnabled = true;
EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload | EnterPlayModeOptions.DisableSceneReload;
EditorApplication.playModeStateChanged += PlayModeChanged;
}
_playCountWithoutDomainReload = 0;
}
private static void PlayModeChanged(PlayModeStateChange obj)
{
if (_playCountWithoutDomainReload>=PlaysAllowedWithoutDomainReload)
{
switch (obj)
{
case PlayModeStateChange.ExitingEditMode:
AssetDatabase.Refresh(ImportAssetOptions.Default);
CompilationPipeline.RequestScriptCompilation();
break;
}
}
if (obj == PlayModeStateChange.EnteredPlayMode) _playCountWithoutDomainReload++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment