Initial Commit
This commit is contained in:
8
NoAutoGame/Editor.meta
Normal file
8
NoAutoGame/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6ade5b442638bd4086b19338f33ea09
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
151
NoAutoGame/Editor/NoAutoGame.cs
Normal file
151
NoAutoGame/Editor/NoAutoGame.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DreadScripts.NoAutoGame
|
||||
{
|
||||
internal static class NoAutoGame
|
||||
{
|
||||
#region Constants
|
||||
private const string GAME_WINDOW_TYPE_ASSEMBLY_FULL_NAME = "UnityEditor.GameView, UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
|
||||
private const string GAME_WINDOW_OPEN_SESS_KEY = "NoAutoGameWindowWasOpen";
|
||||
private const string LAST_WINDOWS_SESS_KEY = "NoAutoGameLastWindowsIDs";
|
||||
|
||||
private const string CLOSE_GAME_WINDOW_PREF_KEY = "NoAutoGameCloseGameWindow";
|
||||
private const string UNFOCUS_GAME_WINDOW_PREF_KEY = "NoAutoGameUnfocusGameView";
|
||||
private const int GAME_WINDOW_HANDLE_DELAY = 100;
|
||||
#endregion
|
||||
|
||||
private static Type _gameWindowType;
|
||||
private static Type gameWindowType => _gameWindowType ?? (_gameWindowType = Type.GetType(GAME_WINDOW_TYPE_ASSEMBLY_FULL_NAME));
|
||||
private static readonly PropertyInfo hasFocusProperty = typeof(EditorWindow).GetProperty("hasFocus", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
private static CancellationTokenSource cts;
|
||||
|
||||
private static bool closeGameWindow;
|
||||
private static bool unfocusGameWindow;
|
||||
private static EditorWindow[] previouslyFocusedWindows;
|
||||
private static void HandlePlaymodeChange(PlayModeStateChange change)
|
||||
{
|
||||
cts?.Cancel();
|
||||
|
||||
switch (change)
|
||||
{
|
||||
case PlayModeStateChange.ExitingEditMode:
|
||||
RememberWindows();
|
||||
break;
|
||||
case PlayModeStateChange.EnteredPlayMode:
|
||||
try
|
||||
{
|
||||
cts = new CancellationTokenSource();
|
||||
_ = HandleGameWindow(cts.Token);
|
||||
}
|
||||
catch(OperationCanceledException){}
|
||||
break;
|
||||
case PlayModeStateChange.EnteredEditMode:
|
||||
SessionState.EraseString(LAST_WINDOWS_SESS_KEY);
|
||||
if (!closeGameWindow && unfocusGameWindow && !SessionState.GetBool(GAME_WINDOW_OPEN_SESS_KEY, true))
|
||||
{
|
||||
var gameWindows = (EditorWindow[])Resources.FindObjectsOfTypeAll(gameWindowType);
|
||||
if (gameWindows.Any() && !gameWindows.Any(w => previouslyFocusedWindows.Contains(w)))
|
||||
foreach(var w in gameWindows)
|
||||
w.Close();
|
||||
}
|
||||
|
||||
previouslyFocusedWindows = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void RememberWindows()
|
||||
{
|
||||
if (unfocusGameWindow && hasFocusProperty != null)
|
||||
{
|
||||
var idList = string.Join(",", Resources.FindObjectsOfTypeAll<EditorWindow>()
|
||||
.Where(w => (bool) hasFocusProperty.GetValue(w))
|
||||
.Select(w => w.GetInstanceID().ToString()));
|
||||
|
||||
SessionState.SetString(LAST_WINDOWS_SESS_KEY, idList);
|
||||
}
|
||||
|
||||
if (gameWindowType == null) return;
|
||||
var gameWindows = Resources.FindObjectsOfTypeAll(gameWindowType);
|
||||
SessionState.SetBool(GAME_WINDOW_OPEN_SESS_KEY, gameWindows.Length > 0); }
|
||||
|
||||
private static async Task HandleGameWindow(CancellationToken ct)
|
||||
{
|
||||
if (!closeGameWindow && !unfocusGameWindow) return;
|
||||
var gameWindowWasOpen = SessionState.GetBool(GAME_WINDOW_OPEN_SESS_KEY, true);
|
||||
await Task.Delay(GAME_WINDOW_HANDLE_DELAY, ct);
|
||||
|
||||
if (closeGameWindow && !gameWindowWasOpen)
|
||||
{
|
||||
var windows = Resources.FindObjectsOfTypeAll(gameWindowType);
|
||||
foreach (var w in windows)
|
||||
{
|
||||
var ew = (EditorWindow)w;
|
||||
ew.Close();
|
||||
}
|
||||
} else if (unfocusGameWindow)
|
||||
{
|
||||
var previousWindowsFocusList = SessionState.GetString(LAST_WINDOWS_SESS_KEY, string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(previousWindowsFocusList)) return;
|
||||
|
||||
previouslyFocusedWindows = previousWindowsFocusList
|
||||
.Split(',').Select(int.Parse)
|
||||
.Select(EditorUtility.InstanceIDToObject)
|
||||
.OfType<EditorWindow>().Where(w => w != null).ToArray();
|
||||
foreach (var w in previouslyFocusedWindows)
|
||||
w.Focus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region Initialization
|
||||
[InitializeOnLoadMethod]
|
||||
private static void Initialize() => LoadVariables();
|
||||
private static void LoadVariables()
|
||||
{
|
||||
closeGameWindow = EditorPrefs.GetBool(CLOSE_GAME_WINDOW_PREF_KEY, false);
|
||||
unfocusGameWindow = EditorPrefs.GetBool(UNFOCUS_GAME_WINDOW_PREF_KEY, true);
|
||||
RefreshHook();
|
||||
}
|
||||
|
||||
private static void RefreshHook()
|
||||
{
|
||||
EditorApplication.playModeStateChanged -= HandlePlaymodeChange;
|
||||
if (closeGameWindow || unfocusGameWindow)
|
||||
EditorApplication.playModeStateChanged += HandlePlaymodeChange;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Menu Items
|
||||
[MenuItem("DreadTools/Utility/NoAutoGame/AutoUnfocus")]
|
||||
private static void ToggleUnfocusGameWindow()
|
||||
{
|
||||
bool newValue = !EditorPrefs.GetBool(UNFOCUS_GAME_WINDOW_PREF_KEY, true);
|
||||
EditorPrefs.SetBool(UNFOCUS_GAME_WINDOW_PREF_KEY, newValue);
|
||||
Debug.Log($"[NoAutoGame] AutoUnfocus is now {(newValue ? "enabled" : "disabled")}.");
|
||||
unfocusGameWindow = newValue;
|
||||
RefreshHook();
|
||||
}
|
||||
|
||||
[MenuItem("DreadTools/Utility/NoAutoGame/AutoClose")]
|
||||
private static void ToggleCloseGameWindow()
|
||||
{
|
||||
bool newValue = !EditorPrefs.GetBool(CLOSE_GAME_WINDOW_PREF_KEY, false);
|
||||
EditorPrefs.SetBool(CLOSE_GAME_WINDOW_PREF_KEY, newValue);
|
||||
Debug.Log($"[NoAutoGame] AutoClose is now {(newValue ? "enabled" : "disabled")}.");
|
||||
closeGameWindow = newValue;
|
||||
RefreshHook();
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
11
NoAutoGame/Editor/NoAutoGame.cs.meta
Normal file
11
NoAutoGame/Editor/NoAutoGame.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6c420c7dc39936499f795315373858c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
15
NoAutoGame/Editor/com.dreadscripts.noautogame.Editor.asmdef
Normal file
15
NoAutoGame/Editor/com.dreadscripts.noautogame.Editor.asmdef
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "com.dreadscripts.noautogame.Editor",
|
||||
"references": [],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0aba0aac9d6446439e0b33d153a8596
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
21
NoAutoGame/LICENSE
Normal file
21
NoAutoGame/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Dreadrith
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
7
NoAutoGame/LICENSE.meta
Normal file
7
NoAutoGame/LICENSE.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba9c196d2f7700141892c2fea0f6ca6a
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
12
NoAutoGame/README.md
Normal file
12
NoAutoGame/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# NoAutoGame
|
||||
Prevents the Game window from opening or automatically focusing.
|
||||
|
||||
### [Download From Here](https://vpm.dreadscripts.com/)
|
||||
|
||||
## How it works
|
||||
There are two settings you can toggle from `DreadTools > Utility > NoAutoGame`
|
||||
- **AutoClose:** If there was no Game window before entering playmode, then the Game window will be closed right away.
|
||||
- **AutoUnfocus:** This will restore all windows' focuses to what it was before entering playmode. Additionally, if AutoClose is disabled, and there was no Game window, then it'll close the Game window upon exiting playmode.
|
||||
|
||||
### Thank You
|
||||
If you enjoy NoAutoGame, please consider [supporting me ♡](https://ko-fi.com/Dreadrith)!
|
7
NoAutoGame/README.md.meta
Normal file
7
NoAutoGame/README.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac0f110be26650d4c8c71ecce405ca12
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
17
NoAutoGame/package.json
Normal file
17
NoAutoGame/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "com.dreadscripts.noautogame",
|
||||
"displayName": "DreadScripts - NoAutoGame",
|
||||
"version": "1.1.0",
|
||||
"description": "Prevents the Game window from opening or automatically focusing.",
|
||||
"gitDependencies": {},
|
||||
"vpmDependencies": {},
|
||||
"author": {
|
||||
"name": "Dreadrith",
|
||||
"email": "dreadscripts@gmail.com",
|
||||
"url": "https://www.dreadrith.com"
|
||||
},
|
||||
"legacyFolders": {},
|
||||
"legacyFiles": {},
|
||||
"type": "tool",
|
||||
"unity": "2019.4"
|
||||
}
|
7
NoAutoGame/package.json.meta
Normal file
7
NoAutoGame/package.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53fccee2c1c00224bb4ebda93d268b44
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user