我在 Unity 游戏引擎/Photon unity 网络中的光子视图(c#)上使用此代码。出于网络动画和其他内容的目的,但由于某种原因,代码不会将我的动画网络化。
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;
}
}
}
}