Search Unity

Question How to use NetworkSerializable with CustomStruct ,Transfer between Server and client

Discussion in 'Netcode for GameObjects' started by andrew-yu-ho, May 15, 2022.

  1. andrew-yu-ho

    andrew-yu-ho

    Joined:
    Mar 29, 2019
    Posts:
    4
    I custom serializable types String Array ,
    learn https://docs-multiplayer.unity3d.co...ced-topics/serialization/inetworkserializable
    and try it.
    I Expect the result is:

    Server Received the RPC #0
    #Test Server RPC#:a
    Server Received the RPC #1
    #Test Server RPC#:a
    Server Received the RPC #2
    ...............................​

    But Real Rsult :
    Server Received the RPC #0
    #Test Server RPC#:a
    Server Received the RPC #1
    Server Received the RPC #2
    Server Received the RPC #3
    .........................................​

    Can Tell me Why, Detail..
    ///////////////////////////////////////////////////////////////////////////
    The code as below
    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. public class RpcTest : NetworkBehaviour
    5. {
    6.  
    7.     private MyCustomStruct mystring;
    8.  
    9.     public override void OnNetworkSpawn()
    10.     {
    11.        
    12.         mystring.Array = new string[]{ "a", "b", "c" };
    13.         if (IsClient)
    14.         {
    15.             TestServerRpc(0,mystring);
    16.         }
    17.     }
    18.  
    19.     [ClientRpc]
    20.     void TestClientRpc(int value,  MyCustomStruct mystring)
    21.     {
    22.         if (IsClient)
    23.         {
    24.             Debug.Log("Client Received the RPC #" + value);
    25.             Debug.Log("#Test Client RPC#:" + mystring.Array[0].ToString());
    26.             TestServerRpc(value + 1, mystring);
    27.         }
    28.     }
    29.  
    30.     [ServerRpc]
    31.     void TestServerRpc(int value, MyCustomStruct mystring)
    32.     {
    33.         Debug.Log("Server Received the RPC #" + value);
    34.         Debug.Log("#Test Server RPC#:" + mystring.Array[0].ToString());
    35.         TestClientRpc(value, mystring);
    36.     }
    37.  
    38.     public struct MyCustomStruct : INetworkSerializable
    39.     {
    40.         public string[] Array;
    41.  
    42.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    43.         {
    44.             // Length
    45.             int length = 0;
    46.             if (!serializer.IsReader)
    47.             {
    48.                 length = Array.Length;
    49.             }
    50.  
    51.             serializer.SerializeValue(ref length);
    52.  
    53.             // Array
    54.             if (serializer.IsReader)
    55.             {
    56.                 Array = new string[length];
    57.             }
    58.  
    59.             for (int n = 0; n < length; ++n)
    60.             {
    61.                 serializer.SerializeValue(ref Array[n]);
    62.             }
    63.         }
    64.     }
    65. }
     
  2. andrew-yu-ho

    andrew-yu-ho

    Joined:
    Mar 29, 2019
    Posts:
    4
    OK~ I found the sloution~~~
    modify to:

    Debug.Log("Server Received the RPC #" + value + " this String: "++ mystring.Array[0]);

    This seems to solve it, although I don't know why