Unityエディタを使用中のみ電源プランを高性能に
UnityEditorPerformanceBoostは、
Unityエディタを使用中のみ電源プランを高性能にするシンプルなエディタ拡張です。
劇的な効果は期待できないものの、
ノーリスクでUnityエディタのパフォーマンスを底上げすることができます。
インストール
INSTALL
1、下記のソースコードをコピーして、UnityEditorPerformanceBoost.cs というファイル名で、Editorフォルダに配置する。
2、ソースコードを配置しているだけで自動的に動作します。
2、ソースコードを配置しているだけで自動的に動作します。
使い方
HOW TO USE
ソースコード
SOURCE CODE
UnityEditorPerformanceBoost ソースコード
#if UNITY_EDITOR_WIN && UNITY_EDITOR
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using UnityEditor;
[InitializeOnLoad]
public static class UnityEditorPerformanceBoost
{
/// <summary>
/// 現在ハイパフォーマンスモードか?
/// </summary>
private static bool isHighPerformance = false;
/// <summary>
/// 省電力電源プラン
/// </summary>
private static readonly string powerSchemeSaving = "a1841308-3541-4fab-bc81-f71556f20b4a";
/// <summary>
/// バランス電源プラン
/// </summary>
private static readonly string powerSchemeBalance = "381b4222-f694-41f0-9685-ff5bb260df2e";
/// <summary>
/// 高パフォーマンス電源プラン
/// </summary>
private static readonly string PowerSchemeHighPerformance = "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c";
/// <summary>
/// タイマー
/// </summary>
private static double lastTime;
/// <summary>
/// 変更前の電源プラン
/// </summary>
private static string originalPowerScheme;
/// <summary>
/// GUID正規表現
/// </summary>
private static readonly Regex GuidRegex = new(@"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b", RegexOptions.Compiled);
static UnityEditorPerformanceBoost()
{
EditorApplication.update += OnUpdate;
}
/// <summary>
/// 定期タイマー
/// </summary>
private static void OnUpdate()
{
if (EditorApplication.timeSinceStartup - lastTime > 2)
{
lastTime = EditorApplication.timeSinceStartup;
CheckFocused();
}
}
/// <summary>
/// フォーカスチェック
/// </summary>
private static void CheckFocused()
{
if (IsWindowFocused())
{
EnableHighPerformance();
}
else
{
RestoreOriginalPowerScheme();
}
}
/// <summary>
/// ハイパフォーマンスモードを有効にする
/// </summary>
private static void EnableHighPerformance()
{
try
{
if (isHighPerformance) return;
isHighPerformance = true;
// 現在の電源プランを保存
var str = CommandPowerCfg($"/getactivescheme");
var match = GuidRegex.Match(str);
originalPowerScheme = match.Value;
// 自身のプロセス優先度を上げる
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
// Windowsの電源プランを高パフォーマンスにする
CommandPowerCfg($"/setactive {PowerSchemeHighPerformance}");
UnityEngine.Debug.Log("高パフォーマンス電源プラン有効:" + PowerSchemeHighPerformance);
}
catch (Exception e)
{
UnityEngine.Debug.LogError(e);
}
}
/// <summary>
/// 元の電源プランに戻す
/// </summary>
private static void RestoreOriginalPowerScheme()
{
try
{
if (!isHighPerformance) return;
isHighPerformance = false;
// プロセス優先度を標準にする
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal;
// Windowsの電源プランを戻す
CommandPowerCfg($"/setactive {originalPowerScheme}");
UnityEngine.Debug.Log("元プランに戻した:" + originalPowerScheme);
}
catch (Exception e)
{
UnityEngine.Debug.LogError(e);
}
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
/// <summary>
/// ウィンドウが前面に出ているか?
/// </summary>
/// <returns></returns>
private static bool IsWindowFocused()
{
IntPtr hwnd = GetForegroundWindow();
if (hwnd == IntPtr.Zero) return false;
GetWindowThreadProcessId(hwnd, out uint processId);
return (processId == (uint)Process.GetCurrentProcess().Id);
}
/// <summary>
/// 電源プランコマンド
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
private static string CommandPowerCfg(string command)
{
var processStartInfo = new ProcessStartInfo("powercfg", command)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(processStartInfo))
{
return process.StandardOutput.ReadToEnd();
}
}
}
#endif
ライセンス
LICENSE
MIT License
Copyright 2025 MMGames
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.
Copyright 2025 MMGames
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.




