首页 > 代码库 > u3d 多线程 网络
u3d 多线程 网络
开启一个线程做网络连接,和接收数据, 用event进行广播
using UnityEngine;using System.Collections;using System.Threading;//using System.Threading.Tasks;using System;using System.Net;using System.Net.Sockets;using System.Collections.Generic;using System.Linq;public class Alpha{ public string v; public List<string> bytelist = new List<string>(); public static event Action<String> handler; public void Beta() { GameNet gamenet = new GameNet(); gamenet.start(); gamenet.send("1"); while(true){ try{ v = gamenet.conrecv(); bytelist.Add(v); gamenet.send ("1"); } catch(SocketException e){ v = "socket closed"; break; } if(handler != null){ handler(bytelist[0]); } bytelist.Clear(); } }};public class thread : MonoBehaviour { Alpha oAlpha = new Alpha(); Thread oThread; void Start () { oThread = new Thread(new ThreadStart(oAlpha.Beta)); oThread.Start(); Alpha.handler += (s) => print ("client handler"); } void Update () { } void OnDestory(){ oThread.Abort(); }}
u3d 多线程 网络
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。