接入聊天

This commit is contained in:
2026-05-22 10:59:18 +08:00
parent 369783939b
commit 6f17a740e8
11 changed files with 610 additions and 984 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Cysharp.Threading.Tasks;
using Stary.Evo;
using UnityEngine;
using UnityEngine.UI;
@@ -9,6 +11,8 @@ namespace Script.Util
{
public static class WebRTCUtil
{
public static Dictionary<string, Sprite> _avatarCache = new();
public static Color GetRandomColor()
{
return new Color(
@@ -19,10 +23,16 @@ namespace Script.Util
);
}
public static async void DownloadAndSetAvatar(string avatarUrl, Image targetImage)
public static async UniTask DownloadAndSetAvatar(string avatarUrl, Image targetImage)
{
try
{
if (_avatarCache.TryGetValue(avatarUrl, out var sprite))
{
targetImage.sprite = sprite;
return;
}
var tempPath = Path.Combine(Application.temporaryCachePath, $"avatar_{Guid.NewGuid()}.png");
var result = await WebRequestSystem.GetFile(avatarUrl, tempPath);
@@ -32,11 +42,12 @@ namespace Script.Util
var texture = new Texture2D(2, 2);
texture.LoadImage(bytes);
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
Vector2.one * 0.5f);
targetImage.sprite = sprite;
File.Delete(tempPath);
_avatarCache.TryAdd(avatarUrl, sprite);
}
}
catch (Exception e)