Commit 0b54818d authored by mercury233's avatar mercury233

cache avatar & change url

parent 4856fc7f
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
......@@ -6,10 +7,23 @@ namespace DefaultNamespace
{
public class MyCard
{
private static Dictionary<string, Texture2D> cachedAvatars = new Dictionary<string, Texture2D>();
public static void LoadAvatar(string username, Action<Texture2D> callback)
{
if (cachedAvatars.ContainsKey(username))
{
var avatar = cachedAvatars[username];
if (avatar != null)
{
callback(cachedAvatars[username]);
}
return;
}
cachedAvatars.Add(username, null);
var request =
UnityWebRequestTexture.GetTexture($"https://sapi.moecube.com:444/accounts/users/{username}.png");
UnityWebRequestTexture.GetTexture($"https://sapi.moecube.com:444/avatar/avatar/{username}/100/ygopro2.png");
var operation = request.SendWebRequest();
operation.completed += _ =>
{
......@@ -19,7 +33,9 @@ namespace DefaultNamespace
return;
}
callback(((DownloadHandlerTexture) request.downloadHandler).texture);
var avatar = ((DownloadHandlerTexture)request.downloadHandler).texture;
cachedAvatars[username] = avatar;
callback(avatar);
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment