This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Steam/SteamFriendsManager.cs

75 lines
2.3 KiB
C#
Raw Normal View History

2023-06-01 17:03:48 +02:00
using Steamworks;
using UnityEngine;
using UnityEngine.UI;
public class SteamFriendsManager : MonoBehaviour
{
public RawImage playerprofile;
public Text playername;
public Transform friendsContent;
public GameObject friendObj;
async void Start()
{
if (!SteamClient.IsValid) return;
playername.text = SteamClient.Name;
InitFriendsAsync();
var img = await SteamFriends.GetLargeAvatarAsync(SteamClient.SteamId);
playerprofile.texture = GetTextureFromImage(img.Value);
}
public static Texture2D GetTextureFromImage(Steamworks.Data.Image image)
{
Texture2D texture = new Texture2D((int)image.Width, (int)image.Height);
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
var p = image.GetPixel(x, y);
texture.SetPixel(x, (int)image.Height - y, new Color(p.r / 255.0f, p.g / 255.0f, p.b / 255.0f, p.a / 255.0f));
}
}
texture.Apply();
return texture;
}
public void InitFriendsAsync()
{
foreach (var friend in SteamFriends.GetFriends())
{
GameObject f = Instantiate(friendObj, friendsContent);
f.GetComponentInChildren<Text>().text = friend.Name;
f.GetComponent<FriendObject>().steamid = friend.Id;
AssingFriendImage(f, friend.Id);
}
}
public async void AssingFriendImage(GameObject f, SteamId id)
{
var img = await SteamFriends.GetLargeAvatarAsync(id);
f.GetComponentInChildren<RawImage>().texture = GetTextureFromImage(img.Value);
}
public static async System.Threading.Tasks.Task<Texture2D> GetTextureFromSteamIdAsync(SteamId id)
{
var img = await SteamFriends.GetLargeAvatarAsync(SteamClient.SteamId);
Steamworks.Data.Image image = img.Value;
Texture2D texture = new Texture2D((int)image.Width, (int)image.Height);
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
var p = image.GetPixel(x, y);
texture.SetPixel(x, (int)image.Height - y, new Color(p.r / 255.0f, p.g / 255.0f, p.b / 255.0f, p.a / 255.0f));
}
}
texture.Apply();
return texture;
}
}