当前位置:网站首页>Unity C # e-learning (VIII) -- www
Unity C # e-learning (VIII) -- www
2022-06-26 15:05:00 【Handsome_ shuai_】
Unity C# E-learning ( 8、 ... and )——WWW
One .WWW class
www Primary supported protocols
1.http:// and https:// Hypertext transfer protocol
2.ftp:// Text transfer protocol ( But only anonymous downloads )
3.file:// Local file transfer protocol
1.www Class
// establish www request
WWW www = new WWW("http://192.168.1.103:8080/Http_Server/123.pptx");
// Returns a sound slice from the downloaded data AudioClip object
AudioClip cp = www.GetAudioClip();
// Replace the existing one with the image in the downloaded data Texture2D object
Texture2D tex = new Texture2D(100, 100);
www.LoadImageIntoTexture(tex);
// Load from cache AB Package object , If the package is not in the cache, it will be automatically downloaded and stored in the cache , To load... Directly from the cache
WWW.LoadFromCacheOrDownload("http://192.168.1.103:8080/Http_Server/123.pptx", 1);
// If the loaded data is AB package , You can get the loading result directly through this variable
var ab = www.assetBundle;
// If you are loading an audio slice file , You can get the loading result directly through this variable
var cp1 = www.GetAudioClip();
// Get the loaded contents in the form of byte array
var bytes = www.bytes;
// Get the number of bytes downloaded in the past
var num = www.bytesDownloaded;
// Return an error message , If an error occurs during the download , You can get the error information by
var error = www.error;
// Determine whether the download is complete
var done = www.isDone;
// Get download progress
var poss = www.progress;
// If the resource is in string form
var str = www.text;
// If the resource is in the form of a picture
var texture = www.texture;
2. utilize www Class to load and download files asynchronously
(1) download HTTP Content on the server
private IEnumerator DownLoadHttp()
{
WWW www = new WWW("http://192.168.1.103:8080/Http_Server/xxx.jpg");
while (!www.isDone)
{
Debug.Log(" speed of progress :" + www.progress);
Debug.Log(" Downloaded size :" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage1.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
(2) download FTP Content on the server
- Be careful :www about FTP The server can only be accessed anonymously , Need to be in FTP Create anonymous users on the server (Anonymous)
private IEnumerator DownLoadFtp()
{
WWW www = new WWW("ftp://192.168.1.103/1.jpg");
while (!www.isDone)
{
Debug.Log(" speed of progress :" + www.progress);
Debug.Log(" Downloaded size :" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage2.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
(3) Load local content
private IEnumerator LoadLocalFile()
{
string path = "file://" + Application.streamingAssetsPath + "/test.jpg";
WWW www = new WWW(path);
while (!www.isDone)
{
Debug.Log(" speed of progress :" + www.progress);
Debug.Log(" Downloaded size :" + www.bytesDownloaded);
yield return null;
}
if (www.error == null && www.isDone)
{
rawImage3.texture = www.texture;
}
else
{
Debug.Log(www.error);
}
}
Two . encapsulation WWWMgr Management category
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(" Download progress :" + www.progress);
Debug.Log(" Size loaded :" + 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. test
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/ Program instructions .txt",(string text) =>
{
Debug.Log(text);
});
}
}
边栏推荐
- Use abp Zero builds a third-party login module (I): Principles
- Unity C# 网络学习(九)——WWWFrom
- 【使用yarn运行报错】The engine “node“ is incompatible with this module.
- kubernetes的Controller之deployment
- Execution of commands in the cluster
- 15 bs对象.节点名称.节点名称.string 获取嵌套节点内容
- This is the graceful file system mounting method, which is effective through personal testing
- R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and use agg
- [async/await] - the final solution of asynchronous programming
- [solo π] ADB connects multiple mobile phones
猜你喜欢

710. 黑名单中的随机数

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

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

印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)

Document 1

RestCloud ETL解决shell脚本参数化

Program analysis and Optimization - 8 register allocation

乐鑫 AWS IoT ExpressLink 模组达到通用可用性
![[cloud native] codeless IVX editor programmable by](/img/10/7c56e46df69be6be522a477b00ec05.png)
[cloud native] codeless IVX editor programmable by "everyone"
随机推荐
Restcloud ETL extraction de données de table de base de données dynamique
Unity C# 网络学习(十)——UnityWebRequest(一)
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
Is it safe to open an online stock account? Somebody give me an answer
Cluster addslots establish a cluster
Datasets dataset class (2)
BLE抓包调试信息分析
Numpy基本使用
What is the ranking of Guosen Securities? Is it safe to open a stock account?
Declaration and assignment of go variables
Sharing ideas for a quick switch to an underlying implementation
Bank of Beijing x Huawei: network intelligent operation and maintenance tamps the base of digital transformation service
clustermeet
The R language cartools package divides data, the scale function scales data, and the KNN function of the class package constructs a k-nearest neighbor classifier
The JVM outputs GC logs, causing the JVM to get stuck. I am stupid
One copy ten, CVPR oral was accused of plagiarizing a lot, and it was exposed on the last day of the conference!
Smoothing data using convolution
Redis-集群
Redis cluster re fragmentation and ask command