Unity2021 version UnityWebRequest usage method reference

Get AssetBundle:

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);
		}
	}
}

Get text:

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);
		}
	}
}

Get pictures:

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);
		}
	}
}

Get audio:

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);
		}
	}
}

Load video:

The early version of this thing needs to use MovieTexure and the like. The loading video of the Unity2021 version has nothing to do with UnityWebRequest. Please use VideoPlayer, haha.

Attached is an introduction to the Result enumeration:

Result value content description
InProgress The request has not been completed.
Success The request was successful.
ConnectionError Unable to communicate with the server. For example, the request could not be connected or the secure channel could not be established.
ProtocolError The server returned an error response. The request communicated successfully with the server, but received an error defined by the connection protocol.
DataProcessingError An error occurred while processing the data. The request successfully communicated with the server, but an error was encountered while processing the received data. For example, the data is corrupted or incorrectly formatted.

Attached is the AssetBundle creation reference script (this script should be placed under the Editor folder):

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);
	}
}

Guess you like

Origin blog.csdn.net/ttod/article/details/124151825