Unity Server不通过客户端执行命令

时间:2015-06-15 15:57:40

标签: c# unity3d network-programming unity-networking

我正在开发一个非常简单/基本的客户端 - 服务器网络程序,这是我希望创建的未来游戏的基础。现在,客户端程序/项目已成功连接到服务器程序/项目' (两者完全分开)。但是,当客户端向服务器发送请求/命令时。好吧,它不会将命令发送到服务器,它会在客户端上执行它。

我读过有关更改“Monodevelop”的信息。继承到' NetworkBehaviour'所以我做了,它导致了错误;

Command xxx was sent to the server
...Debug.logerror(object

所以我似乎已经到了一半,据称它将命令发送到服务器,但服务器没有做任何事情。我不知道为什么。

以下是我为我的服务器和客户端提供的相对较短的程序,并且几乎相同,但如果有人可以帮助我,我会非常感激,因为我的大学课程没有&#39我真的很喜欢网络编程;

服务器代码

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class server : MonoBehaviour
{

    public string IP = "127.0.0.1";
    public int port = 25001;
    public int prevConns = 0;
    private int currConns = 0;

    public virtual void OnServerReady (NetworkConnection conn)
    {
        NetworkServer.SetClientReady (conn);
    }

    void OnGUI ()
    {
        if (Network.peerType == NetworkPeerType.Disconnected) {
            if (GUI.Button (new Rect (100, 100, 100, 25), "Start Client")) {
                Network.Connect (IP, port);
            }
            if (GUI.Button (new Rect (100, 125, 100, 25), "Start Server")) {
                Network.InitializeServer (20, port, false);
            }
        } else {
            //CLIENT - NOT NEEDED
            if (Network.peerType == NetworkPeerType.Client) {
                GUI.Label (new Rect (100, 100, 100, 25), "Client");
                if (GUI.Button (new Rect (100, 125, 100, 25), "Disconnect")) {
                    Network.Disconnect (250);
                }
            }
            if (Network.peerType == NetworkPeerType.Server) {               
                currConns = Network.connections.Length;
                GUI.Label (new Rect (100, 100, 100, 25), "Server");
                GUI.Label (new Rect (100, 125, 100, 25), "Connections: " + currConns);

                if (prevConns != currConns) {
                    if (prevConns < currConns) {
                        Debug.Log ("NEW CONNECTION");
                        prevConns++;

                    } else if (prevConns > currConns) {
                        Debug.Log ("Lost CONNECTION");
                        prevConns--;
                    }
                }

                if (GUI.Button (new Rect (100, 15, 100, 25), "Shutdown")) {
                    Network.Disconnect (250);
                }
            }
        }
    }

}

客户代码

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class connection : MonoBehaviour
{

    public string IP = "127.0.0.1";
    public int port = 25001;
    public GameObject player;

    public virtual void OnClientConnect (NetworkConnection conn)
    {
        ClientScene.Ready (conn);
    }

    void OnGUI ()
    {
        if (Network.peerType == NetworkPeerType.Disconnected) {
            if (GUI.Button (new Rect (100, 100, 100, 25), "Start Client")) {
                Network.Connect (IP, port);
            }
        } else {
            if (Network.peerType == NetworkPeerType.Client) {
                GUI.Label (new Rect (100, 100, 100, 25), "Client");
                if (GUI.Button (new Rect (100, 125, 100, 25), "Disconnect")) {
                    Network.Disconnect (250);
                }

                if (GUI.Button (new Rect (100, 150, 100, 25), "Send Cmd")) {
                    CmdSendCommand ();
                }

            }
        }
    }

    [Command]
    void CmdSendCommand ()
    {
        NetworkBehaviour.print ("HELLO");
    }
}

0 个答案:

没有答案