http协议下载资源及访问url(登录注册);

using UnityEngine;
using System.Collections;
/// <summary>
/// using UnityEngine;
using System.Collections;
/// <summary>
/// http协议下载资源及访问url(登录注册);
/// </summary>
public class HttpDownLoad : MonoBehaviour {

    /// <summary>
    ///委托   下载之后的回调
    /// </summary>
    /// <param name="bytes"></param>
    public delegate void AccessURLCallBack(string url ,byte[] bytes);
    /// <summary>
    /// 单例对象
    /// </summary>
    private static HttpDownLoad _instance;

    public static HttpDownLoad GetInstance()
    {
        if (_instance==null)
        {
            //想办法创建
            GameObject gb = new GameObject();
            //创建具体的单例
           _instance= gb.AddComponent<HttpDownLoad>();
        }
        return _instance;
    }

    void Start() {
        _instance = this;
    }

    // Update is called once per frame
    void Update() {

    }
    /// <summary>
    /// 启动协程
    /// </summary>
    public void StartCoroutineAccessURL(string url,AccessURLCallBack callBack)
    {
        StartCoroutine(AccessURL(url,callBack));
    }
    /// <summary>
    /// 访问URL协程
    /// </summary>
    /// <param name="URL"></param>
    /// <returns></returns>
    IEnumerator AccessURL(string URL,AccessURLCallBack callBack)
    {
        WWW www = new WWW(URL);
        yield return www;
        if (www.error == null|| www.error=="")
        {
            //没有错误
            Debug.Log("www.text=" + www.text);//文本传输协议—>2进制0  1
            callBack(URL,www.bytes);
        }
        else
        {
            Debug.Log("访问URL错误,错误是:"+www.error+",url="+URL);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42661974/article/details/84592307