通过UnityWebRequest得到的纹理转精灵
阅读原文时间:2023年07月09日阅读:1
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;
            }
        }
    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章