PlayerConnectionGUI.ConnectionTargetSelectionDropdown
Declaration
public static void ConnectionTargetSelectionDropdown(Rect rect, Networking.PlayerConnection.IConnectionState state, GUIStyle style);Параметры
| Параметр | Описание |
|---|---|
| прямой | Где нарисовать кнопку раскрывающегося списка. |
| state | состояние подключения, которое используется в EditorWindow, отображающих это раскрывающееся меню. Используйте PlayerConnectionGUIUtility.GetConnectionState, чтобы получить состояние в OnEnable, и не забудьте удалить это состояние в OnDisable. |
| стиль | Определяет GUIStyle, в котором должен быть нарисован выпадающий список. Если не указан стиль, то будет нарисован выпадающий список по умолчанию. |
Описание
Показать раскрывающуюся кнопку и меню для пользователя, чтобы выбрать и установить соединение с проигрывателем.
Это тот же самый элемент управления, который используется в панелях инструментов 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 // 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 подробнее об обработке состояния для этого элемента управления.