using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
namespace WebP.Experiment.Animation
{
///
/// WebP loader for loading assets, should be override to suit your own needs.
///
public class WebPLoader
{
///
/// The actual function to load file from remote location or project related absolute path
///
public static async Task Load(string url)
{
if (string.IsNullOrEmpty(url))
{
Debug.LogError("[WebP] Loading path can not be empty");
return null;
}
Debug.Log($"[WebP] Try loading WebP file: {url}");
byte[] bytes = null;
if (url.Contains("//") || url.Contains("///"))
{
var www = await LoadAsync(url);
bytes = www.downloadHandler.data;
}
else
{
try
{
bytes = File.ReadAllBytes(url);
}
catch (Exception e)
{
Debug.LogError($"[WebP] load error: {e.Message}");
}
}
return bytes;
}
///
/// Example for async UnityWebRequest
///
/// This function won't work, just example!!!
/// You should implement your own loading logic here!
///
private static async Task LoadAsync(string path)
{
var www = new UnityWebRequest(path);
var op = www.SendWebRequest();
while (!op.isDone)
{
await Task.Delay(1000 / 60);
}
return www;
}
}
}