使用Socket实现对话

1、Canvas新建一个Panel,add scroll Rect和mask组件
2、Panel下新建一个Text,Vertical Overflow设置成Overflow,然后add Content Size Fitter这个Component,该Layout的Vertical Fit设置为Perferred Size
3、Panel新建一个ScrollBar
4、Panel的Scroll Rect组件的Content指定Text,Vertical Scrollbar指定上面新建的ScrollBar。

5、Panel下新建的Scroll Rect中Monement Type选择Clamped,这样对话框不会左右移动

    public void OnConn()
    {
        Text msgText = GameObject.Find ("MassageText").GetComponent<Text> ();
        for (int i = 0; i < 100; i++) {
            //i.ToString()添加序号,\n\r换行
            msgText.text += i.ToString() + "Hello\n\r";
        }
    } 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Events : MonoBehaviour {
    private Networks networks;
    void Start()
    {
        networks = GameObject.Find ("Networks").GetComponent<Networks>();
    }
    public void OnConn()
    {
        string str = GameObject.Find ("ConnButton").GetComponentInChildren<Text>().text;
        if (str.Equals ("连接到服务器"))
        {
            if (networks.Connect()) 
            {
                GameObject.Find ("ConnButton").GetComponentInChildren<Text>().text = "断开连接";
            }
        } 
        else 
        {
            networks.DisConnect();
            GameObject.Find ("ConnButton").GetComponentInChildren<Text>().text = "连接到服务器";
        }
    }
    public void OnSend()
    {
        string str = GameObject.Find ("MsgInputField").GetComponentInChildren<InputField> ().text;
        GameObject.Find("MsgInputField").GetComponentInChildren<InputField>().text = "";
        networks.SendMsg (str);
    }
}

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System;
using UnityEngine.UI;
public class Networks : MonoBehaviour {
    Socket socket;
    bool isDisconnect = false;//代表服务器是不是挂了
    List<string> messages = new List<string>();//消息队列
    byte[] buf = new byte[1024];
    //连接服务器
    public bool Connect () {
        try
        {
            IPAddress ipAddress = IPAddress.Parse("192.168.0.102");
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 7777);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(ipEndPoint);
            isDisconnect = false;
            socket.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(receive), null);
        }catch(Exception e)
        {
            if (socket != null)
            {
                socket.Close();
            }
            return false;
        }
        return true;
    }
    //断开连接服务器
    public void DisConnect()
    {
        try {
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }catch(Exception e)
        {
            isDisconnect = true;
        }
    }
    //发送消息
    public void SendMsg(string str)
    {
        socket.Send(Encoding.UTF8.GetBytes(str));
    }
    void receive(IAsyncResult iar)
    {
        try
        {
            int len = socket.EndReceive(iar);
            string str = Encoding.UTF8.GetString(buf, 0, len);
            messages.Add(str);//把得到的消息放到消息队列中
        }catch(Exception e)
        {
            isDisconnect = true;
            return;
        }
        socket.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(receive), null);
    }
    void Update () {
        if (isDisconnect)
        {
            GameObject.Find("ConnButton").GetComponentInChildren<Text>().text = "连接到服务器";
        }
        Text msgText = GameObject.Find("MassageText").GetComponent<Text>();
        if (messages.Count > 0)//如果消息队列中有消息
        {
            string str = messages[0];//拿出第一条消息
            msgText.text += str+"\n\r";//加到文本框中显示
            messages.RemoveAt(0);//把这条消息从队列中移除
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值