IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBundle)
{using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path)){yield return webRequest.SendWebRequest();if (sult == UnityWebRequest.Result.Success){AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;onGetAssetBundle?.Invoke(ab);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){string json = ;onGetJson?.Invoke(json);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){Texture2D tex2d = DownloadHandlerTexture.GetContent(webRequest);onGetTexture2D?.Invoke(tex2d);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);onGetAudio?.Invoke(clip);}else{Debug.Log("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 (sult == UnityWebRequest.Result.Success){byte[] buffer = webRequest.downloadHandler.data;act?.Invoke(buffer);}else{Debug.Log("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);}
}
本文发布于:2024-01-31 01:50:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170663702524469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |