Simple C # communication test in unity, download and save the instance locally

Source: Internet
Author: User

 

1. Simple C # communication test

2. Download and save the instance locally

1. Simple C # communication test

First, we need to test the communication with the server. Here, Yu Hua and Ming Ge discuss how to use C #, because we were born in Java, and the server was handed over to Ming ge, yu Hua only intercepts a small part of the main implementation code, so it can only be used for reference and cannot be used in practice. Ming Ge is busy with multi-thread communication recently, deep problems within the thread lock-0-this is the implementation of the feather write client, only one communication, the server can be accepted multiple times, the following is the server, run in vs2010:

 

The code is stored in the following project file. Here we mainly learn about the client in unity. There are three steps in total: Connection-send-accept, simple three steps. Here, "192.168.0.154" is the IP address of the Local Machine. Do not forget to change it during testing, in addition, if the project is run without the server being opened, it will pause for a certain period of time and start running after an error is reported, because the multi-thread is not enabled in Yuhua --

Ctos. csusing unityengine; using system. collections; using system. componentmodel; using system. text; using system. net; using system. net. sockets; using system. threading; public class ctos: monobehaviour {Public String sendm = "-(00)-"; Public String Server = "192.168.0.154"; private Socket socket; Public guitext test; void start () {try {/* connect */socket = new socket (addressfamily. internetwork, Sockettype. stream, protocoltype. TCP); ipendpoint tempremoteip = new ipendpoint (IPaddress. parse (server), 11000); endpoint eptemp = (endpoint) tempremoteip; socket. connect (eptemp); print ("Success connect");/* Send */byte [] bytessent = encoding. ASCII. getbytes (sendm); socket. send (bytessent, bytessent. length, 0);/* received */byte [] bytesreceived = new byte [256]; int bytes = 0; string page = "server: Success Co Nnect "+ SERVER +": \ r \ n "; do {bytes = socket. receive (bytesreceived, bytesreceived. length, 0); page = page + encoding. ASCII. getstring (bytesreceived, 0, bytes);} while (Bytes> 0); test. TEXT = page;} catch (exception e) {print (e) ;}} when the server is opened, the client receives the returned information (the same as the message sent). The running effect is as follows: 2. downloading and local storage instance downloading are a matter of constant concern. Because webpage and mobile phone platforms are developed, some resources need to be downloaded to the local device with the user's permission, which can accelerate the game operation, so here we will take an image as an example. Here we make a platform judgment, which can still be saved on SD on Android, and PC end will be downloaded to the download of the project. The Code is as follows: download. csusing unityengine; using system. collections; using system. io; public class download: monobehaviour {Public String url = "http://ww3.sinaimg.cn/large/80dfe250jw1dle1r2v4t9j.jpg"; Public guitext test; www WWW; color Alpha; bool appear = false; int A = 0; ienumerator start () {WWW = new WWW (URL); // defines WWW as the WWW type and is equal to the content in the downloaded www. Yield return WWW; // return the downloaded WWW value Renderer. material. shader = shader. find ("transparent/diffuse"); Renderer. material. maintexture = www. texture; texture2d newtexture = www. texture; byte [] pngdata = newtexture. encodetopng (); try {If (application. platform = runtimeplatform. android) {file. writeallbytes (application. persistentdatapath + "/ico.png", pngdata);} else {file. writeallbytes (application. datapath + "/download/ico.png", pngdata) ;}} catch (ioexception e) {print (e) ;}alpha = Renderer. material. color; Alpha. a = 0; appear = true; Renderer. enabled = true; Renderer. material. color = Alpha; // assign the downloaded WWW image to the default object material for rendering} void Update () {test. TEXT = "Download:" + www. progress; If (www. progress = 1 & appear) {A ++; alpha = Renderer. material. color; Alpha. A + = 0.01f; Renderer. material. color = Alpha;} If (input. getkey (keycode. escape) {application. quit ();}}}

 

Download:

Because the above server test uses C #, this feather is also rewritten to C # For convenient management. Here, we need to note that the saved images are output in the form we specified, yu Hua made the image into a texture of this model and showed it in a gradient. This model has an action, and Yu Hua also made it play it out. The picture is an image randomly found on the Internet, of course, it's also something that Yu Hua liked-0-That poor wages don't move love --

Old--Project address:

Http://dl.dbank.com/c0txqah13k

Finally, we will upload a test with the built-in shader and highlight textures of unity. Although there is no normal, the effect is good, the consumption is not very large, and there is a lot of room for improvement ~ ~ The results on the mobile phone are omitted, and the highlight effect is slightly displayed, which needs to be improved --

From: http://blog.csdn.net/libeifs/article/details/6902097

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.