Home > Mobile >  I want to make a P2P networked(thinking about using mirror) game with unity, with no dedicated serve
I want to make a P2P networked(thinking about using mirror) game with unity, with no dedicated serve

Time:10-28

I have managed to basically connect with my friend over internet, by forwarding my IP adress from my router settings... this is not viable because there are few people willing to do what I did to play games with their friends. So how to actually do UDP hole punching(basically what I did manually to my router) in unity using the mirror networking solution...

CodePudding user response:

A common solution to this problem is WebRTC, which takes care of the hole punching under the hood. Unity maintains this package which implements WebRTC. They also provide a great tutorial on how to use it. The nuts and bolts of it are:

using UnityEngine;
using Unity.WebRTC;

public class MyPlayerScript : MonoBehaviour
{

    RTCPeerConnection localConnection, remoteConnection;
    RTCDataChannel sendChannel, receiveChannel;

    private void Awake()
    {
        // Initialize WebRTC
        WebRTC.Initialize();
        // Create local peer
        localConnection = new RTCPeerConnection();
        sendChannel = localConnection.CreateDataChannel("sendChannel");
        channel.OnOpen = handleSendChannelStatusChange;
        channel.OnClose = handleSendChannelStatusChange;
        // Create remote peer
        remoteConnection = new RTCPeerConnection();
        remoteConnection.OnDataChannel = ReceiveChannelCallback;
        // register comms paths
        localConnection.OnIceCandidate = e => { 
            !string.IsNullOrEmpty(e.candidate)
            || remoteConnection.AddIceCandidate(ref e); 
        }
        remoteConnection.OnIceCandidate = e => { 
            !string.IsNullOrEmpty(e.candidate)
            || localConnection.AddIceCandidate(ref e); 
        }
        localConnection.OnIceConnectionChange = state => {
            Debug.Log(state);
        }
    }

    //handle begin
    IEnumerator Call(){
        var op1 = localConnection.CreateOffer();
        yield return op1;
        var op2 = localConnection.SetLocalDescription(ref op1.desc);
        yield return op2;
        var op3 = remoteConnection.SetRemoteDescription(ref op1.desc);
        yield return op3;
        var op4 = remoteConnection.CreateAnswer();
        yield return op4;
        var op5 = remoteConnection.setLocalDescription(op4.desc);
        yield return op5;
        var op6 = localConnection.setRemoteDescription(op4.desc);
        yield return op6;
    }

    //handle send messages
    void SendMessage(string message)
    {
        sendChannel.Send(message);
    }

    void SendBinary(byte[] bytes)
    {
        sendChannel.Send(bytes);
    }

    //handle receive messages
    void ReceiveChannelCallback(RTCDataChannel channel) 
    {
        receiveChannel = channel;
        receiveChannel.OnMessage = HandleReceiveMessage;  
    }
    void HandleReceiveMessage(byte[] bytes)
    {
        var message = System.Text.Encoding.UTF8.GetString(bytes);
        Debug.Log(message);
    }

    //handle end
    private void OnDestroy()
    {
        sendChannel.Close();
        receiveChannel.Close();

        localConnection.Close();
        remoteConnection.Close();

        WebRTC.Finalize();
    }

}

CodePudding user response:

I have also found a useful way to do this using mirror and the epic free relay for this. Thanks so much for the other answers, it really helped understand better what I needed to search and use!

CodePudding user response:

You can use Photon Bolt or Photon Fusion to let players host a game on their local machine like minecraft, etc. Photon provides relay as well as tries to use STUN to establish direct peer to peer connection to the host via UDP. PUN2 is also a good choice, although I like Photon Bolt/Fusion better - it's less of a simple RPC framework and more programmer oriented. Also, PUN does not do any STUN direct peer connection, it will always be relayed. Photon Bolt and Fusion will first attempt a STUN direct peer connection and then fallback to relay if necessary. It's been around for years and is the best choice.

Sure you can develop a Unity game with Mirror (although without a relay built in) but it's not nearly as easy to setup and use as Photon Bolt/Fusion and they don't provide a relay. As someone mentioned, you might be able to hack something together in some way but yeah - not recommended.

Ugh, yeah don't use WebRTC for a Unity game (or probably anything other than streaming music/video like it was made for to be honest).

Unity's MLAPI is "under development" and their last API was suddenly dropped "deprecated", so I wouldn't use that.

  • Related