C # network communication and download and save in Unity

Source: Internet
Author: User

 

Development Environment

Window7

Unity3D 3.4.1

VS2010

MB525defy Android 2.2.1

I am a little embarrassed to have two meals with Jie Ge and Wu Ge this week on the 15th blog of Yuhua. Please come back later-0-yang and his family went to the Inner Mongolia to do business and then say goodbye to the code, it's hard to estimate the trajectory of life. When I came to the company at the beginning of the year, the Demo of playing games with him was basically the first time that Yu Hua was engaged in game production, and I learned a lot from it. People are of similar ages, if you talk a lot about it, you can count on your friends. I hope that his business will flourish in the new environment and everything will be safe. Recently I overturned "Parasitic EVE: 3rd birthdays", a rare masterpiece of PSP. It is undeniable that Square is the first-class game screen and plot, and Aya is gone, an "EyesOn Me" with the same name as FF8 OP took away our memories and brought new hopes for the game industry.

This brings C # network communication and download and storage in Unity, which are very useful code. I hope everyone will like =. = I sent the latest tests free of charge. The company's art has always been awesome, and both programs and art are making progress together, the texture test results are-0-and you will be given the opportunity to share your learning experience in materials and Shader.

 

This course:

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 opening the server, it will pause for a certain period of time and start running after an error is reported. The reason is that the multi-thread-CtoS is not enabled in Yuhua. cs

 

Using UnityEngine;

Using System. Collections;

Using System. ComponentModel;

Using System;

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 Connect" + 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 message (the same as sending). The running effect is as follows:


 

2. Download and save the instance locally

Downloading is a constant concern of Yuhua. Because webpage and mobile 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. cs

 

Using 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;

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 --


Coming soon:

Thinking-0-

From: garden of EVERYTHINGForce Your Way --- Yuhua-0-

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.