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);//把这条消息从队列中移除
}
}
}