Advertisement
AtefBoutara

Synch Parent

Oct 3rd, 2022
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using Fusion;
  2. using UnityEngine;
  3.  
  4. // This component will sync parenting of a transform,
  5. // using either a NetworkObject or NetworkBehaviour reference.
  6. public class WrapUnwrapSample : NetworkBehaviour
  7. {
  8.  
  9.   // Networked NetworkObject with auto properties:
  10.   // The ILWeaver replaces the Get method with code which writes the NetworkId
  11.   // to the snapshot, and the Set method with handling which finds the Object using the NetworkId.
  12.   // Get will return the Object if found.
  13.   // Get will return null if the property was set to null, and also will return null if the Object could not be found.
  14.   [Networked] public NetworkObject    MyNetworkObject    { get; set; }
  15.  
  16.   // Networked NetworkBehaviour with auto properties:
  17.   // The ILWeaver replaces the Get method with an implementation which writes the NetworkBehaviourId
  18.   // to the snapshot, and the Set method implementation which finds the Object using the NetworkBehaviourId.
  19.   // Get will return the Object if found.
  20.   // Get will return null if the property was set to null, and also will return null if the Object could not be found.
  21.   [Networked] public NetworkBehaviour MyNetworkBehaviour { get; set; }
  22.  
  23.   // NetworkId is backed by one int value.
  24.   // Zero represents null/Invalid.
  25.   [Networked] public NetworkId ParentNetId { get; set; }
  26.  
  27.   // NetworkBehaviourId is backed by two values:
  28.   // Object = The NetworkId of the NetworkObject
  29.   // Behaviour = NetworkBehaviour index in the hierarchy of the NetworkObject.
  30.   [Networked] public NetworkBehaviourId ParentNBId { get; set; }
  31.  
  32.   public override void FixedUpdateNetwork()
  33.   {
  34.     if (Object.HasStateAuthority)
  35.     {
  36.       CaptureParent();
  37.     }
  38.     else
  39.     {
  40.       ApplyParent();
  41.     }
  42.   }
  43.  
  44.   private void CaptureParent()
  45.   {
  46.     var parent = transform.parent;
  47.  
  48.     // If the current parent is null, set the Object value to default (0).
  49.     // This represents null.
  50.     if (parent == null)
  51.     {
  52.       ParentNetId = default;
  53.       ParentNBId = default;
  54.       return;
  55.     }
  56.    
  57.     // Find a NetworkBehaviour on the parent and capture its NetworkId.
  58.     var parentNB =  parent.GetComponent<NetworkBehaviour>();
  59.     if (parentNB != null)
  60.     {
  61.       ParentNBId  = parentNB.Id;
  62.       ParentNetId = default;
  63.     }
  64.     // if the parent does not have a NetworkBehaviour component,
  65.     // find a NetworkObject instead.  
  66.     else
  67.     {
  68.       var parentNO = parent.GetComponent<NetworkObject>();
  69.       if (parentNO != null) {
  70.         ParentNetId = parentNO.Id;
  71.         ParentNBId = default;
  72.       }
  73.       else
  74.       {
  75.         Debug.LogWarning("Parent on StateAuthority does not have a NetworkObject or NetworkBehaviour. Cannot be identified and serialized.");
  76.         ParentNetId = default;
  77.         ParentNBId  = default;        
  78.       }
  79.     }
  80.   }
  81.  
  82.   private void ApplyParent()
  83.   {
  84.     // Test for an NetworkId == 0 (same as Invalid),
  85.     // which represents an explicit null value.
  86.     if (ParentNBId.Object.IsValid == false && ParentNetId.IsValid == false)
  87.     {
  88.       transform.SetParent(null);
  89.       return;
  90.     }
  91.    
  92.     // If the parent was a NetworkObject Id, try to unwrap it.
  93.     if (ParentNetId.IsValid)
  94.     {
  95.       if (Runner.TryFindObject(ParentNetId, out var parentNO))
  96.       {
  97.         transform.SetParent(parentNO.transform);
  98.       }
  99.       else
  100.       {
  101.         Debug.LogWarning($"NetworkObject:{ParentNBId.Object} not found on the local runner. " +
  102.                          "NetworkObject is possibly out of Interest.");
  103.         transform.SetParent(null);
  104.       }
  105.       return;
  106.     }
  107.  
  108.     // if the parent was a NetworkBehaviour, try to unwrap it.
  109.     if (ParentNBId.IsValid)
  110.     {
  111.       if (Runner.TryFindBehaviour(ParentNBId, out var parentNB))
  112.       {
  113.         transform.SetParent(parentNB.transform);
  114.       } else
  115.       {
  116.         Debug.LogWarning($"NetworkObject:{ParentNBId.Object} NB:{ParentNBId.Behaviour} not found on the local runner. " +
  117.                          "NetworkObject is possibly out of Interest.");
  118.         transform.SetParent(null);
  119.       }
  120.     }
  121.   }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement