Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question How to Set Protocol Type?

Discussion in 'Unity Transport' started by liquify, Jul 22, 2023.

  1. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    As far as I know, we can only set the Protocol Type of Unity Transport in Inspector. Is there a way to set it with code?
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    When you call
    SetConnectionData
    or
    SetRelayServerData
    , the protocol automatically gets updated to the appropriate value (respectively, "Unity Transport" or "Relay Unity Transport").
     
  3. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    In the Inspector, I set the Protocol Type of Unity Transport to Relay Unity Transport, then I tried the following code:

    Code (CSharp):
    1. private async Task<Allocation> AllocateRelay()
    2. {
    3.     try
    4.     {
    5.         Allocation allocation = await RelayService.Instance.CreateAllocationAsync(4);
    6.         return allocation;
    7.     }
    8.     catch (RelayServiceException e)
    9.     {
    10.         Debug.Log(e);
    11.         return default;
    12.     }
    13. }
    14.  
    15. private async void StartHostForSingleplayerMode()
    16. {
    17.     Allocation allocation = await AllocateRelay();
    18.     NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(allocation, "dtls"));
    19.     NetworkManager.Singleton.StartHost();
    20. }
    21.  
    22. private void Start()
    23. {
    24.     StartHostForSingleplayerMode();
    25. }
    If I used "dtls" as the connectionType, the Protocol Type will still be set to Relay Unity Transport in the Inspector.

    But if I used "udp" as the connectionType, the "NetworkManager.Singleton.StartHost()" line will cause Null Reference Exception error.

    Is the "udp" connectionType supposed to set the Protocol Type to Unity Transport or should I use another method?
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Can you post the stack trace for that null reference exception?
     
  5. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    After re-trying "udp" as the connectionType, the "NetworkManager.Singleton.StartHost()" line doesn't cause Null Reference Exception error anymore. Maybe I forgot to recompile the scripts or there was a connection problem with the relay server.

    But the Protocol Type is still Relay Unity Transport in Inspector, despite using "udp" as the connectionType. Should I use another connectionType to set the Protocol Type to Unity Transport at runtime?
     
  6. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    No, if you use Relay it is expected for the protocol type to be "Relay Unity Transport". Doesn't matter if you use "udp" or "dtls" as the connection type. The "Unity Transport" connection type is for direct IP connections (i.e. not using Relay).
     
  7. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    So, if we select Relay Unity Transport as the Protocol Type in the Inspector, is it possible to change it to Unity Transport at runtime using code? If yes, could you please show the code example?
     
    Last edited: Jul 26, 2023
  8. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Yes, it's possible. If you set the IP address and port to connect to with
    SetConnectionData
    , it will automatically switch the protocol type to "Unity Transport". Here's an example:
    Code (CSharp):
    1. NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData("127.0.0.1", 7777);
     
    Angagsu-1 and liquify like this.
  9. liquify

    liquify

    Joined:
    Dec 9, 2014
    Posts:
    187
    Thanks, it works