How to load textures on run-time for a game that is built for WebGL
This code loads textures from the StreamingAssets folder. It works on the editor and standalone build for PC but doesnt work on WebGL.
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.Networking; using System.Net;
[System.Serializable] // To make List objects visible in the inspector public class AssetsLoader : MonoBehaviour {
public List<Texture> AllTextures;
public int DefaultTexture = 0; // Default texture
public GameObject ConsoleComponent;
void Start () {
StartCoroutine ("LoadAllTextures");
}
IEnumerator LoadAllTextures()
{
AllTextures = new List<Texture>();
string Folderpath= Application.streamingAssetsPath+"/ArtGalleryTextures";
string[] TextureFilePaths = Directory.GetFiles(Folderpath, "*.jpg");
for (int i = 0; i < TextureFilePaths.Length; i++)
{
UnityWebRequest www = UnityWebRequest.Get(Folderpath);
yield return www.SendWebRequest();
WWW diskTextureDir;
diskTextureDir = new WWW("file://" + TextureFilePaths[i]);
//Save the loaded texture from WWW
AllTextures.Add(diskTextureDir.texture);
}
}
}
Comment