当前位置:网站首页>Unity C# 网络学习(八)——WWW
Unity C# 网络学习(八)——WWW
2022-06-26 14:47:00 【帅_shuai_】
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);
});
}
}
边栏推荐
猜你喜欢

Stream常用操作以及原理探索

Common operation and Principle Exploration of stream

Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)

北京银行x华为:网络智能运维夯实数字化转型服务底座

Complimentary Book Cognitive Control: how does our brain accomplish tasks?

TCP congestion control details | 1 summary

Authoritative announcement on the recruitment of teachers in Yan'an University in 2022

【使用yarn运行报错】The engine “node“ is incompatible with this module.

【 Native cloud】 Éditeur ivx Programmable par tout le monde

Where do people get their top energy?
随机推荐
【 Native cloud】 Éditeur ivx Programmable par tout le monde
权威发布 | 延安大学2022年教师岗位招聘公告
Document 1
Numpy基本使用
'coach, I want to play basketball!'—— AI Learning Series booklet for system students
710. random numbers in the blacklist
券商经理给的开户链接办理股票开户安全吗?我想开个户
Stream常用操作以及原理探索
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
qt下多个子控件信号槽绑定方法
Pytoch deep learning code skills
打新债注册开户安全吗,有没有什么风险?
Informatics Olympiad all in one 1400: count the number of words (string matching)
Declaration and assignment of go variables
Sectigo的IP证书申请流程
Talk about the RPA direction planning: stick to simple and valuable things for a long time
Is it safe for flush to register and open an account? Is there any risk?
关于 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error 解决方案(已解决)
15 bs对象.节点名称.节点名称.string 获取嵌套节点内容
SAP 销售数据 实际发货数据导出 销量