using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking; public class C_UnityWebRequest : MonoBehaviour
{
static C_UnityWebRequest instance;
public static C_UnityWebRequest Instance
{
get
{
if (instance == null)
{
GameObject mounter = new GameObject("C_UnityWebRequest");
instance = mounter.AddComponent<C_UnityWebRequest>();
}
return instance;
}
} /// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void Get(string url, Action<UnityWebRequest> actionResult)
{
StartCoroutine(_Get(url, actionResult));
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="downloadFilePathAndName"></param>
/// <param name="actionResult"></param>
public void DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
{
StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
} /// <summary>
/// 请求图片
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void GetTexture(string url, Action<Texture2D> actionResult)
{
StartCoroutine(_GetTexture(url, actionResult));
} /// <summary>
/// 请求assetBuddle
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void GetAssetBundle(string url, Action<AssetBundle> actionResult)
{
StartCoroutine(_GetAssetBundle(url, actionResult));
} /// <summary>
/// 下载音效
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <param name="audioType"></param>
public void GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
{
StartCoroutine(_GetAudioClip(url, actionResult, audioType));
} /// <summary>
/// post form
/// </summary>
/// <param name="serverURL"></param>
/// <param name="lstformData"></param>
/// <param name="actionResult"></param>
public void Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
{
//List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
//formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
StartCoroutine(_Post(serverURL, lstformData, actionResult));
} /// <summary>
/// put方法
/// </summary>
/// <param name="url"></param>
/// <param name="contentBytes"></param>
/// <param name="actionResult"></param>
public void UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult)
{
StartCoroutine(_UploadByPut(url, contentBytes, actionResult, ""));
} /// <summary>
/// get
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator _Get(string url, Action<UnityWebRequest> actionResult)
{
using (UnityWebRequest uwr = UnityWebRequest.Get(url))
{
yield return uwr.SendWebRequest();
if (actionResult != null)
{
actionResult(uwr);
}
}
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="downloadFilePathAndName"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator _DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
{
var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
yield return uwr.SendWebRequest();
if (actionResult != null)
{
actionResult(uwr);
}
} /// <summary>
/// 下载图片
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
{
UnityWebRequest uwr = new UnityWebRequest(url);
DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
uwr.downloadHandler = downloadTexture;
yield return uwr.SendWebRequest();
Texture2D t = null;
if (!(uwr.isNetworkError || uwr.isHttpError))
{
t = downloadTexture.texture;
}
if (actionResult != null)
{
actionResult(t);
}
} /// <summary>
/// 下载AssetBuddle
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator _GetAssetBundle(string url, Action<AssetBundle> actionResult)
{
UnityWebRequest www = new UnityWebRequest(url);
DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
www.downloadHandler = handler; yield return www.SendWebRequest(); AssetBundle bundle = null; if (!(www.isNetworkError || www.isHttpError))
{
bundle = handler.assetBundle;
} if (actionResult != null)
{
actionResult(bundle);
}
} /// <summary>
/// 下载音效
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <param name="audioType"></param>
/// <returns></returns>
IEnumerator _GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
{
using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
{
yield return uwr.SendWebRequest(); if (!(uwr.isNetworkError || uwr.isHttpError))
{
if (actionResult != null)
{
actionResult(DownloadHandlerAudioClip.GetContent(uwr));
}
}
}
} /// <summary>
/// 表单下载
/// </summary>
/// <param name="serverURL"></param>
/// <param name="lstformData"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator _Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
{
//List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
//formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
UnityWebRequest uwr = UnityWebRequest.Post(serverURL, lstformData); yield return uwr.SendWebRequest(); if (actionResult != null)
{
actionResult(uwr);
}
} /// <summary>
/// put
/// </summary>
/// <param name="url"></param>
/// <param name="contentBytes"></param>
/// <param name="actionResult"></param>
/// <param name="contentType"></param>
/// <returns></returns>
IEnumerator _UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult, string contentType = "application/octet-stream")
{
UnityWebRequest uwr = new UnityWebRequest();
UploadHandler uploader = new UploadHandlerRaw(contentBytes);
// Sends header: "Content-Type: custom/content-type";
uploader.contentType = contentType;
uwr.uploadHandler = uploader; yield return uwr.SendWebRequest(); bool res = true;
if (uwr.isNetworkError || uwr.isHttpError)
{
res = false;
} if (actionResult != null)
{
actionResult(res);
}
}
}

ZT:https://www.jianshu.com/p/3da9191f82a0

UnityWebRequest_ZT的更多相关文章

随机推荐

  1. supervisor配置kibana

    在/etc/supervisor/conf.d/目录下添加kibana.conf [program:kibana]command=/opt/kibana-6.8.1-linux-x86_64/bin/ ...

  2. Android Studio [ListView]

    ListViewActivity.java package com.xdw.a122.listview; import android.app.Activity; import android.os. ...

  3. 规则引擎 - drools 使用讲解(简单版) - Java

    drools规则引擎 项目链接 现状: 运维同学(各种同学)通过后台管理界面直接配置相关规则,这里是通过输入框.下拉框等完成输入的,非常简单: 规则配置完毕后,前端请求后端,此时服务端根据参数(即规则 ...

  4. 自学React 入门

    刚开始学习React, 读了官网和别人的一些博客,总结了一部分内容,记录一下.有错误欢迎指正... 一.自定义组件需要了解知识 1. 组件分类 React中有两种类型的组件,一种是"方法组件 ...

  5. 使用java的循环单向链表解决约瑟夫问题

    什么是约瑟夫问题 据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定 ...

  6. JDK 1.8 之 Map.merge()

    Map 中ConcurrentHashMap是线程安全的,但不是所有操作都是,例如get()之后再put()就不是了,这时使用merge()确保没有更新会丢失. 因为Map.merge()意味着我们可 ...

  7. 【Java基础】让编码不再让你困惑

    目录 1. ASCII编码 2. Unicode编码 3. UTF-8编码 4. UTF8.UTF16和UTF32之间的区别 5. GBK.GB2312和GB18030之间的区别 6. Java中的编 ...

  8. Vue/React圆环进度条

    数据展示,一直是各行各业乐此不疲的需求,具体到前端开发行业,则是各种各种图表数据展示,各种表格数据展示,烦不胜烦(繁不胜繁)! 前几天刚做了折线图.柱状图.饼状图之类的图表数据展示效果,今天又碰到了类 ...

  9. LeetCode_20-Valid Parentheses

    给定一个字符串,其中包含字符’(’,’)’,’[’,’]’,’{‘,’}’,左括号必须匹配右括号,一对匹配的括号不能单独出现单个左括号或者右括号.如:(()[])有效,[(])无效空字符串也算是有效的 ...

  10. 【JZOJ4807】破解

    Description 历经千辛万苦,ddddddpppppp 终于找到了IBN5100. dp 事先了解到SERN 共有T 个密码,每个密码是一个长度为N 的01 串,他要利用IBN5100 的特殊 ...