Unity3D UDP 服务端例子
2016-03-21 15:08
357 查看
来源:http://forum.unity3d.com/threads/windows-udp-voice-recognition-server.172758/
// ********************************************************* // UDP SPEECH RECOGNITION // ********************************************************* using UnityEngine; using System.Collections; using System; using System.Net; using System.Text; using System.Net.Sockets; using System.Threading; public class UDP_RecoServer : MonoBehaviour { Thread receiveThread; UdpClient client; public int port = 6000; // DEFAULT UDP PORT !!!!! THE QUAKE ONE ;) string strReceiveUDP = ""; string LocalIP = String.Empty; string hostname; public void Start() { Application.runInBackground = true; init(); } // init private void init() { receiveThread = new Thread( new ThreadStart(ReceiveData)); receiveThread.IsBackground = true; receiveThread.Start(); hostname = Dns.GetHostName(); IPAddress[] ips = Dns.GetHostAddresses(hostname); if (ips.Length > 0) { LocalIP = ips[0].ToString(); Debug.Log(" MY IP : "+LocalIP); } } private void ReceiveData() { client = new UdpClient(port); while (true) { try { IPEndPoint anyIP = new IPEndPoint(IPAddress.Broadcast, port); byte[] data = client.Receive(ref anyIP); strReceiveUDP = Encoding.UTF8.GetString(data); // *********************************************************************** // Simple Debug. Must be replaced with SendMessage for example. // *********************************************************************** Debug.Log(strReceiveUDP); // *********************************************************************** } catch (Exception err) { print(err.ToString()); } } } public string UDPGetPacket() { return strReceiveUDP; } void OnDisable() { if ( receiveThread != null) receiveThread.Abort(); client.Close(); } } // *********************************************************
相关文章推荐
- Ubuntu 16.04 LTS 现在可将 Unity 启动器移动到桌面底部
- unity setActive( false )与移出摄像机范围的区别
- [UnityShader2]表面着色器实例(一)
- Unity3D插件开发
- Unity Shaders and Effects Cookbook (1-5)使用2D渐变纹理模拟BRDF(双向反射分布函数)
- java程序员图文并茂细说Unity中调用Android的接口 (转)
- unity导入模型无色
- unityassetbundle 打包存放百度云加载
- [UnityShader2]顶点片段着色器实例(六)
- Unity3D学习笔记(5)—— 牧师和魔鬼游戏改进
- Unity脚本事件执行的顺序
- Unity3D开发基础组件提取总结
- [UnityShader2]顶点片段着色器实例(五)
- Unity AssetBundle爬坑手记
- Unity Shaders and Effects Cookbook (1-4) 创建 Ramp Texture(渐变纹理)控制漫反射着色
- [UnityShader2]顶点片段着色器基础
- 关于Unity3D协程的使用
- 破解Unity3D的步骤-为部分人提供帮助
- 配置Unity脚本开发环境visual studio 2013 tools for unity
- unity c# LINQ查询