I’m creating a pretty jank multiplayer solution for my game that uses unity’s transport system. To keep it short, there is absolutely no explanation anywhere as to what the hell NetworkEndPoint.AnyIpv4 and NetworkEndPoint.LoopbackIpv4 are.
I’m basically trying to make it so you can join someone’s game by typing in their IP Address and Port, and Host the game by using your IPv4 Address and selecting a port. I am using Unity’s jobified client and server scripts in the documentation, but it doesn’t say why it chooses to use loopbackipv4 and anyipv4, nor what they do. Can I use NetworkEndPoint.Parse()? I tried using it, but it didn’t work. Any help is greatly appreciated.
For what it’s worth, I had the same problem today and am coming from a position of only having a high level understanding of networking and almost no Unity networking experience. Below is my solution.
Basically I convert my string IP into an IPAddress object using Parse and then I convert that into a NativeArray of byte data. The endpoint object is set to AnyIpv4 and I use SetRawAddressBytes(), sending it the NativeArray.
This may not be the best or only solution, this is just what I used today.
m_Driver = NetworkDriver.Create();
m_Connection = default(NetworkConnection);
// Basically I convert my string IP into an IPAddress object using Parse
IPAddress serverAddress = IPAddress.Parse(GameObject.Find("IP").GetComponent<InputField>().text);
NativeArray<byte> nativeArrayAddress;
// Convert that into a NativeArray of byte data
nativeArrayAddress = new NativeArray<byte>(serverAddress.GetAddressBytes().Length, Allocator.Temp);
nativeArrayAddress.CopyFrom(serverAddress.GetAddressBytes());
// Set to AnyIpv4
NetworkEndPoint endpoint = NetworkEndPoint.AnyIpv4;
endpoint.SetRawAddressBytes(nativeArrayAddress); // Use SetRawAddressBytes
endpoint.Port = 9000;
// Then call the Connect method on your driver.
m_Connection = m_Driver.Connect(endpoint);