您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 MrTang1988 于 2021-9-3 09:46 编辑
简单分装了UnityWebRequest常用方法,接收文件或二进制数据、接收Texture、读取AssetBundle资源,并进行处理、Untiy To Web发送消息,并进行返回数据参数进行处理。仅供参考~~~- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class UnityWebRequestTool : MonoBehaviour {
- public static UnityWebRequestTool Instance;
- public delegate void LoadAessetEndDelegate<T>(T AB);
- private string _assetBundlePath;
- private string _assetBundleName;
- private Dictionary<string, string> WWWFormDictionary;
- // Use this for initialization
- void Awake() {
- Instance = this;
- WWWFormDictionary = new Dictionary<string, string>();
- }
- /// <summary>
- /// Untiy To Web发送消息,并返回数据参数进行处理
- /// </summary>
- /// <param name="form"></param>
- /// <param name="_action"></param>
- public void UnityToWebRequestPost(Dictionary<string, string> form, LoadAessetEndDelegate<string> _action)
- {
- WWWFormDictionary.Clear();
- WWWFormDictionary = form;
- StartCoroutine(UnityToWebPost(_action));
- }
- /// <summary>
- /// 向服务端发送form数据(以需求进行拓展)
- /// </summary>
- /// <returns></returns>
- IEnumerator UnityToWebPost(LoadAessetEndDelegate<string> _action)
- {
- WWWForm form = new WWWForm();
- foreach(string key in WWWFormDictionary.Keys)
- {
- form.AddField(key, WWWFormDictionary[key]);
- }
- //Path 根据需求进行修改
- UnityWebRequest webRequest = UnityWebRequest.Post("http://www.baidu.com", form);
- yield return webRequest.SendWebRequest();
- //异常处理,很多博文用了error!=null这是错误的,请看下文其他属性部分
- if (webRequest.isHttpError || webRequest.isNetworkError)
- Debug.Log(webRequest.error);
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- if (_action != null)
- {
- _action(webRequest.downloadHandler.text);
- }
- }
- }
- /// <summary>
- /// 接收文件或二进制数据
- /// </summary>
- /// <param name="_path"></param>
- /// <param name="_assetName"></param>
- /// <param name="_action"></param>
- public void LoadAsset(string _path, string _assetName ,LoadAessetEndDelegate<DownloadHandler> _byte=null)
- {
- _assetBundlePath = _path;
- _assetBundleName = _assetName;
- StartCoroutine(GetText(_byte));
- }
- /// <summary>
- /// 接收文件或二进制数据
- /// </summary>
- /// <returns></returns>
- IEnumerator GetText(LoadAessetEndDelegate<DownloadHandler> _action)
- {
- UnityWebRequest www = UnityWebRequest.Get(_assetBundlePath+ _assetBundleName);
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.Log(www.error);
- }
- else
- {
- // 接收文本数据,并打印到日志中
- Debug.Log(www.downloadHandler.text);
- DownloadHandler down = www.downloadHandler;
- // 接收二进制数据
- byte[] results = www.downloadHandler.data;
- if (_action != null)
- {
- _action(down);
- }
- }
- }
- /// <summary>
- /// 接收Texture
- /// </summary>
- /// <param name="_path"></param>
- /// <param name="_assetName"></param>
- /// <param name="_texture"></param>
- public void LoadAsset(string _path, string _assetName, LoadAessetEndDelegate<Texture> _texture=null)
- {
- _assetBundlePath = _path;
- _assetBundleName = _assetName;
- StartCoroutine(GetTexture(_texture));
- }
- /// <summary>
- /// 接收Texture
- /// </summary>
- /// <returns></returns>
- IEnumerator GetTexture(LoadAessetEndDelegate<Texture> _action)
- {
- UnityWebRequest www = UnityWebRequestTexture.GetTexture(_assetBundlePath + _assetBundleName);
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.Log(www.error);
- }
- else
- {
- //获取并创建Texture
- //Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
- Texture myTexture = DownloadHandlerTexture.GetContent(www);
- if (_action != null)
- {
- _action(myTexture);
- }
- }
- }
- /// <summary>
- /// 读取AssetBundle资源,并进行处理
- /// </summary>
- /// <param name="_path"></param>
- /// <param name="_assetName"></param>
- /// <param name="_action"></param>
- public void LoadAssetBundle(string _path,string _assetName, AssetType _assetPathType, LoadAessetEndDelegate<AssetBundle> _action=null)
- {
- _assetBundlePath = _path;
- _assetBundleName = _assetName;
- if(_assetPathType== AssetType.Local)
- {
- LoadFromFile(_action);
- }
- else if (_assetPathType == AssetType.Server)
- {
- StartCoroutine(GetAssetBundle(_action));
- }
- }
- /// <summary>
- /// 下载assetbundle资源
- /// </summary>
- /// <returns></returns>
- IEnumerator GetAssetBundle(LoadAessetEndDelegate<AssetBundle> _action)
- {
- UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(_assetBundlePath + _assetBundleName);
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.Log(www.error);
- }
- else
- {
- AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
- if (_action != null)
- {
- _action(bundle);
- }
- }
- }
- /// <summary>
- /// 读取本地资源
- /// </summary>
- /// <param name="path"></param>
- /// <param name="assetName"></param>
- /// <param name="_action"></param>
- private void LoadFromFile( LoadAessetEndDelegate<AssetBundle> _action = null)
- {
- //传入这个AB包存在本地的路径加载本地资源
- AssetBundle ab = AssetBundle.LoadFromFile(_assetBundlePath + _assetBundleName);
- if (_action != null)
- {
- _action(ab);
- }
- }
- }
- public enum AssetType
- {
- Server, Local, Text,Texture,AssetBundle
- }
点击此处复制文本
- //测试类代码:工程中需有相应的测试元素(测试获取服务器资源、本地资源)参考
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class UnityWebRequestToolTest : MonoBehaviour {
- /// <summary>
- /// 测试
- /// </summary>
- public void ClickButton(string _type)
- {
- switch (_type)
- {
- case "Text":
- GetText();
- break;
- case "Texture":
- GetTexture();
- break;
- case "AssetBundle":
- GetAesset();
- break;
- }
- }
- private void GetText()
- {
- UnityWebRequestTool.Instance.LoadAsset("http://localhost:8080/FBX/", "Text.txt", GetTextAesst);
- }
- private void GetTextAesst(DownloadHandler handler)
- {
- Debug.Log(handler.text);
- }
- public Material Material;
- private void GetTexture()
- {
- //UnityWebRequesTool.Instance.LoadAsset("http://localhost:8080/FBX/", "beijing.png", GetTextureAesst);
- UnityWebRequestTool.Instance.LoadAsset(Application.dataPath + "/StreamingAssets/BuildAssetBundle/Window/", "beijing.png", GetTextureAesst);
- }
- private void GetTextureAesst(Texture Texture)
- {
- Material.SetTexture("_MainTex", Texture);
- }
- private void GetAesset()
- {
- // UnityWebRequesTool.Instance.LoadAssetBundle("http://localhost:8080/FBX/", "mainasset",AssetType.Server , GetAessetBundle);
- UnityWebRequestTool.Instance.LoadAssetBundle(Application.dataPath + "/StreamingAssets/BuildAssetBundle/Window/", "mainasset", AssetType.Local, GetAessetBundle);
- }
- private void GetAessetBundle(AssetBundle ab)
- {
- GameObject go = ab.LoadAsset<GameObject>("image.prefab");
- GameObject gos = Instantiate(go, Vector3.zero, Quaternion.identity) as GameObject;
- gos.transform.SetParent(GameObject.Find("Canvas").transform.GetComponent<RectTransform>());
- gos.transform.GetComponent<RectTransform>().localPosition = Vector2.zero;
- gos.transform.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
- ab.Unload(false);
- }
- }
点击此处复制文本
|