Currently, when loading from Appdata, there is a noticeable delay. See a sample video here, when it changes from the sprite set in UI to the sprite loaded from Appdata through C# script.
I want to make it so that there is no delay that is visible to the end user. If this delay in loading from Appdata is not avoidable, I'd be fine with loading everything at Start in the background, be it from Appdata or from UnityWebRequest before it is visible to the end user. I would like to know how to do that.
Currently I am loading the texture from UnityWebRequest and converting it to sprite, if Texture does not already exist in Appdata. Here is the code of the ImageLoader class:
public void GenImage(string baseUrl, string imageLin, RawImage rawImag = null, Image imag = null) {
if(isCoroutineRunning) {
StopAllCoroutines();
} else {
if(rawImag != null) StartCoroutine(LoadImage(rawImag, baseUrl+imageLin));
if(imag != null) StartCoroutine(LoadImageSprite(imag, baseUrl, imageLin));
}
}
// **Coroutine class to Load Sprite**
public IEnumerator LoadImageSprite(Image image, string baseUrl, string fileName) {
isCoroutineRunning = true;
UnityWebRequest request = UnityWebRequestTexture.GetTexture(baseUrl+fileName);
yield return request.SendWebRequest();
if(request.result == UnityWebRequest.Result.ConnectionError ||
request.result == UnityWebRequest.Result.ProtocolError) {
Debug.Log(request.error);
} else {
Texture2D nTexture = LoadCachedImage(fileName);
if(nTexture != null) {
Sprite nSprite = Sprite.Create(nTexture, new Rect(0, 0, nTexture.width, nTexture.height), new Vector2(0.5f, 0.5f));
image.sprite = nSprite;
}
else {
nTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
Debug.Log("else block "+nTexture);
Sprite nSprite = Sprite.Create(nTexture, new Rect(0, 0, nTexture.width, nTexture.height), new Vector2(0.5f, 0.5f));
image.sprite = nSprite;
SaveImage(nTexture, fileName);
}
}
isCoroutineRunning = false;
}
// Save and Load Image
public void SaveImage(Texture2D texture, string fileName) {
string savePath = Application.persistentDataPath +"/";
Debug.Log(savePath + " " + fileName);
try {
if(!Directory.Exists(savePath)) {
Directory.CreateDirectory(savePath);
}
byte[] textureBytes = texture.EncodeToJPG();
File.WriteAllBytes(savePath + fileName, textureBytes);
}
catch(Exception e) {
Debug.Log(e.Message);
}
}
public Texture2D LoadCachedImage(string fileName) {
string savePath = Application.persistentDataPath + "/";
try {
if(File.Exists(savePath + fileName)) {
Debug.Log("Exists"+savePath+fileName);
byte[] bytes = File.ReadAllBytes(savePath + fileName);
Texture2D texture = new Texture2D(0,0);
texture.LoadImage(bytes);
return texture;
}
return null;
} catch {
return null;
}
}
and loading image from a different script:
imageLoader.GenImage(baseUrl: ImageLoader.SPRITE_URL, imageLin: "urashiki.jpg", imag:characterIcon);
imageLoader has already been called as necessary. There is no error in Console, everything works as expected except the delay.
I understand loading from UnityWebRequest may have a noticeable delay, but I don't understand why loading from Appdata also has a noticeable delay. I would like to know how to avoid that delay, and if not possible, how to hide that delay.