我的 Unity 2D 游戏中的 OnSerializeNetworkView 问题
OnSerializeNetworkView issus in my Unity 2D game
我有 2 个脚本:
- 网络管理器
- PlayerScript
和游戏对象播放器:
对于控制网络,我有 GameObject 脚本:
PlayerScript 代码:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float speed = 10f;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private void InputMovement()
{
if (Input.GetKey(KeyCode.W))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.right * speed * Time.deltaTime);
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Debug.Log("OnSerializeNetworkView!!!!!!!!!!!!!!!!!!!!");
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody2D.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody2D.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody2D.position;
}
}
void Awake()
{
lastSynchronizationTime = Time.time;
}
void Update()
{
if (networkView.isMine)
{
InputMovement();
}
else
{
SyncedMovement();
}
}
[RPC]
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody2D.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
Debug.Log(syncStartPosition+ " ####### " +rigidbody2D.position+ " ####### " +syncEndPosition);
}
}
和 NetworkManager 代码:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public Transform point1;
public Transform point2;
public GameObject player;
// Use this for initialization
void Start () {
if (Network.isServer) {
Network.Instantiate(player,point1.transform.position,Quaternion.identity,0);
}
}
void OnPlayerConnected ( NetworkPlayer netPlayer)
{
Debug.Log("Player " + " connected from " + netPlayer.ipAddress + ":" + netPlayer.port);
networkView.RPC("net_DoSpawn",RPCMode.All);
}
[RPC]
void net_DoSpawn()
{
Network.Instantiate(player,point2.transform.position,Quaternion.identity,0);
}
}
我试了很多次调试id,都没有变化。 OnSerializeNetworkView 没有被调用并且玩家被生成两次。如果我试图去其他玩家的位置他们开始闪烁,否则他们不可见..有人可以帮助我吗?非常感谢和抱歉我的英语:))
您必须遵守脚本,而不是转换。
将脚本组件拖到观察到的插槽中。然后就会触发。
我有 2 个脚本:
- 网络管理器
- PlayerScript
和游戏对象播放器:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float speed = 10f;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private void InputMovement()
{
if (Input.GetKey(KeyCode.W))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rigidbody2D.MovePosition(rigidbody2D.position + Vector2.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rigidbody2D.MovePosition(rigidbody2D.position - Vector2.right * speed * Time.deltaTime);
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Debug.Log("OnSerializeNetworkView!!!!!!!!!!!!!!!!!!!!");
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody2D.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody2D.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody2D.position;
}
}
void Awake()
{
lastSynchronizationTime = Time.time;
}
void Update()
{
if (networkView.isMine)
{
InputMovement();
}
else
{
SyncedMovement();
}
}
[RPC]
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody2D.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
Debug.Log(syncStartPosition+ " ####### " +rigidbody2D.position+ " ####### " +syncEndPosition);
}
}
和 NetworkManager 代码:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public Transform point1;
public Transform point2;
public GameObject player;
// Use this for initialization
void Start () {
if (Network.isServer) {
Network.Instantiate(player,point1.transform.position,Quaternion.identity,0);
}
}
void OnPlayerConnected ( NetworkPlayer netPlayer)
{
Debug.Log("Player " + " connected from " + netPlayer.ipAddress + ":" + netPlayer.port);
networkView.RPC("net_DoSpawn",RPCMode.All);
}
[RPC]
void net_DoSpawn()
{
Network.Instantiate(player,point2.transform.position,Quaternion.identity,0);
}
}
我试了很多次调试id,都没有变化。 OnSerializeNetworkView 没有被调用并且玩家被生成两次。如果我试图去其他玩家的位置他们开始闪烁,否则他们不可见..有人可以帮助我吗?非常感谢和抱歉我的英语:))
您必须遵守脚本,而不是转换。
将脚本组件拖到观察到的插槽中。然后就会触发。