Open a thread to do network connection, and receive data, broadcast with event
usingUnityengine;usingSystem.Collections;usingSystem.Threading;//using System.Threading.Tasks;usingSystem;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Collections.Generic;usingSystem.Linq; Public classalpha{ Public stringv; Publiclist<string> bytelist =Newlist<string>(); Public Static EventAction<string>handler; Public voidBeta () {gamenet gamenet=Newgamenet (); 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 classthread:monobehaviour {Alpha Oalpha=NewAlpha (); Thread Othread; voidStart () {othread=NewThread (NewThreadStart (Oalpha.beta)); Othread.start (); Alpha.handler+ = (s) = print ("Client Handler"); } voidUpdate () {}voidondestory () {othread.abort (); }}
U3d Multithreaded Network