state | 显示下拉选单的 EditorWindow 中使用的连接状态。使用 EditorGUIUtility.GetAttachToPlayerState 获取 OnEnable 中的状态,并记住在 OnDisable 中处置该状态。 |
style | 定义绘制下拉选单按钮时应采用的 GUIStyle。如果未指定,则将绘制默认下拉选单按钮。 |
显示下拉选单按钮和菜单,供用户选择并建立与播放器的连接。
此控件与 Profiler 窗口、Frame Debugger 或 Console 窗口的工具栏中使用的控件相同。下拉选单将列出您的编辑器可以连接并且可被发现的所有可用播放器和编辑器。还提供直接连接到 IP 地址的条目。
您需要提供 EditorWindow 使用的连接状态。要获取此状态,请使用 OnEnable 中的 EditorGUIUtility.GetAttachToPlayerState 并记住在使用该状态的 EditorWindow 的 OnDisable 中处置该状态。
此类是实验性的,因为它目前仅适用于性能分析工具和控制台所使用的连接。在未来版本中,此类将使用 PlayerConnection。
using UnityEngine; using UnityEngine.Profiling; using UnityEditor; using UnityEngine.Experimental.Networking.PlayerConnection; using PlayerConnectionGUILayout = UnityEditor.Experimental.Networking.PlayerConnection.EditorGUILayout; using PlayerConnectionGUIUtility = UnityEditor.Experimental.Networking.PlayerConnection.EditorGUIUtility;
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.GetAttachToPlayerState(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.AttachToPlayerDropdown(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(); } }
另请参阅 EditorGUI.AttachToPlayerDropdown(了解有关手动定位的信息)以及 EditorGUIUtility.GetAttachToPlayerState 和 IConnectionState(了解有关此 UI 控件的状态处理的详细信息)。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.