当前位置:网站首页>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);
});
}
}
边栏推荐
- 文献1
- 这才是优美的文件系统挂载方式,亲测有效
- 印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网
- SAP gui 770 下载
- Sharing ideas for a quick switch to an underlying implementation
- Can wptx64 be uninstalled_ Which software of win10 can be uninstalled
- Where do people get their top energy?
- 乐鑫 AWS IoT ExpressLink 模组达到通用可用性
- Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
- TCP拥塞控制详解 | 1. 概述
猜你喜欢
![[cloud native] codeless IVX editor programmable by](/img/10/7c56e46df69be6be522a477b00ec05.png)
[cloud native] codeless IVX editor programmable by "everyone"

Combat readiness mathematical modeling 32 correlation analysis 2

TCP congestion control details | 1 summary

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

Stream常用操作以及原理探索

How to mount cloud disks in ECS

feil_uVission4左侧工目录消失

获取两个dataframe的交并差集

710. 黑名单中的随机数

【soloπ】adb连接单个多个手机
随机推荐
Use of subqueries
Electron
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
Declaration and assignment of go variables
Pytoch deep learning code skills
Redis集群消息
集群中命令的执行过程
Unity UnityWebRequest 下载封装
710. random numbers in the blacklist
备战数学建模30-回归分析2
Sharing ideas for a quick switch to an underlying implementation
Document 1
Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)
A remove the underline from the label
Go变量的声明与赋值
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)
北京银行x华为:网络智能运维夯实数字化转型服务底座
kubernetes的Controller之deployment
Principle of TCP reset attack
网上股票开户安不安全?谁给回答一下