Photon Fusion

Exemple of Photon Fusion integration

This exemple is pseudo code giving an idea on how to implement our Network module. It may be different based on your Network Architecture and we recommend you to develop your own implementation.

This example only works in SHARED game mode

using System;
using Fusion;
using Kinetix;
using UnityEngine;

public class Player : NetworkBehaviour
{
    // This is the networked property itself
    [Networked(OnChanged = nameof(OnPoseChanged)), Capacity(2000)]
    public NetworkArray<byte> Pose { get; }


    private KinetixCharacterComponent kcc;
    private double timer = 0;
    private byte[] previousPose =null;


    public override void FixedUpdateNetwork()
    {
        base.FixedUpdateNetwork();
        
        // The pose system uses timestamps in order to determine the frame order
        timer = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
        

        if (kcc != null) 
        {
            // If we are the local player,
            // get the current pose and set the networked var
            if (HasInputAuthority) 
            {
                // This line will extract a networkable pose
                byte[] currentPose = ((KinetixCharacterComponentLocal) kcc).GetSerializedPose();

                if (currentPose != null || previousPose != null)
                {
                    Pose.CopyFrom(poseArray, 0, poseArray.Length);
                }

                previousPose = currentPose;
            }
        }
    }
    
    public override void Spawned()
    {
        base.Spawned();
        Animator anim = GetComponentInChildren<Animator>();

        if (HasInputAuthority) 
        {
            // Register our animator
            KinetixCore.Animation.RegisterLocalPlayerAnimator(anim);
            // If we are the local player, we want to get our KCC
            kcc = KinetixCore.Animation.GetLocalKCC();

            Object.RequestStateAuthority();
            
            if (!HasStateAuthority) 
                Debug.Log ("Failed to set authority");
        } 
        else 
        {
            KinetixCore.Network.RegisterRemotePeerAnimator(Id.ToString(), anim);
            this.kcc = KinetixCore.Network.GetRemoteKCC(Id.ToString());
        }
    }

    // Has to be public static void
    public static void OnPoseChanged(Changed<Player> changed)
    {
        changed.Behaviour.ApplyPose();
    }

    // Called on remote peers, apply the pose sent by local player
    private void ApplyPose()
    {
        ((KinetixCharacterComponentRemote) kcc).ApplySerializedPose(Pose.ToArray(), timer);
    }
}

Last updated