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.
PhotonConnectManager is a global script attached one time to get information if the local player joined a room and then instantiate its prefab.
Player is a script attached to the player prefab and will register and unregister if its local player or a remote player.
using Photon.Pun;
using Kinetix;
using Newtonsoft.Json;
public class PhotonConnectionManager : MonoBehaviourPunCallbacks
{
// Called when local player joined room
public override void OnJoinedRoom()
{
GameObject gameObject = PhotonNetwork.Instantiate(PLAYER_PREFAB, Vector3.zero, Quaternion.identity);
Player player = gameObject.GetComponent<Player>();
player.Init()
}
}
using Photon.Pun;
using Kinetix;
public class Player : MonoBehaviourPun, IPunObservable
{
private KinetixCharacterComponent Kcc;
private AnimationIds nextAnimation;
private void Init()
{
Animator animator = GetComponent<Animator>();
if (photonView.AmOwner)
{
// Player is local player
KinetixCore.Animation.RegisterLocalPlayer(animator);
KinetixCore.Animation.OnAnimationStartOnLocalPlayerAnimator += (_Ids) => nextAnimation = _Ids;
}
else
{
peerID = PhotonView.Get(this).ViewID;
// Player is remote player
KinetixCore.Network.RegisterRemotePeerAnimator(peerID, animator);
}
}
public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
{
if (Kcc == null) return;
if (stream.IsWriting)
{
// If the animation ids have been set
if (nextAnimation != null)
{
// Serialize and send on stream, then set it to null
stream.SendNext(JsonConvert.SerializeObject (nextAnimation));
nextAnimation = null;
}
}
else
{
// When receiving an emote to play from a distant player
if (stream.Count > 0)
{
string nextAnimationJson = (string) stream.ReceiveNext();
nextAnimation = JsonConvert.DeserializeObject<AnimationIds>(nextAnimationJson);
((KinetixCharacterComponentRemote) Kcc).PlayAnimation(nextAnimation);
}
}
}
private void OnDestroy()
{
if (photonView.AmOwner)
{
KinetixCore.Animation.UnregisterLocalPlayer();
KinetixCore.Network.UnregisterAllRemotePeers();
}
else
{
KinetixCore.Network.UnregisterRemotePeer(peerID);
}
}
}