state | 展示下拉选项的 EditorWindow 的连接状态。在 OnEnable 中使用 PlayerConnectionGUIUtility.GetConnectionState 来获取状态。确保在 OnDisable 中释放该状态,以免发生泄漏。 |
style | 定义要用来绘制下拉按钮的 GUIStyle。如果未指定,则会绘制一个默认下拉按钮。 |
maxWidth | 下拉选项的最大宽度(像素)(可选)。 |
展示一个下拉按钮和菜单,供用户选择并建立与播放器的连接。
这是与 Profiler Window、Frame Debugger 或 Console Window 工具栏中使用的控件相同的控件。下拉选项会列出所有可用的播放器和编辑器,供你的编辑器连接且可发现。它还提供了直接连接到 IP 地址的条目。你需要为你 EditorWindow 提供连接状态。若要获取状态,请在 OnEnable 中使用 PlayerConnectionGUIUtility.GetConnectionState。确保在你使用的 EditorWindow 的 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 of. // 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); PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown(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 控件的状态处理的细节。