Search Unity

Third Party PUN2 - OnPhotonSerializeView() not giving any feedback.

Discussion in 'Multiplayer' started by Agilapathy, Mar 3, 2019.

  1. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    I have a simple script attached to a game object in my scene, its being observed by a PhotonView:
    upload_2019-3-3_15-36-55.png
    The script looks like this:

    Code (CSharp):
    1. using Photon.Pun;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PVTest : MonoBehaviour, IPunObservable {
    7.  
    8.     private string recievedData = "Nothing!";
    9.  
    10.     void Update()
    11.     {
    12.         print(recievedData);
    13.     }
    14.  
    15.     void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    16.     {
    17.         if (stream.IsWriting)
    18.         {
    19.             stream.SendNext(PhotonNetwork.NickName);
    20.         }
    21.         else if (stream.IsReading)
    22.         {
    23.             recievedData = stream.ReceiveNext().ToString();
    24.         }
    25.     }
    26. }
    I open two instances of my game (One in the editor, the other "Build and Run") and the console only prints the default of "Nothing!", like the OnPhotonSerializeView isn't working at all. What am I doing wrong here?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
  3. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    I did this. Unfortunately it didn't work.
    Code (CSharp):
    1. using Photon.Pun;
    2. using UnityEngine;
    3.  
    4. public class PVTest : MonoBehaviourPunCallbacks {
    5.  
    6.     private string recievedData = "Nothing!";
    7.  
    8.     public override void OnEnable()
    9.     {
    10.         base.OnEnable();
    11.         PhotonNetwork.AddCallbackTarget(this);
    12.     }
    13.  
    14.     public override void OnDisable()
    15.     {
    16.         base.OnDisable();
    17.         PhotonNetwork.RemoveCallbackTarget(this);
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         print(recievedData);
    23.     }
    24.  
    25.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    26.     {
    27.         if (stream.IsWriting)
    28.         {
    29.             print("Working!");
    30.             stream.SendNext(PhotonNetwork.NickName);
    31.         }
    32.         else if (stream.IsReading)
    33.         {
    34.             recievedData = stream.ReceiveNext().ToString();
    35.         }
    36.     }
    37. }
    38.  
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    You should find this works okay...

    Code (CSharp):
    1. using Photon.Pun;
    2. using UnityEngine;
    3.  
    4. public class PVTest : MonoBehaviourPun, IPunObservable
    5. {
    6.     private string recievedData = "Nothing!";
    7.  
    8.     public void OnEnable()
    9.     {
    10.         PhotonNetwork.AddCallbackTarget(this);
    11.     }
    12.  
    13.     public void OnDisable()
    14.     {
    15.         PhotonNetwork.RemoveCallbackTarget(this);
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (photonView.IsMine)
    21.             Debug.Log(PhotonNetwork.NickName);
    22.         else
    23.             Debug.Log(recievedData);
    24.     }
    25.  
    26.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    27.     {
    28.         if (stream.IsWriting)
    29.         {
    30.             stream.SendNext(PhotonNetwork.NickName);
    31.         }
    32.         else if (stream.IsReading)
    33.         {
    34.             recievedData = stream.ReceiveNext().ToString();
    35.         }
    36.     }
    37.  
    38. }
    Just to clarify, you should add the script to your player object and drag it into the ObservedComponents field in the PhotonView component and set synchronization mode to UnreliableOnChange.

    Also, bear in mind that OnPhotonSerializeView will only be called when you have at least 2 clients in the same room.
     
    Last edited: Mar 7, 2019