简体   繁体   English

Unity3D On Photon Serialize 视图

[英]Unity3D On Photon Serialize view

I am using this code on a photon view(c#) in the Unity game engine/Photon unity networking.我在 Unity 游戏引擎/Photon unity 网络中的光子视图(c#)上使用此代码。 For the purpose of networking animations and other stuff however for some reason the code just won't network my animations.出于网络动画和其他内容的目的,但由于某种原因,代码不会将我的动画网络化。

using UnityEngine;
using System.Collections;

public class NetworkCharacter : Photon.MonoBehaviour {

    Vector3 realPos = Vector3.zero;
    Quaternion realRot = Quaternion.identity;
    Animator anim;
    bool gotFU = false;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        if (photonView.isMine) 
        {

        } 
        else 
        {
            transform.position = Vector3.Lerp (transform.position, realPos, 0.1f);
            transform.rotation = Quaternion.Lerp (transform.rotation, realRot, 0.1f);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting) 
        {
            stream.SendNext (transform.position);
            stream.SendNext (transform.rotation);
            stream.SendNext (anim.GetFloat("Speed"));
            stream.SendNext (anim.GetFloat("Strafe"));
        } 
        else 
        {
            realPos = (Vector3)stream.ReceiveNext ();
            realRot = (Quaternion)stream.ReceiveNext ();
            anim.SetFloat("Speed", (float)stream.ReceiveNext());
            anim.SetFloat("Strafe", (float)stream.ReceiveNext());


            if (gotFU == false) 
            {
                transform.position = realPos;
                transform.rotation = realRot;
                gotFU = true;
            }


        }


    }
}

To synchronize position and rotation you can use Photon View and Photon Transform View component. 要同步位置和旋转,可以使用“光子视图”和“光子变换视图”组件。

Please check it out: Serialization in Photon 请检查一下: Photon中的序列化

As the previous answer said, for sending the position / rotation of objects you can just use the PhotonTransformView and PhotonView components.正如前面的答案所说,要发送 position / 旋转对象,您可以只使用PhotonTransformViewPhotonView组件。 You can also synchronize your animations using the PhotonAnimatorView .您还可以使用PhotonAnimatorView同步您的动画。

If you really want to use the OnPhotonSerializeView method, make sure that the object that the script is on has a PhotonView component on it too, else OnPhotonSerializeView will fail to run.如果真的要使用OnPhotonSerializeView方法,请确保脚本所在的 object 上也有 PhotonView 组件,否则OnPhotonSerializeView将无法运行。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM