网络问题 - 服务器控制客户端

时间:2015-03-06 19:00:32

标签: c# networking unity3d unityscript multiplayer

我正在尝试开发一个人们可以闲聊和聊天的在线游戏,但由于某种原因,客户端的多维数据集不会显示在服务器的游戏上,反之亦然,但服务器控制客户端和服务器。以下是两个文件的代码:

网络脚本:

using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour
{

string registeredGameName = "Hangout Zone";
bool isRefreshing = false;
float refreshRequestLength = 3.0f;
HostData[] hostData;

private void StartServer()
{
    Network.InitializeServer(16, 25002, false);
    MasterServer.RegisterHost(registeredGameName, "Hangout Zone", "The place to chill with friends.");
}

void OnServerInitialized()
{
    Debug.Log("Server has been initialized.");
}

void OnPlayerDisconnected(NetworkPlayer player)
{
    Debug.Log("Player disconnected.");
    Network.RemoveRPCs(player);
    Network.DestroyPlayerObjects(player);
}

void OnApplicationQuit()
{
    if (Network.isServer)
    {
        Network.Disconnect(200);
        MasterServer.UnregisterHost();
    }

    if (Network.isClient)
    {
        Network.Disconnect(200);
    }
}

void OnMasterServerEvent(MasterServerEvent masterServerEvent)
{
    if (masterServerEvent == MasterServerEvent.RegistrationSucceeded)
    {
        Debug.Log("Registration Successful.");
    }
}

public IEnumerator RefreshHostList()
{
    Debug.Log("Refreshing...");
    MasterServer.RequestHostList(registeredGameName);
    float timeStarted = Time.time;
    float timeEnd = Time.time + refreshRequestLength;

    while (Time.time < timeEnd)
    {
        hostData = MasterServer.PollHostList();
        yield return new WaitForEndOfFrame();
    }

    if (hostData == null || hostData.Length == 0)
    {
        Debug.Log("No active servers have been found.");
    }
    else
    {
        Debug.Log(hostData.Length + " have been found.");
    }

}

private void SpawnPlayer()
{
    Debug.Log("Spawning player...");
    Network.Instantiate(Resources.Load("Prefabs/Block"), new Vector3(0f, 2.5f, 0f), Quaternion.identity, 0);
}



public void OnGUI()
{

    if (Network.isServer)
    {
        GUILayout.Label("Running as a server.");
    }
    else if (Network.isClient)
    {
        GUILayout.Label("Running as a client.");
    }

    if (Network.isClient)
    {
        if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Spawn"))
        {
            SpawnPlayer();
        }
    }
    if (Network.isClient || Network.isServer)
    {
        return;
    }

    if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Start New Server"))
    {
        StartServer();
    }
    if (GUI.Button(new Rect(25f, 65f, 150f, 30f), "Refresh Server List"))
    {
        StartCoroutine("RefreshHostList");
    }
    if (hostData != null)
    {
        for (int i = 0; i < hostData.Length; i++)
        {
            if (GUI.Button(new Rect(Screen.width / 2, 65f + (30f * i), 300f, 30f), hostData[i].gameName))
            {
                Network.Connect(hostData[i]);
            }
        }
    }
}
}

控制脚本:(这是JavaScript)

#pragma strict

function Update () {
    if (Input.GetKey(KeyCode.W))
    {
        transform.position.z += 0.03;
    }

    if (Input.GetKey(KeyCode.S))
    {
        transform.position.z -= 0.03;
    }

    if (Input.GetKey(KeyCode.A))
    {
        transform.position.x -= 0.03;
    }

    if (Input.GetKey(KeyCode.D))
    {
        transform.position.x += 0.03;
    }
}

1 个答案:

答案 0 :(得分:0)

你错过了OnPlayerConnect或类似的东西吗? 我只看到SpawnPlayer(),并且无法看到它从除按钮之外的任何地方调用。

无论如何,您正在使用本地控件生成对象。这意味着如果第二个玩家实际上DID产生了更多的立方体,您将从所有机器控制所有这些立方体。 控制脚本只需要响应,如果发现本地播放器是对象的所有者(因为我怀疑你将脚本附加到所有生成的块)。

我认为团结在网络视图的NetworkView.isMine属性中有这个。将这样的内容添加到控制器脚本中:

function Update () {
if (!networkView.isMine) { return; }
if (Input.GetKey(KeyCode.W))
{
    transform.position.z += 0.03;
}