版本: Unity 6 (6000.0)
语言英语
  • C#

PlayerConnectionGUIUtility.GetConnectionState

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实阅读了用户提出的每个建议更改,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static Networking.PlayerConnection.IConnectionState GetConnectionState(EditorWindow parentWindow, Action<string> connectedCallback);

参数

parentWindow 将使用连接的 EditorWindow
connectedCallback 每当用户发起的连接尝试成功时触发的回调。

返回值

IConnectionState 返回与 Player 的连接的未序列化状态,在 PlayerConnectionGUI.ConnectionTargetSelectionDropdownPlayerConnectionGUILayout.ConnectionTargetSelectionDropdown 中使用。返回的连接状态对象包含有关 Player 连接到哪个目标以及哪些目标可用的信息。

描述

此方法生成一个状态跟踪对象,用于建立和显示编辑器到 Player 的连接。

此状态和相应的 GUI 方法(PlayerConnectionGUI.ConnectionTargetSelectionDropdownPlayerConnectionGUILayout.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(); } }