Initial version. Uses external library for image resizing. Fixed memory consumption, but slow.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Cedar.Core.Coroutines;
|
||||
using HtmlAgilityPack;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public enum LoaderStatus
|
||||
{
|
||||
Initializing,
|
||||
Ready,
|
||||
Error
|
||||
}
|
||||
|
||||
public class ImageLoader
|
||||
{
|
||||
private bool ready = false;
|
||||
private string errorMessage;
|
||||
private string url;
|
||||
|
||||
public LoaderStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!ready) return LoaderStatus.Initializing;
|
||||
if (string.IsNullOrEmpty(errorMessage)) return LoaderStatus.Ready;
|
||||
return LoaderStatus.Error;
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> images;
|
||||
|
||||
public ImageLoader(string url)
|
||||
{
|
||||
this.url = url;
|
||||
CoroutineRunner.Instance.StartCoroutine(OnUrlUpdated());
|
||||
}
|
||||
|
||||
private IEnumerator OnUrlUpdated()
|
||||
{
|
||||
errorMessage = null;
|
||||
var request = UnityWebRequest.Get(url);
|
||||
yield return request.SendWebRequest();
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
{
|
||||
errorMessage = request.error;
|
||||
ready = true;
|
||||
yield break;
|
||||
}
|
||||
var responseText = request.downloadHandler.text;
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml(responseText);
|
||||
var nodes = doc.DocumentNode.SelectNodes("//a[preceding-sibling::img[@alt=\"[IMG]\"]]");
|
||||
images = new List<string>();
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var target = node.GetAttributeValue("href", "");
|
||||
images.Add(url + "/" + target);
|
||||
}
|
||||
ready = true;
|
||||
request.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user