简体   繁体   English

Unity 2D游戏中的OnSerializeNetworkView issus

[英]OnSerializeNetworkView issus in my Unity 2D game

I have 2 Scripts: 我有2个脚本:

  • NetworkManager 网络管理器
  • PlayerScript PlayerScript

and GameObject Player: 和GameObject Player: 播放机 for control network i have GameObject Scripts: 对于控制网络,我有GameObject脚本: 脚本 PlayerScript code: 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);
}

}

and NetworkManager code: 和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);
}
}

I tried many times to debug id, but no change. 我尝试了很多次来调试ID,但是没有任何变化。 OnSerializeNetworkView isn't called and player are spawned twice.. If I'am trying to go to other players positions they start flashing, otherwise they are not visible.. Anybody can help me please? 不会调用OnSerializeNetworkView,并且会重生播放器两次。.如果我要转到其他播放器位置,它们将开始闪烁,否则它们将不可见。.有人可以帮助我吗? A lot of thanks and sorry for my English :)) 非常感谢和抱歉我的英语:))

You must observe the script, not the transform. 您必须观察脚本,而不是转换。

Drag the scrip component to the observed slot. 将Scrip组件拖到观察到的插槽中。 Then it will trigger. 然后它将触发。

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

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