unity中簡單的C#通訊測試,下載和本地儲存執行個體

來源:互聯網
上載者:User

 

1. 簡單的C#通訊測試

2. 下載和本地儲存執行個體

1.  簡單的C#通訊測試

        首先是與伺服器端通訊測試,這裡羽化和明哥商量的是用C#,因為我們都是Java出生,伺服器部分就交給了明哥,羽化這裡只是截取了主要實現的小部分代碼,所以只能拿來參考,並不能用於實戰,明哥最近又在忙什麼多線程通訊,線程鎖之內的高深問題-0- 這是羽化寫的用戶端實現,只能一次通訊,伺服器可以多次接受,下面是伺服器的,在VS2010中運行:

 

        代碼放在了下面的工程檔案中,這裡主要學習Unity中的用戶端。總共分成3步:串連-發送-接受,簡單的3步,這裡的“192.168.0.154”是羽化原生IP地址,大家要測試時不要忘了更改,再者若沒開啟伺服器就運行本工程,就會停頓一定時間,在報錯開始運行,原因是羽化沒開多線程- -

CtoS.csusing 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);}}}    伺服器開啟的話用戶端收到返回資訊(和發送一樣)運行效果如:2.  下載和本地儲存執行個體        下載是羽化一直關心的問題,由於開發的是網頁和手機平台,有些資源要得到使用者許可下載到本地,這樣可以加速遊戲運行,所以這裡羽化就以一張圖片為例,這裡做了平台判斷,在Android上依然可以儲存在SD上,PC端就下載到了工程的download裡面。代碼如下: 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); //定義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

        最後送上一張我們做的測試,Unity內建的Shader和高光貼圖,雖然沒有法線,效果還不錯,消耗不是很大,還有很大上升空間~ ~ 手機上效果略次,微弱顯示高光效果,有待改善- -

轉自:http://blog.csdn.net/libeifs/article/details/6902097

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.