1. 程式人生 > >關於unity中Network元件的簡單應用

關於unity中Network元件的簡單應用

以下資訊來自麥子學院

這是最終實現的效果 


Network元件的基本原理及相關知識


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetWork : MonoBehaviour {
    int port = 10010;
    string message = "";
    Vector2 sc;

    void OnGUI() { 
        switch(Network.peerType){
            case NetworkPeerType.Disconnected:
                StartSeever();
                break;
            case NetworkPeerType.Server:
                OnServer();
                break;
            case NetworkPeerType.Client:

                break;
            case NetworkPeerType.Connecting:

                break;
              
        }
    }
    void StartSeever() {
        if (GUILayout.Button("建立伺服器")) {
            var error = Network.InitializeServer(12, port, false);
            //初始化伺服器 返回一個建立錯誤的狀態
            switch(error){
                case NetworkConnectionError.NoError:
                    break;
                default:
                    Debug.LogError("啟動伺服器失敗");
                    break;
            }


        }
    }
    void OnServer(){
        GUILayout.Label("伺服器已經執行,等待客戶端連線");
        foreach (var temp in Network.connections) {
            GUILayout.Label("客戶端"+"IP"+temp.ipAddress+"Potr"+temp.port);
            GUILayout.Label("-----------------");
        }
        //建立scrollview
        sc=GUILayout.BeginScrollView(sc,GUILayout.Width(280),GUILayout.Height(350));
        //繪製紋理顯示內容
        GUILayout.Box(message);
        //結束滾動檢視
        GUILayout.EndScrollView();
    }
    [RPC]
    void RecvMessage(string str,NetworkMessageInfo info){
        message = "傳送端" + info.sender + "訊息" + str;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetWorkClient : MonoBehaviour {

    int port = 10010;
    string message = "";
    Vector2 sc;

    void OnGUI()
    {
        switch (Network.peerType)
        {
            case NetworkPeerType.Disconnected:
                StartConnection();
                break;
            case NetworkPeerType.Server:
                break;
            case NetworkPeerType.Client:
                OnClient();
                break;
            case NetworkPeerType.Connecting:

                break;

        }
    }
    void StartConnection()
    {
        if (GUILayout.Button("連線伺服器"))
        {
            var error = Network.Connect("127.0.0.1",port);
            //連線伺服器
            switch (error)
            {
                case NetworkConnectionError.NoError:
                    break;
                default:
                    Debug.LogError("客戶端錯誤"+error);
                    break;
            }


        }
    }
    void OnClient()
    {
        //建立scrollview
        sc = GUILayout.BeginScrollView(sc, GUILayout.Width(280), GUILayout.Height(350));
        //繪製紋理顯示內容
        GUILayout.Box(message);
        //文字框
        message = GUILayout.TextArea(message);
        if(GUILayout.Button("傳送")){
            GetComponent<NetworkView>().RPC("RecvMessage",RPCMode.All,message);
            //第一個引數發給的物件名稱 第二個是給所有人發 
        }
        //結束滾動檢視GUILayout.Label("伺服器已經執行,等待客戶端連線");
        GUILayout.EndScrollView();
    }
    [RPC]
    void RecvMessage(string str, NetworkMessageInfo info)
    {
        message = "傳送端" + info.sender + "訊息" + str;
    }
}