- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
开发环境
Window7
unity3d 3.4.1
VS2010
MB525defy Android 2.2.1
这次带来的是Unity中C#网络通信和下载保存,都是很有实用价值的代码,希望大家喜欢=。= 再免费送上最近做的测试效果图,公司美术一直很给力,程序与美术之间都在共同进步,这次贴图测试效果出来了-0- 以后有机会就给大家分享下材质和Shader方面的学习经验。
本次学习:
1. 简单的C#通信测试
2. 下载和本地保存实例
1. 简单的C#通信测试
首先是与服务器端通信测试,这里羽化和明哥商量的是用C#,因为我们都是Java出生,服务器部分就交给了明哥,羽化这里只是截取了主要实现的小部分代码,所以只能拿来参考,并不能用于实战,明哥最近又在忙什么多线程通信,线程锁之内的高深问题-0- 这是羽化写的客户端实现,只能一次通信,服务器可以多次接受,下面是服务器的截图,在VS2010中运行截图:
代码放在了下面的工程文件中,这里主要学习Unity中的客户端。总共分成3步:连接-发送-接受,简单的3步,这里的“192.168.0.154”是羽化本机的IP地址,大家要测试时不要忘了更改,再者若没打开服务器就运行本工程,就会停顿一定时间,在报错开始运行,原因是羽化没开多线程- -
CtoS.cs
view plaincopy to clipboardprint?
1.using UnityEngine;
2.
using System.Collections;
3.
using System.ComponentModel ;
4.
using System;
5.
using System.Text;
6.
using System.Net;
7.
using System.Net.Sockets;
8.
using System.Threading;
9.
10.
public
class CtoS : MonoBehaviour
11.{
12.
public
string SendM= "-(00)-";
13.
public
string server = "192.168.0.154";
14.
private Socket socket;
15.
public GUIText Test;
16.
17.
void Start ()
18. {
19.
try
20. {
21.
/*Connect*/
22. socket = new Socket ( AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
23. IPEndPoint tempRemoteIP = new IPEndPoint (IPAddress.Parse(server), 11000);
24. EndPoint epTemp = (EndPoint)tempRemoteIP;
25. socket.Connect(epTemp);
26. print("Success Connect");
27.
28.
/*Send*/
29. Byte[] bytesSent = Encoding.ASCII.GetBytes(SendM);
30. socket.Send(bytesSent,bytesSent.Length,0);
31.
32.
/*Received*/
33. Byte[] bytesReceived = new Byte[256];
34.
int bytes = 0;
35.
string page = "Server: Success Connect " + server + ":
";
36.
do
37. {
38. bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
39. page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
40. }
41.
while (bytes > 0);
42. Test.text = page;
43.
44. }
45.
catch (Exception e)
46. {
47. print(e);
48. }
49. }
50.}
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 + ":
"; 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); } }} 服务器打开的话客户端收到返回信息(和发送一样)运行效果如下图:
2. 下载和本地保存实例
下载是羽化一直关心的问题,由于开发的是网页和手机平台,有些资源要得到用户许可下载到本地,这样可以加速游戏运行,所以这里羽化就以一张图片为例,这里做了平台判断,在Android上依然可以保存在SD上,PC端就下载到了工程的download里面。代码如下:
Download.cs
view plaincopy to clipboardprint?
1.using UnityEngine;
2.
using System.Collections;
3.
using System.IO;
4.
public
class Download : MonoBehaviour
5.{
6.
public
string url = "http://ww3.sinaimg.cn/large/80dfe250jw1dle1r2v4t9j.jpg";
7.
public GUIText Test;
8. WWW www;
9. Color Alpha;
10.
bool Appear = false;
11.
int a = 0;
12.
13. IEnumerator Start()
14. {
15. www = new WWW(url);
16.
//定义www为WWW类型并且等于所下载下来的WWW中内容。
17. yield return www;
18.
//返回所下载的www的值
19. renderer.material.shader = Shader.Find("Transparent/Diffuse");
20. renderer.material.mainTexture = www.texture;
21.
22. Texture2D newTexture = www.texture;
23.
byte[] pngData = newTexture.EncodeToPNG();
24.
try
25. {
26.
if (Application.platform == RuntimePlatform.Android)
27. {
28. File.WriteAllBytes(Application.persistentDataPath + "/ICO.png", pngData);
29. }
30.
else
31. {
32. File.WriteAllBytes(Application.dataPath + "/download/ICO.png", pngData);
33. }
34. }
35.
catch(IOException e)
36. {
37. print(e);
38. }
39.
40. Alpha = renderer.material.color;
41. Alpha.a = 0;
42. Appear = true;
43. renderer.enabled = true;
44. renderer.material.color = Alpha;
45.
//将下载下来的WWW中的图片赋予到默认物体的材质上进行渲染出来
46. }
47.
48.
void Update()
49. {
50. Test.text = "DownLoad: " + www.progress;
51.
if(www.progress == 1 && Appear)
52. {
53. a++;
54. Alpha = renderer.material.color;
55. Alpha.a += 0.01F;
56. renderer.material.color = Alpha;
57. }
58.
59.
if(Input.GetKey(KeyCode.Escape))
60. {
61. Application.Quit();
62. }
63. }
64.}
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 www; Color Alpha; bool Appear = false; int a = 0; IEnumerator Start() { www = new WWW(url); //定义www为WWW类型并且等于所下载下来的WWW中内容。 yield return www; //返回所下载的www的值 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; //将下载下来的WWW中的图片赋予到默认物体的材质上进行渲染出来 } 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(); } } } 下载效果图:
由于上面的服务器测试用C#,所以这个羽化也改写成了C#,方便管理,这里需要注意的是,保存的图片是以我们规定的形式输出出去的,羽化把这图片做成了这个模型的贴图,渐变显示出来,这个模型有动作的,羽化也让它播放了出来,图片为网上任意找的一张图片,当然也是羽化喜欢的东西-0- 那可怜的工资带不动的爱啊- -
老样子- -工程地址:
http://dl.dbank.com/c0txqah13k |
|