Skip to content

Commit

Permalink
感谢 鲲哥,可以自动设置编辑器下的屏幕分辨率
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPsyCongree committed Nov 30, 2018
1 parent 60b4114 commit a55db93
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LuaProfiler/Common/Editor/TreeView/LuaProfilerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void DoToolbar()
LuaDeepProfilerSetting.Instance.isRecord = flag;
if (flag)
{
EditorUtility.DisplayDialog("", "suggest you to set resolution to 480*270", "OK");
GameViewUtility.ChangeGameViewSize(480, 270);
}
EditorApplication.isPlaying = false;
}
Expand Down
113 changes: 113 additions & 0 deletions LuaProfiler/Common/GameViewUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#if UNITY_EDITOR
namespace MikuLuaProfiler
{

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class GameViewUtility
{
public static void ChangeGameViewSize(int width, int height)
{
m_width = width;
m_height = height;
callback = new EditorApplication.CallbackFunction(Update);
EditorApplication.update += callback;
}

#region member
private enum GameViewSizeType
{
AspectRatio,
FixedResolution
}
private static readonly string s_MiKuProfiler = "LuaProfiler";
private static object s_GameViewSizesInstance;
private static MethodInfo s_GetGroup;
private static EditorApplication.CallbackFunction callback;
private static int m_width;
private static int m_height;
#endregion

#region private
private static void Update()
{
var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
s_GetGroup = sizesType.GetMethod("GetGroup");
s_GameViewSizesInstance = instanceProp.GetValue(null, null);

if (!SizeExists(GetCurrentGroupType(), s_MiKuProfiler))
{
AddCustomSize(GameViewSizeType.FixedResolution, GetCurrentGroupType(), m_width, m_height, s_MiKuProfiler);
}
int type = FindSize(GetCurrentGroupType(), s_MiKuProfiler);
SetSize(type);
EditorApplication.update -= callback;
}

private static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text)
{
var group = GetGroup(sizeGroupType);
var addCustomSize = s_GetGroup.ReturnType.GetMethod("AddCustomSize");
var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
var ctor = gvsType.GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(string) });
var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
addCustomSize.Invoke(group, new object[] { newSize });
}

private static void SetSize(int index)
{
var gameViewType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
var mainGameView = gameViewType.GetMethod("GetMainGameView", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);
if (mainGameView == null || mainGameView.ToString().ToLowerInvariant().Equals("null"))
{
return;
}
gameViewType.GetMethod("Focus", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Invoke(mainGameView, null);
var selectedSizeIndexProp = gameViewType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
selectedSizeIndexProp.SetValue(mainGameView, index, null);
var sizeSelectionCallback = gameViewType.GetMethod("SizeSelectionCallback", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
sizeSelectionCallback.Invoke(mainGameView, new object[] { index, null });
}

public static bool SizeExists(GameViewSizeGroupType sizeGroupType, string text)
{
return FindSize(sizeGroupType, text) != -1;
}

private static int FindSize(GameViewSizeGroupType sizeGroupType, string text)
{
var group = GetGroup(sizeGroupType);
var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
for (int i = 0; i < displayTexts.Length; i++)
{
string display = displayTexts[i];
int pren = display.IndexOf('(');
if (pren != -1)
display = display.Substring(0, pren - 1);
if (display == text)
return i;
}
return -1;
}

static object GetGroup(GameViewSizeGroupType type)
{
return s_GetGroup.Invoke(s_GameViewSizesInstance, new object[] { (int)type });
}

private static GameViewSizeGroupType GetCurrentGroupType()
{
var getCurrentGroupTypeProp = s_GameViewSizesInstance.GetType().GetProperty("currentGroupType");
return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(s_GameViewSizesInstance, null);
}
#endregion

}
}
#endif
12 changes: 12 additions & 0 deletions LuaProfiler/Common/GameViewUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions LuaProfiler/Common/LuaDeepProfilerSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public bool isDeepProfiler
set
{
m_isDeepProfiler = value;
if (value && LuaDeepProfilerSetting.Instance.isRecord)
{
GameViewUtility.ChangeGameViewSize(480, 270);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions LuaProfiler/HookSetup/LuaHookSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public static IntPtr luaL_newstate()
MikuLuaProfilerLuaProfilerWrap.__Register(l);
Install();
LuaProfiler.mainL = l;
if (LuaDeepProfilerSetting.Instance.isRecord)
{
GameViewUtility.ChangeGameViewSize(480, 270);
}
}
return l;
}
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
![](doc/profiler.png)
<br/>

---
__特别提醒,因为截屏的效率不是很高,如果游戏本身效率不是很好,很容易触发截屏的话,建议把编辑器的分辨率设置为==480*270==,这样不会因为分辨率过大而导致截屏保存的IO卡顿__
---
#### Record模式
&nbsp;&nbsp;&nbsp;&nbsp;也是默认模式,如果关闭点击Profiler窗口下的Record按钮即可开启。在这个模式下将保存每次Profiler采样的历史记录,并且会将出现帧率下降、或者超大GC申请的Sample进行截屏保存,需要截屏的GC的阈值你可以在 __capture gc__ 后面的输入框中进行配置<br/>
&nbsp;&nbsp;&nbsp;&nbsp;也是默认模式,如果关闭点击Profiler窗口下的Record按钮即可开启。在这个模式下将保存每次Profiler采样的历史记录,并且会将出现帧率下降、或者超大GC申请的Sample进行截屏保存,需要截屏的GC的阈值你可以在 __capture gc__ 后面的输入框中进行配置,并且为了避免大分辨率下截屏,会自动把屏幕分辨率设置为480*270<br/>
&nbsp;&nbsp;&nbsp;&nbsp;当你在编辑器下暂停或者关闭游戏的时候你可以拖动 __start__ 以及**end**的进度条进行历史记录查阅,sample列表中将显示**start****end** 的累计采样值。要单独观察某一个sample的消耗可以把**start**__end__ 设置为相同值。

##### 按钮功能介绍
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void DoToolbar()
LuaDeepProfilerSetting.Instance.isRecord = flag;
if (flag)
{
EditorUtility.DisplayDialog("", "suggest you to set resolution to 480*270", "OK");
GameViewUtility.ChangeGameViewSize(480, 270);
}
EditorApplication.isPlaying = false;
}
Expand Down
113 changes: 113 additions & 0 deletions SLua/Assets/LuaProfiler/Common/GameViewUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#if UNITY_EDITOR
namespace MikuLuaProfiler
{

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class GameViewUtility
{
public static void ChangeGameViewSize(int width, int height)
{
m_width = width;
m_height = height;
callback = new EditorApplication.CallbackFunction(Update);
EditorApplication.update += callback;
}

#region member
private enum GameViewSizeType
{
AspectRatio,
FixedResolution
}
private static readonly string s_MiKuProfiler = "LuaProfiler";
private static object s_GameViewSizesInstance;
private static MethodInfo s_GetGroup;
private static EditorApplication.CallbackFunction callback;
private static int m_width;
private static int m_height;
#endregion

#region private
private static void Update()
{
var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
s_GetGroup = sizesType.GetMethod("GetGroup");
s_GameViewSizesInstance = instanceProp.GetValue(null, null);

if (!SizeExists(GetCurrentGroupType(), s_MiKuProfiler))
{
AddCustomSize(GameViewSizeType.FixedResolution, GetCurrentGroupType(), m_width, m_height, s_MiKuProfiler);
}
int type = FindSize(GetCurrentGroupType(), s_MiKuProfiler);
SetSize(type);
EditorApplication.update -= callback;
}

private static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text)
{
var group = GetGroup(sizeGroupType);
var addCustomSize = s_GetGroup.ReturnType.GetMethod("AddCustomSize");
var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
var ctor = gvsType.GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(string) });
var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
addCustomSize.Invoke(group, new object[] { newSize });
}

private static void SetSize(int index)
{
var gameViewType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
var mainGameView = gameViewType.GetMethod("GetMainGameView", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);
if (mainGameView == null || mainGameView.ToString().ToLowerInvariant().Equals("null"))
{
return;
}
gameViewType.GetMethod("Focus", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Invoke(mainGameView, null);
var selectedSizeIndexProp = gameViewType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
selectedSizeIndexProp.SetValue(mainGameView, index, null);
var sizeSelectionCallback = gameViewType.GetMethod("SizeSelectionCallback", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
sizeSelectionCallback.Invoke(mainGameView, new object[] { index, null });
}

public static bool SizeExists(GameViewSizeGroupType sizeGroupType, string text)
{
return FindSize(sizeGroupType, text) != -1;
}

private static int FindSize(GameViewSizeGroupType sizeGroupType, string text)
{
var group = GetGroup(sizeGroupType);
var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
for (int i = 0; i < displayTexts.Length; i++)
{
string display = displayTexts[i];
int pren = display.IndexOf('(');
if (pren != -1)
display = display.Substring(0, pren - 1);
if (display == text)
return i;
}
return -1;
}

static object GetGroup(GameViewSizeGroupType type)
{
return s_GetGroup.Invoke(s_GameViewSizesInstance, new object[] { (int)type });
}

private static GameViewSizeGroupType GetCurrentGroupType()
{
var getCurrentGroupTypeProp = s_GameViewSizesInstance.GetType().GetProperty("currentGroupType");
return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(s_GameViewSizesInstance, null);
}
#endregion

}
}
#endif
12 changes: 12 additions & 0 deletions SLua/Assets/LuaProfiler/Common/GameViewUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions SLua/Assets/LuaProfiler/Common/LuaDeepProfilerSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public bool isDeepProfiler
set
{
m_isDeepProfiler = value;
if (value && LuaDeepProfilerSetting.Instance.isRecord)
{
GameViewUtility.ChangeGameViewSize(480, 270);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions SLua/Assets/LuaProfiler/HookSetup/LuaHookSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public static IntPtr luaL_newstate()
MikuLuaProfilerLuaProfilerWrap.__Register(l);
Install();
LuaProfiler.mainL = l;
if (LuaDeepProfilerSetting.Instance.isRecord)
{
GameViewUtility.ChangeGameViewSize(480, 270);
}
}
return l;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void DoToolbar()
LuaDeepProfilerSetting.Instance.isRecord = flag;
if (flag)
{
EditorUtility.DisplayDialog("", "suggest you to set resolution to 480*270", "OK");
GameViewUtility.ChangeGameViewSize(480, 270);
}
EditorApplication.isPlaying = false;
}
Expand Down
Loading

0 comments on commit a55db93

Please sign in to comment.