当前位置:网站首页>Download Text, pictures and ab packages used by unitywebrequest Foundation

Download Text, pictures and ab packages used by unitywebrequest Foundation

2022-07-07 10:10:00 Sunshine in front of my heart

Get ready

UnityWebRequest
downloadHandler Is a download assistant , The downloaded resources can be obtained directly through it
UnityWebRequest Inherit IDisposable Interface
Be careful : The server you visit should have download permission To download

Download the text

IEnumerator LoadText(string url)
{
    
    using (UnityWebRequest req = UnityWebRequest.Get(url))
    {
    
        yield return req.SendWebRequest();// issue get request 
        if (req.error == null)
        {
    
            Debug.Log(req.downloadHandler.text);
            Debug.Log(req.downloadHandler.data.Length);
        }
        else
        {
    
            Debug.Log(" Download error " + req.responseCode + "," + req.error);
        }
    }
}

Download the pictures

IEnumerator LoadTexture(string url)
{
    
    using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(url))
    {
    
        yield return req.SendWebRequest();
        if (req.error == null)
        {
    
            Debug.Log((req.downloadHandler as DownloadHandlerTexture).texture.width);
            Debug.Log(DownloadHandlerTexture.GetContent(req).height);
        }
        else
        {
    
            Debug.Log(" Download error " + req.responseCode + "," + req.error);
        }
    }
}

download AB package

IEnumerator LoadAB(string url)
{
    
    using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(url))
    {
    
        yield return req.SendWebRequest();
        if (req.error == null)
        {
    
            Debug.Log((req.downloadHandler as DownloadHandlerAssetBundle).assetBundle.name);
            Debug.Log(DownloadHandlerAssetBundle.GetContent(req).name);
        }
        else
        {
    
            Debug.Log(" Download error " + req.responseCode + "," + req.error);
        }
    }
}

原网站

版权声明
本文为[Sunshine in front of my heart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070731403058.html