u3d做的第一个test:局域网聊天,移动
using UnityEngine; using System.Collections; public class server : MonoBehaviour { //端口號 int port =10000; //聊天信息 string Message = ""; //移動信息 string moveinfo = ""; //滾動視圖位置 Vector2 scrollPosition; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI() { GUI.skin.label.fontSize = 10; switch (Network.peerType) { //服務器未開啟狀態 case NetworkPeerType.Disconnected: StartServer(); break; //成功連接至服務器端 case NetworkPeerType.Server: Onserver(); break; case NetworkPeerType.Client: break; case NetworkPeerType.Connecting: break; } } void StartServer() { if (GUILayout.Button("創建本地服務器")) { NetworkConnectionError error = Network.InitializeServer(10, port, false); Debug.Log("連接狀態" + error); } } void Onserver() { GUILayout.Label("服務器創建完畢,等待客戶端連接....."); //得到客戶端連接的數量 int length = Network.connections.Length; for (int i = 0; i < length; i++) { GUILayout.Label("連接服務器客戶端id" + i); GUILayout.Label("連接服務器客戶端ip" + Network.connections[i].ipAddress); GUILayout.Label("連接服務器客戶端端口號" + Network.connections[i].port); } //斷開服務器 if (GUILayout.Button("斷開服務器")) { Network.Disconnect(); Message = ""; moveinfo = ""; } scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(200), GUILayout.Height(500)); GUILayout.Box(Message); //GUILayout.EndScrollView(); //顯示玩家移動信息 GUILayout.Box(moveinfo); GUILayout.EndScrollView(); } //接受消息 [RPC] void RequestMessage(string message, NetworkMessageInfo info) { Message += "\n" + "發送者" + info.sender + ":" + message; } [RPC] void RequestMove(string message, NetworkMessageInfo info) { moveinfo += "\n" + "移動人" + info.sender + ":執行了" + message + "移動事件"; } }
server 端
client 端
using UnityEngine; using System.Collections; public class client : MonoBehaviour { string IP = "172.16.120.149"; int port = 10000; string inputMessage = "請輸入信息:"; //接受到的信息 string Message = ""; Vector2 scrollPosition; //移動速度 float speed = 50.0f; float rotationSpeed = 100.0f; GameObject cube0 = null; GameObject cube1 = null; // Use this for initialization void Start () { cube0 = GameObject.Find("Cube0"); cube1 = GameObject.Find("Cube1"); } // Update is called once per frame void Update () { } void FixedUpdate() { if (Network.isClient) { float translation = Input.GetAxis("Vertical"); float rotation = Input.GetAxis("Horizontal"); if (translation == 1) networkView.RPC("RequestMove", RPCMode.All, "up"); if (translation == -1) networkView.RPC("RequestMove", RPCMode.All, "down"); if (rotation == -1) networkView.RPC("RequestMove", RPCMode.All, "right"); if (rotation == 1) networkView.RPC("RequestMove", RPCMode.All, "left"); } } void OnGUI() { switch (Network.peerType) { case NetworkPeerType.Disconnected: StartConnect(); break; case NetworkPeerType.Server: break; case NetworkPeerType.Client: OnClient(); break; case NetworkPeerType.Connecting: break; } } void StartConnect() { if (GUILayout.Button("加入遊戲")) { //客戶端開始嘗試連接服務器 NetworkConnectionError error = Network.Connect(IP, port); Debug.Log("連接狀態" + error); } } void OnClient() { //創建一個滾動視圖,用來顯示聊天信息 scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(200), GUILayout.Height(500)); //顯示聊天信息 GUILayout.Box(Message); //創建水平方向視圖 GUILayout.BeginHorizontal(); //編輯輸入內容 inputMessage = GUILayout.TextArea(inputMessage); //發送內容 if (GUILayout.Button("發送消息")) { //使用rpc發送內容 networkView.RPC("RequestMessage", RPCMode.All, inputMessage); } GUILayout.EndHorizontal(); //斷開連接 if (GUILayout.Button("斷開連接")) { Network.Disconnect(); Message = ""; } GUILayout.EndScrollView(); } //接受消息 [RPC] void RequestMessage(string message, NetworkMessageInfo info) { Message += "\n" + "發送者" + info.sender + ":" + message; } //接受模型移動消息 [RPC] void RequestMove(string message, NetworkMessageInfo info) { string sender = info.sender.ToString(); GameObject moveobject = null; //自己的移動事件 if (sender == "-1") { moveobject = cube0; } //其他玩家的移動事件 else { moveobject = cube1; } //根據消息判斷事件類型 int vertical = 0; int horizontal = 0; if (message == "up") { vertical = 1; } if (message == "down") { vertical = -1; } if (message == "left") horizontal = 1; if (message == "right") horizontal = -1; float translation = vertical * speed * Time.deltaTime; float rotation = horizontal * rotationSpeed * Time.deltaTime; moveobject.gameObject.transform.Translate(0, 0, translation); moveobject.gameObject.transform.Rotate(0, rotation, 0); } }
要点:需要给对象添加network view组件
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。