Skip to content

Commit 19e909f

Browse files
authored
misc: revert default bind ip to use loopback and update UI to be more user-friendly (#2408)
1 parent 4a1c2ff commit 19e909f

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Changed
1414

15+
- The UTP component UI has been updated to be more user-friendly for new users by adding a simple toggle to switch between local-only (127.0.0.1) and remote (0.0.0.0) binding modes, using the toggle "Allow Remote Connections" (#2408)
1516
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.1.
1617
- `NetworkShow()` of `NetworkObject`s are delayed until the end of the frame to ensure consistency of delta-driven variables like `NetworkList`.
1718
- Dirty `NetworkObject` are reset at end-of-frame and not at serialization time.
1819
- `NetworkHide()` of an object that was just `NetworkShow()`n produces a warning, as remote clients will _not_ get a spawn/despawn pair.
19-
- The default listen address of `UnityTransport` is now 0.0.0.0. (#2307)
2020
- Renamed the NetworkTransform.SetState parameter `shouldGhostsInterpolate` to `teleportDisabled` for better clarity of what that parameter does. (#2228)
2121
- Network prefabs are now stored in a ScriptableObject that can be shared between NetworkManagers, and have been exposed for public access. By default, a Default Prefabs List is created that contains all NetworkObject prefabs in the project, and new NetworkManagers will default to using that unless that option is turned off in the Netcode for GameObjects settings. Existing NetworkManagers will maintain their existing lists, which can be migrated to the new format via a button in their inspector. (#2322)
2222

com.unity.netcode.gameobjects/Editor/HiddenScriptEditor.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#endif
55
using Unity.Netcode.Transports.UTP;
66
using UnityEditor;
7+
using UnityEngine;
78

89
namespace Unity.Netcode.Editor
910
{
@@ -43,7 +44,109 @@ public class UNetTransportEditor : HiddenScriptEditor
4344
[CustomEditor(typeof(UnityTransport), true)]
4445
public class UnityTransportEditor : HiddenScriptEditor
4546
{
47+
private static readonly string[] k_HiddenFields = { "m_Script", "ConnectionData" };
4648

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+
}
47150
}
48151

49152
#if COM_UNITY_MODULES_ANIMATION

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,23 @@ public struct ConnectionAddressData
303303
public ushort Port;
304304

305305
/// <summary>
306-
/// IP address the server will listen on. If not provided, will use 0.0.0.0.
306+
/// IP address the server will listen on. If not provided, will use localhost.
307307
/// </summary>
308-
[Tooltip("IP address the server will listen on. If not provided, will use 0.0.0.0.")]
308+
[Tooltip("IP address the server will listen on. If not provided, will use localhost.")]
309309
[SerializeField]
310310
public string ServerListenAddress;
311311

312-
private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port)
312+
private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port, bool silent = false)
313313
{
314314
NetworkEndpoint endpoint = default;
315315

316316
if (!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv4) &&
317317
!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv6))
318318
{
319-
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
319+
if (!silent)
320+
{
321+
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
322+
}
320323
}
321324

322325
return endpoint;
@@ -336,13 +339,13 @@ public NetworkEndpoint ListenEndPoint
336339
{
337340
if (string.IsNullOrEmpty(ServerListenAddress))
338341
{
339-
var ep = NetworkEndpoint.AnyIpv4;
342+
var ep = NetworkEndpoint.LoopbackIpv4;
340343

341-
// If an address was entered and it's IPv6, switch to using :: as the
344+
// If an address was entered and it's IPv6, switch to using ::1 as the
342345
// default listen address. (Otherwise we always assume IPv4.)
343346
if (!string.IsNullOrEmpty(Address) && ServerEndPoint.Family == NetworkFamily.Ipv6)
344347
{
345-
ep = NetworkEndpoint.AnyIpv6;
348+
ep = NetworkEndpoint.LoopbackIpv6;
346349
}
347350

348351
return ep.WithPort(Port);
@@ -353,8 +356,11 @@ public NetworkEndpoint ListenEndPoint
353356
}
354357
}
355358
}
359+
360+
public bool IsIpv6 => !string.IsNullOrEmpty(Address) && ParseNetworkEndpoint(Address, Port, true).Family == NetworkFamily.Ipv6;
356361
}
357362

363+
358364
/// <summary>
359365
/// The connection (address) data for this <see cref="UnityTransport"/> instance.
360366
/// This is where you can change IP Address, Port, or server's listen address.

0 commit comments

Comments
 (0)