|
4 | 4 | #endif
|
5 | 5 | using Unity.Netcode.Transports.UTP;
|
6 | 6 | using UnityEditor;
|
| 7 | +using UnityEngine; |
7 | 8 |
|
8 | 9 | namespace Unity.Netcode.Editor
|
9 | 10 | {
|
@@ -43,7 +44,109 @@ public class UNetTransportEditor : HiddenScriptEditor
|
43 | 44 | [CustomEditor(typeof(UnityTransport), true)]
|
44 | 45 | public class UnityTransportEditor : HiddenScriptEditor
|
45 | 46 | {
|
| 47 | + private static readonly string[] k_HiddenFields = { "m_Script", "ConnectionData" }; |
46 | 48 |
|
| 49 | + private bool m_AllowIncomingConnections; |
| 50 | + private bool m_Initialized; |
| 51 | + |
| 52 | + private UnityTransport m_UnityTransport; |
| 53 | + |
| 54 | + private SerializedProperty m_ServerAddressProperty; |
| 55 | + private SerializedProperty m_ServerPortProperty; |
| 56 | + private SerializedProperty m_OverrideBindIpProperty; |
| 57 | + |
| 58 | + private const string k_LoopbackIpv4 = "127.0.0.1"; |
| 59 | + private const string k_LoopbackIpv6 = "::1"; |
| 60 | + private const string k_AnyIpv4 = "0.0.0.0"; |
| 61 | + private const string k_AnyIpv6 = "::"; |
| 62 | + |
| 63 | + |
| 64 | + private void Initialize() |
| 65 | + { |
| 66 | + if (m_Initialized) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + m_Initialized = true; |
| 71 | + m_UnityTransport = (UnityTransport)target; |
| 72 | + |
| 73 | + var connectionDataProperty = serializedObject.FindProperty(nameof(UnityTransport.ConnectionData)); |
| 74 | + |
| 75 | + m_ServerAddressProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Address)); |
| 76 | + m_ServerPortProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Port)); |
| 77 | + m_OverrideBindIpProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.ServerListenAddress)); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Draws inspector properties without the script field. |
| 82 | + /// </summary> |
| 83 | + public override void OnInspectorGUI() |
| 84 | + { |
| 85 | + Initialize(); |
| 86 | + EditorGUI.BeginChangeCheck(); |
| 87 | + serializedObject.UpdateIfRequiredOrScript(); |
| 88 | + DrawPropertiesExcluding(serializedObject, k_HiddenFields); |
| 89 | + serializedObject.ApplyModifiedProperties(); |
| 90 | + EditorGUI.EndChangeCheck(); |
| 91 | + |
| 92 | + EditorGUILayout.PropertyField(m_ServerAddressProperty); |
| 93 | + EditorGUILayout.PropertyField(m_ServerPortProperty); |
| 94 | + |
| 95 | + serializedObject.ApplyModifiedProperties(); |
| 96 | + |
| 97 | + EditorGUILayout.HelpBox("It's recommended to leave remote connections disabled for local testing to avoid exposing ports on your device.", MessageType.Info); |
| 98 | + bool allowRemoteConnections = m_UnityTransport.ConnectionData.ServerListenAddress != k_LoopbackIpv4 && m_UnityTransport.ConnectionData.ServerListenAddress != k_LoopbackIpv6 && !string.IsNullOrEmpty(m_UnityTransport.ConnectionData.ServerListenAddress); |
| 99 | + allowRemoteConnections = EditorGUILayout.Toggle(new GUIContent("Allow Remote Connections?", $"Bind IP: {m_UnityTransport.ConnectionData.ServerListenAddress}"), allowRemoteConnections); |
| 100 | + |
| 101 | + bool isIpV6 = m_UnityTransport.ConnectionData.IsIpv6; |
| 102 | + |
| 103 | + if (!allowRemoteConnections) |
| 104 | + { |
| 105 | + if (m_UnityTransport.ConnectionData.ServerListenAddress != k_LoopbackIpv4 && m_UnityTransport.ConnectionData.ServerListenAddress != k_LoopbackIpv6) |
| 106 | + { |
| 107 | + if (isIpV6) |
| 108 | + { |
| 109 | + m_UnityTransport.ConnectionData.ServerListenAddress = k_LoopbackIpv6; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + m_UnityTransport.ConnectionData.ServerListenAddress = k_LoopbackIpv4; |
| 114 | + } |
| 115 | + EditorUtility.SetDirty(m_UnityTransport); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + using (new EditorGUI.DisabledScope(!allowRemoteConnections)) |
| 120 | + { |
| 121 | + string overrideIp = m_UnityTransport.ConnectionData.ServerListenAddress; |
| 122 | + if (overrideIp == k_AnyIpv4 || overrideIp == k_AnyIpv6 || overrideIp == k_LoopbackIpv4 || overrideIp == k_LoopbackIpv6) |
| 123 | + { |
| 124 | + overrideIp = ""; |
| 125 | + } |
| 126 | + |
| 127 | + overrideIp = EditorGUILayout.TextField("Override Bind IP (optional)", overrideIp); |
| 128 | + if (allowRemoteConnections) |
| 129 | + { |
| 130 | + if (overrideIp == "") |
| 131 | + { |
| 132 | + if (isIpV6) |
| 133 | + { |
| 134 | + overrideIp = k_AnyIpv6; |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + overrideIp = k_AnyIpv4; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (m_UnityTransport.ConnectionData.ServerListenAddress != overrideIp) |
| 143 | + { |
| 144 | + m_UnityTransport.ConnectionData.ServerListenAddress = overrideIp; |
| 145 | + EditorUtility.SetDirty(m_UnityTransport); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
47 | 150 | }
|
48 | 151 |
|
49 | 152 | #if COM_UNITY_MODULES_ANIMATION
|
|
0 commit comments