播放时如何在编辑器中打开/关闭统计面板? [英] How to switch on/off statistics panel in editor while playing?
本文介绍了播放时如何在编辑器中打开/关闭统计面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在播放时通过C#脚本打开/关闭此面板. 这可能吗?尚未为此找到任何Editor API函数.
I want to switch this panel on/off by C# script while playing. Is this possible? Haven't found any Editor API functions for this.
推荐答案
您可以进行反射.我很久以前提出的修改后的类似答案.以下是一个有效的设置/获取统计信息功能.已通过 Unity 5.4.0f1 测试.我放置了Unity版本,以便人们在停止工作时不会抱怨.如果将变量的 any 重命名,则Unity的更新可以随时中断此操作.
You can do it with reflection. Modified similar answer I made long ago. Below is a working set/get stats function. Tested with Unity 5.4.0f1. I put the Unity version so that people won't complain when it stops working. Unity's update can break this anytime if they rename any of the variables.
-
GameView
=一个类,用于表示以下版本中的Unity GameView选项卡 编辑器. -
GetMainGameView
=返回当前GameView
的静态函数 实例. -
m_Stats
=一个布尔变量,用于确定统计信息是否 应该显示还是不显示.
GameView
= A class that is used to represent Unity GameView tab in the Editor.GetMainGameView
= static function that returns currentGameView
instance.m_Stats
= a boolean variable that is used to determine if stats should be displayed or not.
代码:
//Show/Hide stats
void showStats(bool enableStats)
{
Assembly asm = Assembly.GetAssembly(typeof(Editor));
Type type = asm.GetType("UnityEditor.GameView");
if (type != null)
{
MethodInfo gameViewFunction = type.GetMethod("GetMainGameView", BindingFlags.Static |
BindingFlags.NonPublic);
object gameViewInstance = gameViewFunction.Invoke(null, null);
FieldInfo getFieldInfo = type.GetField("m_Stats", BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
getFieldInfo.SetValue(gameViewInstance, enableStats);
}
}
//Returns true if stats is enabled
bool statsIsEnabled()
{
Assembly asm = Assembly.GetAssembly(typeof(Editor));
Type type = asm.GetType("UnityEditor.GameView");
if (type != null)
{
MethodInfo gameViewFunction = type.GetMethod("GetMainGameView", BindingFlags.Static |
BindingFlags.NonPublic);
object gameViewInstance = gameViewFunction.Invoke(null, null);
FieldInfo getFieldInfo = type.GetField("m_Stats", BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
return (bool)getFieldInfo.GetValue(gameViewInstance);
}
return false;
}
用法:
//Show stats
showStats(true);
//Hide stats
showStats(false);
//Read stats
bool stats = statsIsEnabled();
这篇关于播放时如何在编辑器中打开/关闭统计面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文