码迷,mamicode.com
首页 > 移动开发 > 详细

u3d做的第一个test:局域网聊天,移动

时间:2014-08-12 21:22:44      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   strong   for   

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组件

u3d做的第一个test:局域网聊天,移动,布布扣,bubuko.com

u3d做的第一个test:局域网聊天,移动

标签:style   blog   color   使用   os   io   strong   for   

原文地址:http://www.cnblogs.com/zhanying/p/3908259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!