Home > other >  OnPhotonSerializeView callback is not working in photon pun2
OnPhotonSerializeView callback is not working in photon pun2

Time:11-05

I'm creating multi-play game using photon pun2.

I'm setted the components inherited from the IPunObservable to object contain a photon view. but, OnPhotonSerializeView is never called.

This code is a function that synchronizes the position and others. but, it didn't work. and it doesn't catch a break point.

This object is created through PhotonNetwork.Instantiate, and here is the contents of the source and prefab.

using Photon.Pun;
using UnityEngine;

public class Character : MonoBehaviourPun, IPunObservable
{
    [Header("Properties")]
    public float hp;
    public float maxHp;
    public bool isMoving;
    public float moveSpeed;
    public Vector2 moveDirection;

    private void OnEnable()
    {
        PhotonNetwork.AddCallbackTarget(this);
    }

    private void OnDisable()
    {
        PhotonNetwork.RemoveCallbackTarget(this);
    }

/** skip **/

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        Debug.Log(nameof(OnPhotonSerializeView));

        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(hp);
            stream.SendNext(maxHp);
            stream.SendNext(moveSpeed);
            stream.SendNext(isMoving);
            stream.SendNext(moveDirection);
        }
        else if (stream.IsReading)
        {
            transform.position = (Vector3)stream.ReceiveNext();
            hp = (float)stream.ReceiveNext();
            maxHp = (float)stream.ReceiveNext();
            moveSpeed = (float)stream.ReceiveNext();
            isMoving = (bool)stream.ReceiveNext();
            moveDirection = (Vector2)stream.ReceiveNext();
        }
    }

}

enter image description here

Or is there anything I missed about OnPhotonSerializeView?

I understand that it repeats every second as many frames as specified by PhotonNetwork.SerializationRate or PhotonNetwork.SendRate.

sorry about my poor english...

I want to know about is why not OnPhotonSerializeView called or my wrong knowledge about that.

added

I'm checked that OnPhotonSerializeView called when player entered two or more. but, not.

I'm used PhotonTransformView. but, it's not synchronized transform too.

PunRPC is working normally.

this is skipped source

    private void FixedUpdate()
    {
        if (photonView.IsMine)
        {
            MoveEvent();
        }
    }

    protected virtual void MoveEvent()
    {
        isMoving = moveDirection != Vector2.zero;

        if (isMoving)
        {
            transform.position = transform.position   moveDirection * moveSpeed * Time.deltaTime;
        }
    }

added

enter image description here

i founded cloned object on other client view. and it's properties are empty. why is this happening?

CodePudding user response:

there is problem on creating room! i will check my room options or typed lobby. Thank you to everyone try to help me.

  • Related