当前位置:网站首页>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);
});
}
}
边栏推荐
- 710. random numbers in the blacklist
- 710. 黑名单中的随机数
- Principle of TCP reset attack
- C语言刷题随记 —— 乒乓球比赛
- qt下多个子控件信号槽绑定方法
- The engine "node" is inconsistent with this module
- R语言dplyr包intersect函数获取在两个dataframe中都存在的数据行、获取两个dataframe交叉的数据行
- Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
- [solo π] ADB connects multiple mobile phones
- One copy ten, CVPR oral was accused of plagiarizing a lot, and it was exposed on the last day of the conference!
猜你喜欢

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

The annual salary of 500000 is one line, and the annual salary of 1million is another line

Solution to the upper limit of TeamViewer display devices

Mark: unity3d cannot select resources in the inspector, that is, project locking

详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型

Use abp Zero builds a third-party login module (I): Principles

The heavyweight white paper was released. Huawei continues to lead the new model of smart park construction in the future

Keil4打开单片机工程一片空白,cpu100%程序卡死的问题解决

Common operation and Principle Exploration of stream

权威发布 | 延安大学2022年教师岗位招聘公告
随机推荐
杜老师说网站更新图解
R语言epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.X.axis参数指定X轴轴刻度数值标签字体的大小
Principle of TCP reset attack
Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
A remove the underline from the label
文献1
Can wptx64 be uninstalled_ Which software of win10 can be uninstalled
Use abp Zero builds a third-party login module (I): Principles
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
网上找客户经理办理股票开户安全吗??
R language dplyr package bind_ The rows function merges the rows of the two dataframes vertically. The final number of rows is the sum of the rows of the original two dataframes (combine data frames)
这才是优美的文件系统挂载方式,亲测有效
Summary of decimal point of amount and price at work and pit
Optimizing for vectorization
Electron
子查询的使用
'coach, I want to play basketball!'—— AI Learning Series booklet for system students
Halcon C# 设置窗体字体,自适应显示图片
券商经理给的开户链接办理股票开户安全吗?我想开个户
Optimizing for vectorization