First, take a look at the Unity interface:
Set up 2 cameras, position overlap, rotate the same, parent-child relationship, add script A.cs to the parent camera, and copy the sub-camera to the variable cam in the a script;
Cam is used to provide the screen for the Rendertexture, port is the socket listening ports;
Second, the A.cs script code (night too deep, another day to add comments, direct sticker code)
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;usingUnityengine; Public classa:monobehaviour{ PublicCamera Cam; Public intPort =10002; Rendertexture Cameraview=NULL; Socket Socket=NULL; Thread Thread=NULL; BOOLSuccess =true; /// <summary> ///Client List/// </summary>dictionary<string, client> clients =Newdictionary<string, client>(); Vector3 old_position;//Old LocationQuaternion old_rotation;//Old Rotation voidStart () {Cameraview=NewRendertexture (Screen.width, Screen.height, -); Cameraview.enablerandomwrite=true; Cam.targettexture=Cameraview; Old_position=transform.position; Old_rotation=transform.rotation; Socket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket. Bind (NewIPEndPoint (Ipaddress.parse ("127.0.0.1"), port); Socket. Listen ( -); Thread=NewThread (NewThreadStart (Accept)); Thread. Start (); } intIsnewadd =0; voidAccept () { while(Thread. ThreadState = =threadstate.running) {Socket _socket=socket. Accept (); if(clients. ContainsKey (_socket. Remoteendpoint.tostring ())) {Try{clients[_socket. Remoteendpoint.tostring ()].socket. Shutdown (Socketshutdown.both); } Catch{} clients. Remove (_socket. Remoteendpoint.tostring ()); } Client Client=NewClient {Socket=_socket}; Clients. ADD (_socket. Remoteendpoint.tostring (), client); Isnewadd=1; Debug.logerror ("Connect ..."); } } voidUpdate () {if(Success && clients. Count >0) {Success=false; Sendtexture (); } if(Isnewadd >0) {Isnewadd=0; Sendtexture (1); } } voidOngui () {GUI. Drawtexture (NewRect (Ten,Ten, -,135), Cameraview, Scalemode.stretchtofill); } voidOnapplicationquit () {Try{socket. Shutdown (Socketshutdown.both); } Catch { } Try{thread. Abort (); } Catch { } }Texture2d screenshot=NULL; intGc_count =0; voidSendtexture (intIsinit =0) { if(!old_position. Equals (transform.position) | | !old_rotation. Equals (transform.rotation)) | | Isinit = =1) { if(NULL==screenshot) screenshot=NewTexture2d (Screen.width, Screen.height, Textureformat.rgb24,false); Rendertexture.active=Cameraview; Screenshot.readpixels (NewRect (0,0, Cameraview.width, Cameraview.height),0,0); Rendertexture.active=NULL; byte[] bytes = Screenshot.encodetojpg ( -); foreach(varValinchclients. Values) {Try{val.socket.Send (bytes); } Catch { if(!val.socket.Connected) clients. Remove (Val.socket.RemoteEndPoint.ToString ()); Break; }} Gc_count++; if(Gc_count > the) {Gc_count=0; Gc. Collect (2); } Debug.Log ("Send data:"+ (float) bytes. length/1024f +"KB"); Old_position=transform.position; Old_rotation=transform.rotation; } Success=true; }}classclient{ PublicSocket socket =NULL;}
Third, WinForm end
Drag a PictureBox directly onto the window to dock the parent form;
Iv. WinForm Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacetransformviewclient{ Public Partial classForm1:form {BOOLIsover =false; Socket Socket=NULL; Thread Thread=NULL; byte[] buffer =NULL; BOOLRecestate =true; intReadtimes =0; PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) {Control.checkforillegalcrossthreadcalls=true; Buffer=New byte[1024x768*1024x768*Ten]; Socket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Socket. Connect (Ipaddress.parse ("127.0.0.1"),10002); Thread=NewThread (NewThreadStart (Receive)); Thread. Start (); } voidReceive () { while(Thread. ThreadState = = Threadstate.running &&socket. Connected) {intCount =socket. Receive (buffer); if(Recestate && Count >0) {recestate=false; Bytestoimage (count, buffer); }}} MemoryStream ms=NULL; Public voidBytestoimage (intCountbyte[] bytes) { Try{ms=NewMemoryStream (Bytes,0, Count); pictureBox1.Image=Image.fromstream (MS); Readtimes++; if(Readtimes > the) {Readtimes=0; Gc. Collect (2); } } Catch{} recestate=true; } Private voidForm1_formclosing (Objectsender, FormClosingEventArgs e) { Try{socket. Shutdown (Socketshutdown.both); } Catch { } Try{thread. Abort (); } Catch { } } }}
There may be many bugs and flaws in the program, I hope the expert guidance!
Unity sends game screen to Winform