rect | 绘制下拉按钮的位置。 |
state | 用于此下拉菜单显示的连接状态。使用 PlayerConnectionGUIUtility.GetConnectionState 在 OnEnable 中获取状态,并在 OnDisable 中记得释放该状态。 |
style | 定义下拉按钮应绘制的 GUIStyle。如果未指定样式,则将绘制默认的下拉按钮。 |
显示一个下拉按钮和菜单,供用户选择并建立与玩家的连接。
此控件与 性能分析器窗口、帧调试器 或 控制台窗口 的工具栏中使用的控件相同。下拉菜单将列出您的编辑器可以连接到且可发现的所有可用玩家和编辑器。它还提供了一个直接连接到 IP 地址的条目。您需要提供用于 编辑器窗口 的连接状态。要获取一个,请在 OnEnable 中使用 PlayerConnectionGUIUtility.GetConnectionState,并在您使用的 编辑器窗口 的 OnDisable 中记得释放状态。
此类目前仅适用于性能分析工具和控制台使用的连接。在将来的版本中,这将与 PlayerConnection 一起使用。
using UnityEngine; using UnityEngine.Profiling; using UnityEditor; using UnityEngine.Networking.PlayerConnection; using UnityEditor.Networking.PlayerConnection;
public class MyWindow : EditorWindow { // The state can survive for the life time of the EditorWindow so it's best to store it here and just renew/dispose of it in OnEnable and OnDisable, rather than fetching repeatedly it in OnGUI. IConnectionState attachProfilerState;
[MenuItem("Window/My Window")] static void Init() { MyWindow window = (MyWindow)GetWindow(typeof(MyWindow)); window.Show(); }
private void OnEnable() { // The state of the connection is not getting serialized and needs to be disposed // Therefore, it's recommended to fetch it in OnEnable and call Dispose() on it in OnDisable attachProfilerState = PlayerConnectionGUIUtility.GetConnectionState(this, OnConnected); }
private void OnConnected(string player) { Debug.Log(string.Format("MyWindow connected to {0}", player)); }
private void OnGUI() { // Draw a toolbar across the top of the window and draw the drop-down in the toolbar drop-down style too EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); var rect = GUILayoutUtility.GetRect(100, EditorGUIUtility.singleLineHeight, EditorStyles.toolbarDropDown); PlayerConnectionGUI.ConnectionTargetSelectionDropdown(rect, attachProfilerState, EditorStyles.toolbarDropDown);
switch (attachProfilerState.connectedToTarget) { case ConnectionTarget.None: //This case can never happen within the Editor, since the Editor will always fall back onto a connection to itself. break; case ConnectionTarget.Player: Profiler.enabled = GUILayout.Toggle(Profiler.enabled, string.Format("Profile the attached Player ({0})", attachProfilerState.connectionName), EditorStyles.toolbarButton); break; case ConnectionTarget.Editor: // The name of the Editor or the PlayMode Player would be "Editor" so adding the connectionName here would not add anything. Profiler.enabled = GUILayout.Toggle(Profiler.enabled, "Profile the Player in the Editor", EditorStyles.toolbarButton); break; default: break; } EditorGUILayout.EndHorizontal(); }
private void OnDisable() { // Remember to always dispose of the state! attachProfilerState.Dispose(); } }
另请参阅 PlayerConnectionGUI.ConnectionTargetSelectionDropdown 以了解自动布局,以及 PlayerConnectionGUIUtility.GetConnectionState 和 IConnectionState 以了解此 UI 控件的状态处理详细信息。