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

参数

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

返回

IConnectionState Returns the unserialized state of the connection to a Player, which is used in PlayerConnectionGUI.ConnectionTargetSelectionDropdown or PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown. The returned connection state object contains information on what target is connected to the Player, and what targets are available.

描述

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

This state and the corresponding GUI Methods (PlayerConnectionGUI.ConnectionTargetSelectionDropdown and PlayerConnectionGUILayout.ConnectionTargetSelectionDropdown) are supposed to be used from within an EditorWindow. The supplied Editor Window is used to: 1.连接时显示覆盖面板 2.建立连接后重新绘制窗口 3.当使用相同连接类型的其他窗口建立连接时,通过 connectedCallback 接收回调调用 从技术上讲,有可能将 parentWindow 作为 null 提供并在 EditorWindow 之外进行使用,但不推荐这样做,而且我们也不计划支持这种用法。

另外切记,收到的状态不是序列化的,需要处理。推荐的使用模式是获取 EditorWindow 的 OnEnable 中的状态,并在 OnDisable 中对其进行处理。

如果通过自动建立性能分析器与播放器的连接或者回退到与编辑器的连接来建立连接,则不会触发 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(); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961