Unity C# 网络学习(八)——WWW
一.WWW类
www主要支持的协议
1.http://和https:// 超文本传输协议
2.ftp:// 文本传输协议(但仅限于匿名下载)
3.file://本地文件传输协议
1.www类的常用方法和变量
//创建www请求
WWW www = new WWW("http://192.168.1.103:8080/Http_Server/123.pptx");
//从下载数据返回一个音效切片AudioClip对象
AudioClip cp = www.GetAudioClip();
//用下载数据中的图像来替换现有的一个Texture2D对象
Texture2D tex = new Texture2D(100, 100);
www.LoadImageIntoTexture(tex);
//从缓存加载AB包对象,如果该包不在缓存则自动下载存储到缓存中,以便直接从缓存中加载
WWW.LoadFromCacheOrDownload("http://192.168.1.103:8080/Http_Server/123.pptx", 1);
//如果加载的数据是AB包,可以通过该变量直接获取加载结果
var ab = www.assetBundle;
//如果加载的是音频切片文件,可以通过该变量直接获取加载结果
var cp1 = www.GetAudioClip();
//以字节数组的形式获取加载到的内容
var bytes = www.bytes;
//获取过去已下载的字节数
var num = www.bytesDownloaded;
//返回一个错误信息,如果下载期间出现错误,可以通过获取错误信息
var error = www.error;
//判断下载是否完成
var done = www.isDone;
//获取下载进度
var poss = www.progress;
//如果资源是字符串形式
var str = www.text;
//如果资源是图片形式
var texture = www.texture;
2.利用www类来异步加载和下载文件
(1)下载HTTP服务器上的内容
private IEnumerator DownLoadHttp()
{
WWW www = new WWW("http://192.168.1.103:8080/Http_Server/xxx.jpg");
while (!www.isDone)
{
Debug.Log("进度:" + www.progress);
Debug.Log("已经下载大小:" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage1.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
(2)下载FTP服务器上的内容
- 注意:www对于FTP服务器只能匿名访问,需要在FTP服务器上创建匿名的用户(Anonymous)
private IEnumerator DownLoadFtp()
{
WWW www = new WWW("ftp://192.168.1.103/1.jpg");
while (!www.isDone)
{
Debug.Log("进度:" + www.progress);
Debug.Log("已经下载大小:" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage2.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
(3)加载本地内容
private IEnumerator LoadLocalFile()
{
string path = "file://" + Application.streamingAssetsPath + "/test.jpg";
WWW www = new WWW(path);
while (!www.isDone)
{
Debug.Log("进度:" + www.progress);
Debug.Log("已经下载大小:" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage3.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
二.封装WWWMgr管理类
1.WWWMgr
public class WWWMgr : MonoBehaviour
{
private static WWWMgr instance;
public static WWWMgr Instance => instance;
private void Awake()
{
instance = this;
}
public void LoadRes<T>(string path, Action<T> action) where T : class
{
StartCoroutine(LoadResAsync(path, action));
}
private IEnumerator LoadResAsync<T>(string path, Action<T> action) where T : class
{
WWW www = new WWW(path);
while (!www.isDone)
{
Debug.Log("下载进度:" + www.progress);
Debug.Log("已经加载大小:" + www.bytesDownloaded);
yield return null;
}
if (www.error != null)
{
action?.Invoke(null);
yield break;
}
if (typeof(T) == typeof(AssetBundle))
action?.Invoke(www.assetBundle as T);
else if (typeof(T) == typeof(AudioClip))
action?.Invoke(www.GetAudioClip() as T);
else if(typeof(T) == typeof(Texture2D))
action?.Invoke(www.texture as T);
else if(typeof(T) == typeof(string))
action?.Invoke(www.text as T);
else
action?.Invoke(www.bytes as T);
}
}
2.测试
public class Lesson24 : MonoBehaviour
{
[SerializeField] private AudioSource audioSource;
private void Start()
{
if (WWWMgr.Instance == null)
{
GameObject wwwMgrObj = new GameObject("WWWMgr");
wwwMgrObj.AddComponent<WWWMgr>();
}
WWWMgr.Instance.LoadRes<AudioClip>("http://192.168.1.103:8080/Http_Server/music.mp3", clip =>
{
audioSource.clip = clip;
audioSource.Play();
});
WWWMgr.Instance.LoadRes("ftp://192.168.1.103/程序使用说明.txt",(string text) =>
{
Debug.Log(text);
});
}
}