1 /* HttpRequestTest.cs 2 * 采用C#的WebClinet类下载数据,多线程,但需要在主线程里转成texture。 3 */ 4 using System; 5 using System.Text; 6 using System.Net; 7 using UnityEngine; 8 9 public enum RequestType 10 { 11 Text, 12 Image 13 } 14 15 public interface IHttpRequest 16 { 17 void CaptureRequestData(int requestTag, string requestData); 18 void CaptureRequestTexture(int requestTag, byte[] texture); 19 void LostRequestData(int requestTag, string requestData); 20 } 21 22 public class HttpRequestInfo 23 { 24 public IHttpRequest httpRequest; 25 public RequestType requestType; 26 public int requestTag; 27 28 public HttpRequestInfo (IHttpRequest httpRequest, RequestType requestType, int requestTag) 29 { 30 this.httpRequest = httpRequest; 31 this.requestType = requestType; 32 this.requestTag = requestTag; 33 } 34 } 35 36 public class HttpRequestTest : MonoBehaviour 37 { 38 public static HttpRequestTest inst = null; 39 40 void Awake() 41 { 42 inst = this; 43 } 44 45 public void DownloadData(IHttpRequest httpRequest, RequestType requestType, string httpUrl, int requestTag) 46 { 47 WebClient webClient = new WebClient(); 48 webClient.DownloadDataCompleted += DownloadDataCompletedCallback; 49 webClient.DownloadDataAsync(new Uri(httpUrl), new HttpRequestInfo(httpRequest, requestType, requestTag)); 50 } 51 52 private void DownloadDataCompletedCallback(object sender, DownloadDataCompletedEventArgs args) 53 { 54 HttpRequestInfo httpRequestInfo = args.UserState as HttpRequestInfo; 55 if(!args.Cancelled && args.Error == null) 56 { 57 if(httpRequestInfo.requestType == RequestType.Text) 58 { 59 httpRequestInfo.httpRequest.CaptureRequestData(httpRequestInfo.requestTag, Encoding.UTF8.GetString(args.Result)); 60 } 61 else if(httpRequestInfo.requestType == RequestType.Image) 62 { 63 httpRequestInfo.httpRequest.CaptureRequestTexture(httpRequestInfo.requestTag, args.Result); 64 } 65 } 66 else 67 { 68 string requestError = args.Error.Message; 69 httpRequestInfo.httpRequest.LostRequestData(httpRequestInfo.requestTag, requestError); 70 } 71 } 72 73 } 74 75 76 77 /* HttpRequestTestNew.cs 78 * 方便直接,但不是多线程,因此能得到texture。 79 */ 80 using UnityEngine; 81 using System.Collections; 82 83 public enum RequestType 84 { 85 Text, 86 Image 87 } 88 89 public interface IHttpRequest 90 { 91 void CaptureRequestData(int requestTag, string requestData); 92 void CaptureRequestTexture(int requestTag, Texture texture); 93 void LostRequestData(int requestTag, string requestData); 94 } 95 96 public class HttpRequestTestNew : MonoBehaviour 97 { 98 public static HttpRequestTestNew inst = null; 99 100 void Awake() 101 { 102 inst = this; 103 } 104 105 public void DownloadData(IHttpRequest httpRequest, RequestType requestType, string httpUrl, int requestTag) 106 { 107 WWW www = new WWW(httpUrl); 108 StartCoroutine(DownloadCoroutine(www, httpRequest, requestType, requestTag)); 109 } 110 111 private IEnumerator DownloadCoroutine(WWW www, IHttpRequest httpRequest, RequestType requestType, int requestTag) 112 { 113 yield return www; 114 if(!string.IsNullOrEmpty(www.error)) 115 { 116 Debug.LogError("www is error!"); 117 httpRequest.LostRequestData(requestTag, www.error); 118 } 119 else 120 { 121 switch(requestType) 122 { 123 case RequestType.Text: 124 httpRequest.CaptureRequestData(requestTag, www.text); 125 break; 126 case RequestType.Image: 127 httpRequest.CaptureRequestTexture(requestTag, www.texture); 128 break; 129 } 130 } 131 if(www.isDone) 132 www.Dispose(); 133 } 134 }
原文:http://www.cnblogs.com/leng-yuye/p/3726283.html