Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Custom NetworkVariable not syncing

Discussion in 'Netcode for GameObjects' started by YuriyVotintsev, Dec 20, 2022.

  1. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    93
    Here is code of my implementation of custom network variable. I am trying to sync array of character bones. Size of array of Bones is fixed.
    Client writes values, server reads. Problem is that ReadField or ReadDelta did not run on server side.

    Code (CSharp):
    1. using System;
    2. using Unity.Netcode;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class NetworkBoneArray : NetworkVariableBase
    7. {
    8.     private Bone[] _bones;
    9.  
    10.     public Bone this[int i]
    11.     {
    12.         get => _bones[i];
    13.         set
    14.         {
    15.             _bones[i] = value;
    16.             SetDirty(true);
    17.         }
    18.     }
    19.  
    20.     public NetworkBoneArray(int count, NetworkVariableReadPermission readPerm = DefaultReadPerm,
    21.         NetworkVariableWritePermission writePerm = DefaultWritePerm) : base(readPerm, writePerm)
    22.     {
    23.         _bones = new Bone[count];
    24.     }
    25.  
    26.     public override void WriteField(FastBufferWriter writer)
    27.     {
    28.         Debug.Log("Write field");
    29.         for (var i = 0; i < _bones.Length; i++)
    30.         {
    31.             var bone = _bones[i];
    32.             writer.WriteValueSafe(bone.LocalPosition);
    33.             writer.WriteValueSafe(bone.LocalRotation);
    34.         }
    35.     }
    36.  
    37.     public override void ReadField(FastBufferReader reader)
    38.     {
    39.         Debug.Log("Read field");
    40.         for (var i = 0; i < _bones.Length; i++)
    41.         {
    42.             var bone = new Bone();
    43.             reader.ReadValueSafe(out bone.LocalPosition);
    44.             reader.ReadValueSafe(out bone.LocalRotation);
    45.             _bones[i] = bone;
    46.         }
    47.     }
    48.  
    49.     public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
    50.     {
    51.         Debug.Log("Read Delta");
    52.         ReadField(reader);
    53.     }
    54.  
    55.     public override void WriteDelta(FastBufferWriter writer)
    56.     {
    57.         Debug.Log("Write Delta");
    58.         WriteField(writer);
    59.     }
    60. }
    61.  
    62. public struct Bone
    63. {
    64.     public Vector3 LocalPosition;
    65.     public Quaternion LocalRotation;
    66.  
    67.     public static implicit operator Bone(Transform t)
    68.     {
    69.         return new Bone {LocalPosition = t.localPosition, LocalRotation = t.localRotation};
    70.     }
    71. }
    72.  
    73. public class BodySync : NetworkBehaviour
    74. {
    75.     [SerializeField] private Transform[] Bones;
    76.  
    77.     public NetworkBoneArray NetworkBones;
    78.  
    79.     public void Awake()
    80.     {
    81.         NetworkBones = new NetworkBoneArray(Bones.Length, writePerm: NetworkVariableWritePermission.Owner);
    82.         NetworkBones.Initialize(this);
    83.     }
    84.  
    85.     void Update()
    86.     {
    87.         if (IsOwner)
    88.         {
    89.             for (var i = 0; i < Bones.Length; i++)
    90.             {
    91.                 NetworkBones[i] = Bones[i];
    92.             }
    93.             NetworkBones.SetDirty(true);
    94.         }
    95.         else
    96.         {
    97.             for (var i = 0; i < Bones.Length; i++)
    98.             {
    99.                 SetBoneToTransform(Bones[i], NetworkBones[i]);
    100.             }
    101.         }
    102.     }
    103.  
    104.     private static void SetBoneToTransform(Transform transform, Bone bone)
    105.     {
    106.         transform.localPosition = bone.LocalPosition;
    107.         transform.localRotation = bone.LocalRotation;
    108.     }
    109. }
    What am i doing wrong?
     
  2. YuriyVotintsev

    YuriyVotintsev

    Joined:
    Jun 11, 2013
    Posts:
    93
    Ups.. it was fault not related to this code. Code works fine.