parentWindow | 将使用连接的 EditorWindow。 |
connectedCallback | 每当用户发起的连接尝试成功时触发的回调。 |
IConnectionState 返回与 Player 的连接的未序列化状态,在 PlayerConnectionGUI.ConnectionTargetSelectionDropdown 或 PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown 中使用。返回的连接状态对象包含有关 Player 连接到哪个目标以及哪些目标可用的信息。
此方法生成一个状态跟踪对象,用于建立和显示编辑器到 Player 的连接。
此状态和相应的 GUI 方法(PlayerConnectionGUI.ConnectionTargetSelectionDropdown 和 PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown)应该在 EditorWindow 中使用。提供的 Editor Window 用于:1. 在连接时显示覆盖层 2. 在建立连接后重新绘制窗口 3. 在使用相同连接类型的其他窗口建立连接时,通过 connectedCallback 接收回调调用。从技术上讲,可能可以将 parentWindow 作为 null 并将其用于 EditorWindow 之外,但我们既不推荐也不打算支持这种用例。
还要记住,接收到的状态未序列化,需要进行处置。推荐的使用模式是在 EditorWindow 的 OnEnable 中获取状态,并在 OnDisable 中对其进行处置。
例如,如果连接是通过自动建立与 Player 的探查器连接或回退到与编辑器的连接而建立的,则不会触发 connectedCallback。相反,它仅响应用户主动选择目标,并等待该连接建立。如果连接类型是在编辑器窗口之间共享的,则当用户在不同的 EditorWindow 中选择它时,也会触发它。
返回的状态知道当前连接了哪个目标以及哪些目标可用。
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); 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 the state! attachProfilerState.Dispose(); } }