当前位置:网站首页>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);
});
}
}
边栏推荐
- 【 Native cloud】 Éditeur ivx Programmable par tout le monde
- The DOTPLOT function in the epidisplay package of R language visualizes the frequency of data points in different intervals in the form of point graphs, specifies the grouping parameters with the by p
- R语言epiDisplay包的tableStack函数制作统计汇总表格(分组的描述性统计、假设检验等)、不设置by参数计算基础描述性统计信息、指定对于大多数样本负相关的变量进行反序
- Summary of decimal point of amount and price at work and pit
- R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、使用aggregate.data.frame函数计算分组汇总统计信息
- The JVM outputs GC logs, causing the JVM to get stuck. I am stupid
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- Redis事务与watch指令
- Keil4打开单片机工程一片空白,cpu100%程序卡死的问题解决
- 杜老师说网站更新图解
猜你喜欢

NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...

qt下多个子控件信号槽绑定方法

Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation

关于 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error 解决方案(已解决)

15 bs对象.节点名称.节点名称.string 获取嵌套节点内容

View触摸分析

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

15 BS object Node name Node name String get nested node content

Use abp Zero builds a third-party login module (II): server development

TS常用数据类型总结
随机推荐
Program analysis and Optimization - 8 register allocation
feil_uVission4左侧工目录消失
Is it safe to open an online stock account? Somebody give me an answer
Principle of TCP reset attack
Informatics Olympiad all in one 1400: count the number of words (string matching)
Unity UnityWebRequest 下载封装
Is it safe to open a stock account with the account manager online??
The annual salary of 500000 is one line, and the annual salary of 1million is another line
C语言刷题随记 —— 乒乓球比赛
Pytorch深度学习代码技巧
R语言epiDisplay包的tableStack函数制作统计汇总表格(分组的描述性统计、假设检验等)、不设置by参数计算基础描述性统计信息、指定对于大多数样本负相关的变量进行反序
teamviewer显示设备数量上限解决方法
R语言dplyr包intersect函数获取在两个dataframe中都存在的数据行、获取两个dataframe交叉的数据行
这才是优美的文件系统挂载方式,亲测有效
[cloud native] codeless IVX editor programmable by "everyone"
Stream常用操作以及原理探索
Combat readiness mathematical modeling 32 correlation analysis 2
redis集群的重新分片与ASK命令
获取两个dataframe的交并差集
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型