首先我们需要一个websocket服务器,之前的博文中有做
Tomcat架设简单Websocket服务器
用的时候打开就行了,先不管它
Unity中新建场景
建UI(UGUI)
有一个连接按钮Button
一个信息输入框InputField
一个发送按钮Button
一个断开按钮Button
一个消息显示框Text
场景中建一个GameObject,在上面加个脚本,就叫WSMgr好了
用到了BestHTTP这个插件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BestHTTP;
using BestHTTP.WebSocket;
using System;
using BestHTTP.Examples;
using UnityEngine.UI;
using System.Text;
public class WSMgr : MonoBehaviour {
//public string url = "ws://localhost:8080/web1/websocket";
public string url = "ws://localhost:8080/web1/ws";
public InputField msg;