How to load texture from web on run time in Unity and apply it to an object

I have an idea to store texture files on web (on server) and then we allow users to load any textures available on web to apply to an object in Unity.

So I experimented with this script:

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

public class TexturesFromWeb : MonoBehaviour
	public InputField texUrlInput;
	public void setTextureFromWeb(){
		string textureurl = texUrlInput.text;
	IEnumerator DownloadTexture(string textureurl)
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(textureurl);
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
            this.gameObject.GetComponent<Renderer>().material.mainTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;

I placed a cube to the scene, also an input field and a button. By entering texture url on the input field and clicking the button, I should be able to apply the online texture to the cube.

Leave a Reply

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