Unity http请求封装(www)

项目中需要经常访问后台,以任何需求都只写一遍代码的想法决定对www进行一下封装,目的即是让程序在每个访问后台的入口保持唯一,同时可以自己定制回调函数,处理每一个访问http请求:

首先看了其他网友的文章,访问www方法如下:

WWW __request = new WWW(__url);
yield return __request;
if(string.IsNullOrEmpty(__request.error)){
   //doing something
}else{
   //doing something
}

首先我希望访问http方法要是单例,但是如果用协程的方法(yield)必须要继承MonoBehaviour,所以先写一个继承MonoBehaviour的单例

public class _InstanceMonoBehaviour : MonoBehaviour where T : MonoBehaviour  
...
然后写一个访问http的单例类

//
//  HttpTool.cs
//  EndlessRunner
//
//  Created by jiabl on 09/06/2017.
//
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void HttpRequestEventHandle(string __request);

/// 
/// Http tool.
/// 当需要请求http的时候调用Request方法
/// 例子:
/// HttpTool.Instance.Request("www.baidu.com",new HttpRequestEventHandle(myfunc))
/// void myfunc(string str) {do something]
/// 
public class HttpTool : _InstanceMonoBehaviour {
	/// 
	/// http请求
	/// 
	/// URL.
	/// Fa.
	/// 
	public void Request(string __url,HttpRequestEventHandle __eventHandle){
		StartCoroutine(httpRequest(__url,__eventHandle));
	}
	/// 
	/// Request the specified __url, __eventHandleForComplete and __eventHandleForError.
	/// 
	/// URL.
	/// Event handle for complete.
	/// Event handle for error.
	public void Request(string __url,HttpRequestEventHandle __eventHandleForComplete,HttpRequestEventHandle __eventHandleForError){
		StartCoroutine(httpRequest(__url,__eventHandleForComplete,__eventHandleForError));
	}


	/// 
	/// Https the request.
	/// 
	/// The request.
	/// URL.
	public IEnumerator httpRequest(string __url,HttpRequestEventHandle __eventHandle){
		WWW __request = new WWW(__url);
		yield return __request;

		if(string.IsNullOrEmpty(__request.error)){
			onHttpRequestCompleted(__request.text,__eventHandle);
		}else{
			Debug.Log("<====== http request error! =======> info: url=>"+__url+" request error: "+__request.error);
		}
	}
	/// 
	/// Https the request.
	/// 
	/// The request.
	/// URL.
	/// Event handle for complete.
	/// Event handle for error.
	public IEnumerator httpRequest(string __url,HttpRequestEventHandle __eventHandleForComplete,HttpRequestEventHandle __eventHandleForError){
		WWW __request = new WWW(__url);
		yield return __request;

		if(string.IsNullOrEmpty(__request.error)){
			onHttpRequestCompleted(__request.text,__eventHandleForComplete);
		}else{
			onHttpRequestFailed(__request.text,__eventHandleForError);
			Debug.Log("<====== http request error! =======> info: url=>"+__url+" request error: "+__request.error);
		}
	}

	/// 
	/// http请求后的回调方法.
	/// 
	/// Request text.
	/// Event handle.
	private void onHttpRequestCompleted(string __requestText,HttpRequestEventHandle __eventHandle){
		__eventHandle(__requestText);
	}
	/// 
	/// http请求失败的回调方法
	/// 
	/// Request text.
	/// Event handle.
	private void onHttpRequestFailed(string __requestText,HttpRequestEventHandle __eventHandle){
		__eventHandle(__requestText);
	}
}


其中用了委托的方法处理回调函数

调用方法如下:

HttpTool.Instance.Request("https://www.baidu.com",new HttpRequestEventHandle(requestEventForPromotion));


//	void requestEventForPromotion(string resquest){
//		Dictionary response = (Dictionary)Json.Deserialize(resquest);
//
//		if (response.ContainsKey("img"))
//		{
//			WWW imgRequest = new WWW((string)response["img"]);
//			// do something...
//		}
//	}



你可能感兴趣的:(Unity开发)