Unity NetWork

using UnityEngine;
using System.Collections;

public class NetworkTest : MonoBehaviour {

    int myPort = 10000;//端口號
    string serverIP = "172.16.19.30";//服務器ip
    string yourState = "";
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    void OnGUI() {
        if (GUILayout.Button("Creat Server")) {
            /*建立一個服務器
             * 3個參數
             * 一、容許的入站鏈接或玩家數量
             * 二、監聽的端口
             * 三、設置穿透功能
             */
            Network.InitializeServer(32, myPort,false);
        }
        if (GUILayout.Button("Connect Server")) {
            /*客戶端鏈接服務器*/
            Network.Connect(serverIP, myPort);
        }
        GUILayout.TextArea(yourState);
        switch (Network.peerType) { 
            case NetworkPeerType.Server:
                yourState = "You are Server, The server has Created!";
                GUILayout.TextField("Had "+Network.connections.Length.ToString()+" Connected");
                break;
            case NetworkPeerType.Client:
                yourState = "You are Client";
                break;
            case NetworkPeerType.Connecting:
                yourState = "Connecting...";
                break;
            case NetworkPeerType.Disconnected:
                yourState = "You had not connected the server";
                break;
        }
    }
}
相關文章
相關標籤/搜索