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.
usingPhoton.Pun;usingKinetix;usingNewtonsoft.Json;publicclassPhotonConnectionManager:MonoBehaviourPunCallbacks{ // Called when local player joined roompublicoverridevoidOnJoinedRoom() {GameObject gameObject =PhotonNetwork.Instantiate(PLAYER_PREFAB,Vector3.zero,Quaternion.identity);Player player =gameObject.GetComponent<Player>();player.Init() } }
usingPhoton.Pun;usingKinetix;publicclassPlayer:MonoBehaviourPun,IPunObservable{privateKinetixCharacterComponent Kcc;privateAnimationIds nextAnimation;privatevoidInit() {Animator animator =GetComponent<Animator>();if (photonView.AmOwner) { // Player is local playerKinetixCore.Animation.RegisterLocalPlayer(animator);KinetixCore.Animation.OnAnimationStartOnLocalPlayerAnimator+= (_Ids) => nextAnimation = _Ids; }else { peerID =PhotonView.Get(this).ViewID; // Player is remote playerKinetixCore.Network.RegisterRemotePeerAnimator(peerID, animator); } }publicvoidOnPhotonSerializeView (PhotonStream stream,PhotonMessageInfo info) {if (Kcc ==null) return;if (stream.IsWriting) { // If the animation ids have been setif (nextAnimation !=null) { // Serialize and send on stream, then set it to nullstream.SendNext(JsonConvert.SerializeObject (nextAnimation)); nextAnimation =null; } } else { // When receiving an emote to play from a distant playerif (stream.Count>0) {string nextAnimationJson = (string) stream.ReceiveNext(); nextAnimation =JsonConvert.DeserializeObject<AnimationIds>(nextAnimationJson); ((KinetixCharacterComponentRemote) Kcc).PlayAnimation(nextAnimation); } } }privatevoidOnDestroy() {if (photonView.AmOwner) {KinetixCore.Animation.UnregisterLocalPlayer();KinetixCore.Network.UnregisterAllRemotePeers(); }else {KinetixCore.Network.UnregisterRemotePeer(peerID); } }}