WWW类过时提示使用 UnityWebRequest
学习笔记
其中包括
加载音频
加载图片
加载AssetBundle
加载文本文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class GetPost : MonoBehaviour
{
private string mMusicURL;
private string mMusicWebURL;
private string mImageURL;
private string mImageWebURL;
private string mAssetBundleURL;
private string mAssetBundleWebURL;
private string mFileURL;
private string mFileWebURL;
void Start()
{
mMusicURL = Application.dataPath + "/少年.ogg";
mMusicWebURL = "http://localhost/少年.ogg";
// mMusicWebURL = "https://mp32.9ku.com/upload/128/2019/05/23/892204.mp3";
mImageURL = Application.dataPath + "/Texture/003.png";
mImageWebURL = "http://localhost/003.png";
mAssetBundleURL = Application.dataPath + "/AB/player.ab";
mAssetBundleWebURL = "http://localhost/AB/player.ab";
mFileURL = Application.dataPath + "mSerializableSerializableAndDeserialize.txt";
mFileWebURL = "http://localhost/readme.txt";
// 测试脚本是否能够加载
mFileURL = Application.dataPath + "/Code/LoadAB.cs";
Debug.Log(mFileWebURL);
// StartCoroutine( loadMusic(mMusicURL));
StartCoroutine( loadMusic(mMusicWebURL));
StartCoroutine(loadTexture(mImageWebURL));
StartCoroutine(loadAssetBundle(mAssetBundleWebURL));
StartCoroutine(loadFile(mFileURL));
}
IEnumerator loadMusic(string URL)
{
UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(URL, AudioType.OGGVORBIS);
AsyncOperation async = request.SendWebRequest();
while (!request.isDone)
{
Debug.Log("Music "+ async.progress);// 下载进度
yield return null;
}
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
}
else
{
AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
AudioSource source = gameObject.AddComponent<AudioSource>();
source.clip = clip;
source.Play();
}
}
IEnumerator loadTexture(string URL)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(URL);
AsyncOperation async = request.SendWebRequest();
while (!request.isDone)
{
Debug.Log("Texture "+ async.progress);
yield return null;
}
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
}
else {
Texture2D texture = DownloadHandlerTexture.GetContent(request);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
SpriteRenderer renderer= gameObject.AddComponent<SpriteRenderer>();
renderer.sprite = sprite;
}
}
IEnumerator loadAssetBundle(string URL)
{
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(URL);
AsyncOperation async= request.SendWebRequest();
while (!request.isDone)
{
Debug.Log("AssetBundle " + async.progress);
yield return null;
}
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
}
else
{
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(request);
UnityEngine.Object cube = assetBundle.LoadAsset("Cube.prefab");
Instantiate(cube);
}
}
IEnumerator loadFile(string URL)
{
UnityWebRequest request = UnityWebRequest.Get(URL);
AsyncOperation async = request.SendWebRequest();
while (!request.isDone)
{
Debug.Log("File " + async.progress);
yield return null;
}
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
}
else
{
Debug.Log(request.downloadHandler.text);
}
}
}