PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown
Declaration
public static void ConnectionTargetSelectionDropdown(Networking.PlayerConnection.IConnectionState state, GUIStyle style, int maxWidth);Параметры
| Параметр | Описание |
|---|---|
| state | состояние подключения, которое использует EditorWindow, отображающее раскрывающееся меню. Используйте PlayerConnectionGUIUtility.GetConnectionState, чтобы получить состояние в OnEnable. Обязательно удалите состояние в OnDisable, чтобы избежать утечки. |
| стиль | Определяет GUIStyle кнопку выпадающего списка, которая должна быть нарисована. Кнопка выпадающего списка по умолчанию будет нарисована, если не указано. |
| maxWidth | Максимальная ширина выпадающего списка в пикселях (необязательно). |
Описание
Показать раскрывающуюся кнопку и меню для пользователя, чтобы выбрать и установить соединение с проигрывателем.
Это тот же самый элемент управления, который используется в панелях инструментов Profiler Окно, Frame Debugger или Окно консоли. В выпадающем списке перечислены все доступные плееры и редакторы, к которым может подключиться ваш редактор и которые удаётся обнаружить. Там же есть поле для прямого подключения по IP-адресу. Вам нужно указать состояние подключения для вашего EditorWindow. Чтобы получить состояние, используйте PlayerConnectionGUIUtility.GetConnectionState в OnEnable. Обязательно уберите это состояние в OnDisable из EditorWindow вы используете его внутри для того чтобы избежать протекать его.
Сейчас этот класс работает только с подключением, которое используют инструменты профилирования и Console. В одном из будущих выпусков он будет работать и с 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 подробнее об обработке состояния для этого элемента управления.