赞
踩
- IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBundle)
- {
- using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path))
- {
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.Success)
- {
- AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
- onGetAssetBundle?.Invoke(ab);
- }
- else
- {
- Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
- onGetAssetBundle?.Invoke(null);
- }
- }
- }
- IEnumerator GetText(string path, UnityAction<string> onGetJson)
- {
- using (UnityWebRequest webRequest = UnityWebRequest.Get(path))
- {
- yield return webRequest.SendWebRequest();
-
- if (webRequest.result == UnityWebRequest.Result.Success)
- {
- string json = webRequest.downloadHandler.text;
- onGetJson?.Invoke(json);
- }
- else
- {
- Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
- onGetJson?.Invoke(null);
- }
- }
- }
- IEnumerator GetTexture2D(string path, UnityAction<Texture2D> onGetTexture2D)
- {
- using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(path))
- {
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.Success)
- {
- Texture2D tex2d = DownloadHandlerTexture.GetContent(webRequest);
- onGetTexture2D?.Invoke(tex2d);
- }
- else
- {
- Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
- onGetTexture2D?.Invoke(null);
- }
- }
- }
- IEnumerator GetAudio(string path, AudioType type, UnityAction<AudioClip> onGetAudio)
- {
- using (UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(path, type))
- {
- yield return webRequest.SendWebRequest();
-
- if (webRequest.result == UnityWebRequest.Result.Success)
- {
- AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
- onGetAudio?.Invoke(clip);
- }
- else
- {
- Debug.Log("error = " + webRequest.error + "\n Load Path = " + path);
- onGetAudio?.Invoke(null);
- }
- }
- }
- IEnumerator GetBuffer(string url, UnityAction<byte[]> act)
- {
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.Success)
- {
- byte[] buffer = webRequest.downloadHandler.data;
- act?.Invoke(buffer);
- }
- else
- {
- Debug.Log("error = " + webRequest.error + "\n Load Path = " + url);
- act?.Invoke(null);
- }
- }
- }
这个事情早期版本要用到MovieTexure之类的,Unity目前版本加载视频实际上和UnityWebRequest没有毛线关系,请使用VideoPlayer,哈哈。
Result枚举的介绍:
Result值 | 内容描述 |
---|---|
InProgress | 请求尚未完成。 |
Success | 请求成功。 |
ConnectionError | 无法与服务器进行通信。例如,请求无法连接或无法建立安全通道。 |
ProtocolError | 服务器返回了一个错误响应。请求成功与服务器通信,但收到连接协议定义的错误。 |
DataProcessingError | 处理数据时出错。请求成功与服务器通信,但在处理接收到的数据时遇到错误。例如,数据损坏或格式不正确。 |
AssetBundle创建参考脚本(这个脚本注意放到Editor文件夹下):
- using System.IO;
- using UnityEditor;
- using UnityEngine;
-
- public class BuildAssetBundle : MonoBehaviour
- {
- static void Package(BuildTarget buildTarget)
- {
- string packagePath = EditorUtility.OpenFolderPanel("Set " + buildTarget + " Save Path", Application.dataPath, "");
- if (packagePath.Length <= 0 || !Directory.Exists(packagePath))
- {
- Debug.Log("Directory Error!");
- return;
- }
- BuildPipeline.BuildAssetBundles(packagePath, BuildAssetBundleOptions.None, buildTarget);
- AssetDatabase.Refresh();
- }
-
- [MenuItem("BuildAssetBundle / WebGL")]
- static void PackageWebGL()
- {
- Package(BuildTarget.WebGL);
- }
-
- [MenuItem("BuildAssetBundle / Window")]
- static void PackageWindow()
- {
- Package(BuildTarget.StandaloneWindows);
- }
-
- [MenuItem("BuildAssetBundle / Android")]
- static void PackageAndroid()
- {
- Package(BuildTarget.Android);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。