RustDedicated/Rust.Platform.Common/AvatarCache.cs
2025-08-09 20:48:06 +09:30

56 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class AvatarCache
{
private readonly struct Entry
{
public readonly ulong UserId;
public readonly Texture2D Texture;
public Entry(ulong userId, Texture2D texture)
{
UserId = userId;
Texture = texture;
}
}
private readonly Dictionary<ulong, Entry> _entries;
private readonly Action<ulong, Texture2D> _loadAvatar;
public AvatarCache(Action<ulong, Texture2D> loadAvatar)
{
_entries = new Dictionary<ulong, Entry>();
_loadAvatar = loadAvatar ?? throw new ArgumentNullException("loadAvatar");
}
public Texture2D Get(ulong userId)
{
if (_entries.TryGetValue(userId, out var value))
{
return value.Texture;
}
Texture2D texture2D = new Texture2D(64, 64, TextureFormat.ARGB32, mipChain: false)
{
name = $"Avatar_{userId}",
filterMode = FilterMode.Trilinear,
wrapMode = TextureWrapMode.Clamp,
anisoLevel = 8
};
for (int i = 0; i < texture2D.width; i++)
{
for (int j = 0; j < texture2D.height; j++)
{
texture2D.SetPixel(i, j, new Color32(0, 0, 0, 20));
}
}
texture2D.Apply(updateMipmaps: true);
Entry value2 = new Entry(userId, texture2D);
_entries.Add(userId, value2);
_loadAvatar(userId, texture2D);
return texture2D;
}
}