通过UnityWebRequest得到的纹理转精灵

private IEnumerator GetImage(string url, Image fileImage)
    {
        UnityWebRequest WebRequest = new UnityWebRequest(url);
        DownloadHandlerTexture Download = new DownloadHandlerTexture();
        WebRequest.downloadHandler = Download;
        yield return WebRequest.SendWebRequest();

        if (WebRequest.downloadHandler.isDone)
        {
            if (!fileImage)
            {
                if (Application.platform == RuntimePlatform.WindowsEditor ||
                    Application.platform == RuntimePlatform.WindowsPlayer)
                    File.WriteAllBytes(Application.streamingAssetsPath + "/HeadPicture.png", Download.data);
                else if (Application.platform == RuntimePlatform.Android)
                    File.WriteAllBytes(Application.persistentDataPath + "/HeadPicture.png", Download.data);

                SetHeadPictureComponent();
            }
            else
            {
                Rect spriteRect = new Rect(0, 0, Download.texture.width, Download.texture.height);
                Sprite sprite = Sprite.Create(Download.texture, spriteRect, Vector2.zero);
                fileImage.sprite = sprite;
            }
        }
    }
posted @ 2021-06-19 19:10  yassine  阅读(244)  评论(0编辑  收藏  举报