Unity3D 封装WWW

using System.Collections;using System.Collections.Generic;using UnityEngine;public class WWWTest { /// /// 下载路径 /// private string url; public string URL {
摘要由CSDN通过智能技术生成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WWWTest {
    /// <summary>
    /// 下载路径
    /// </summary>
    private string url;

    public string URL
    {
        set
        {
            url = value;
        }
        get
        {
            return url; 
        }
    }

    /// <summary>
    /// 开始下载
    /// </summary>
    public virtual void BeginDownLoad()
    {

    }
    /// <summary>
    /// 下载结束
    /// </summary>
    public virtual void FinishDownLoad(WWW tepWWW)
    {

    }
    /// <summary>
    /// 下载出错
    /// </summary>
    public virtual void ErrorDownLoad()
    {

    }

    /// <summary>
    /// 下载协程
    /// </summary>
    /// <returns></returns>
    public IEnumerator DownLoad()
    {
        BeginDownLoad();

        WWW www = new WWW(URL);

        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            //下载成功
            FinishDownLoad(www);
        }
        else
        {
            //下载失败
            ErrorDownLoad();
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WWWDownLoad : WWWTest {
    /// <summary>
    /// 构造方法
    /// </summary>
    /// <param name="url"></param>
    public WWWDownLoad(string url)
    {
        Init(url);
    }
    /// <summary>
    /// 丰富URL
    /// </summary>
    /// <param name="url"></param>
    public void Init(string url)
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            this.URL = "jar:file://" + url;
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            this.URL = "file:///" + url;
        }
        else
        {
            this.URL = "file://" + url;

        }
    }

    public override void FinishDownLoad(WWW tepWWW)
    {
        Debug.Log(tepWWW.text);
    }


}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DownLoading : MonoBehaviour {

    public static DownLoading Instance;
    /// <summary>
    /// 下载队列
    /// </summary>
    Queue<WWWTest> DownLoadQueue;
    /// <summary>
    /// 是否下载完成
    /// </summary>
    private bool IsFinish;

    private void Awake()
    {
        Instance = this;
    }

    void Start () {

        DownLoadQueue = new Queue<WWWTest>();

        IsFinish = true;   
    }
    /// <summary>
    /// 添加下载任务到下载队列中
    /// </summary>
    public void AddDownLoad(WWWTest test)
    {
        DownLoadQueue.Enqueue(test);
        //如果下载完成 开启下载协程
        if (IsFinish)
        {
            IsFinish = false;
            StartCoroutine(DownLoad());
        }
    }

    public IEnumerator DownLoad()
    {
        while(DownLoadQueue.Count > 0)
        {
            WWWTest test = DownLoadQueue.Dequeue();

            yield return test.DownLoad();
        }

        IsFinish = true;
    }

    void Update () {
        //调用
        if (Input.GetKeyDown(KeyCode.A))
        {
            string url = Application.dataPath + "/WWW/WWWTest.cs";
            WWWDownLoad download1 = new WWWDownLoad(url);
            AddDownLoad(download1);
            Debug.Log(123);
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值