Files
webRtc/Assets/Script/Util/WebRTCUtil.cs

48 lines
1.4 KiB
C#
Raw Normal View History

2026-05-19 22:40:52 +08:00
using System;
using System.IO;
using Stary.Evo;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
namespace Script.Util
{
public static class WebRTCUtil
{
public static Color GetRandomColor()
{
return new Color(
Random.Range(0.2f, 0.8f),
Random.Range(0.2f, 0.8f),
Random.Range(0.2f, 0.8f),
1f
);
}
public static async void DownloadAndSetAvatar(string avatarUrl, Image targetImage)
{
try
{
var tempPath = Path.Combine(Application.temporaryCachePath, $"avatar_{Guid.NewGuid()}.png");
var result = await WebRequestSystem.GetFile(avatarUrl, tempPath);
if (result.code == 200)
{
var bytes = File.ReadAllBytes(tempPath);
var texture = new Texture2D(2, 2);
texture.LoadImage(bytes);
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
Vector2.one * 0.5f);
targetImage.sprite = sprite;
File.Delete(tempPath);
}
}
catch (Exception e)
{
Debug.LogError($"下载头像失败: {e.Message}");
}
}
}
}