Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to send and receive unconnected packets in Unity Transport #3073

Open
SubatomicPlanets opened this issue Sep 21, 2024 · 1 comment
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feature New feature, request or improvement

Comments

@SubatomicPlanets
Copy link

This is a feature related to the Unity Transport, not Netcode for GameObjects directly, so I don't know if this is the right place.
I want to send and receive packets without any connection. In my specific case, I want to send some data to a server. This server is made in C++ and has nothing to do with Unity. I just want to send something using the socket used by the Unity Transport.

I would like to see a function like .SendTo(message, IPEndPoint) and some way of receiving a response like .ReceiveFrom(IPEndPoint). They could be part of the NetworkDriver.

Right now I am using the Ruffles transport from the multiplayer-community-contributions. Ruffles allows me to send such packets using the .SendRaw function. This is not ideal, as I would like to use the official Unity Transport.

This would also allow an easy implementation of NAT hole punching.

@SubatomicPlanets SubatomicPlanets added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feature New feature, request or improvement labels Sep 21, 2024
@SubatomicPlanets
Copy link
Author

I got it working.

Just in case anyone ever needs this or if anyone would like to know how I did it, here is a simple explanation:

I modified the Unity transport package. I added a function to the NetworkDriver called DirectSend. For receiving I added a new file called DirectDataQueue which you can pop from to get the newest received packet. I also had to modify the SimpleConnectionLayer code a bit.
It isn't a perfect solution but it works well enough in my case. To access the driver in Unity from my own scripts I just made a new script which inherits from UnityTransport and makes the driver public. I could have also just modified the UnityTransport script, but it is in the netcode package and I didn't want to modify another package.

I managed to add NAT punching support with this. By the way, I just did this as a hobby project, not for something I'm going to release. I had to understand the entire Unity Transport code first before doing this but somehow, miraculously, it only took 4 days from starting to read up on the NetworkDriver to having NAT punching implemented.

I will leave this issue open since official support for something like this would be nice, but feel free to close it if you want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:feature New feature, request or improvement
Projects
None yet
Development

No branches or pull requests

1 participant