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
{
 
    [Networked(OnChanged = nameof(NetworkAnimChanged))]
    [Capacity(100)]
    public string AnimID { get; set; }

    private KinetixCharacterComponent kcc;

    
    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());
        }
        

        if (HasInputAuthority && IsLocal) 
	{
	    KinetixCore.Animation.RegisterLocalPlayerAnimator(anim);
	    kcc = KinetixCore.Animation.GetLocalKCC();

            KinetixCore.Animation.OnAnimationStartOnLocalPlayerAnimator += (_AnimIds) => {
	        AnimID = _AnimIds.UUID;
	    };

            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 NetworkAnimChanged(Changed<Player> changed)
    {
        string animID = changed.Behaviour.AnimID;

        changed.Behaviour.ApplyPose(animID);
    }

    public void ApplyPose(string animID)
    {
	if (!IsLocal) ((KinetixCharacterComponentRemote)kcc).PlayAnimation(animID);
    }
}

Last updated