FetchTexture

mail@pastecode.io avatar
unknown
csharp
6 months ago
1.5 kB
0
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class FetchTexture : MonoBehaviour
{

    public string URL;

    void Start()
    {
        StartCoroutine(GetTexture());
    }

    IEnumerator GetTexture()
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(URL))
        {
            yield return uwr.SendWebRequest();

            if(uwr.result != UnityWebRequest.Result.ConnectionError)
            {
                var texture = DownloadHandlerTexture.GetContent(uwr);
                float width = texture.width;
                float height = texture.height;
                Rect sourceRect = new Rect(0, 0, width, height);

                Image img = GetComponent<Image>();
                Sprite sprite = Sprite.Create(texture, sourceRect, Vector2.zero);
                img.sprite = sprite;

                if(uwr.isDone)
                {
                    UiController.Instance.ConnectedToServer();
                }
                

            }
            else
            {
                UiController.Instance.ConnectedToServer();
                Debug.Log("Failed To Connect to Server");
                UiController.Instance.connectedToServer = false;

            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
Leave a Comment