How to set a texture of a Game Object from URL in Unity3D



I’ve searched for this thing recently on internet and I found a way to set a texture image from URL to a game object in Unity3d.

Here is the script, you can attach this script to your GameObject, but don’t worry to specify your desired URL on the inspector.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class LoadTextureFromURL : MonoBehaviour
{

    public string TextureURL = "";

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(DownloadImage(TextureURL));
    }

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

    IEnumerator DownloadImage(string MediaUrl)
    {
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
            Debug.Log(request.error);
        else
            this.gameObject.GetComponent<Renderer>().material.mainTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
    }
}

You can download a UnityPackage containing this script and an example scene here: https://drive.google.com/open?id=12pekmW_MAucbBY-mm5LIlCsvSCTSYTTU

UPDATE:

I’ve made an update, you can get the complete updated scripts from GitHub: https://github.com/habibieamrullah/Load-Image-from-URL-in-Unity

Here is the new video about it:

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *