Version: 2020.2

PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown

切换到手册
public static void ConnectionTargetSelectionDropdown (Networking.PlayerConnection.IConnectionState state, GUIStyle style);

参数

state The state of the connection that the EditorWindow displaying the drop-down uses. Use PlayerConnectionGUIUtility.GetConnectionState to get a state in OnEnable. Make sure you dispose of that state in OnDisable to avoid leaking it.
style 定义绘制下拉选单按钮时应采用的 GUIStyle。如果未指定,则将绘制默认下拉选单按钮。

描述

显示下拉选单按钮和菜单,供用户选择并建立与播放器的连接。

This is the same control that is used in the toolbars of the Profiler Window, Frame Debugger or Console Window. The drop-down will list all available Players and Editors that your Editor can connect to and that are discoverable. It also offers an entry to directly connect to an IP address. You need to provide the state of the connection for your EditorWindow. To get the state, use PlayerConnectionGUIUtility.GetConnectionState in OnEnable. Make sure you dispose of the state in OnDisable of the EditorWindow you use it in to avoid leaking it.

This class currently only works for the connection that the Profiling tools and the Console use. In a future release this will work with 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(); } }

Also see PlayerConnectionGUI.ConnectionTargetSelectionDropdown for manual positioning as well as PlayerConnectionGUIUtility.GetConnectionState and IConnectionState for details of the state handling for this UI control.


Did you find this page useful? Please give it a rating: