57 lines
1.4 KiB
C#
57 lines
1.4 KiB
C#
using Steamworks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SteamShowAvatarByUserId : MonoBehaviour
|
|
{
|
|
public ulong steamUserID;
|
|
|
|
private RawImage imageRaw;
|
|
|
|
private Texture startTexture;
|
|
|
|
private void Start()
|
|
{
|
|
imageRaw = GetComponent<RawImage>();
|
|
startTexture = imageRaw.texture;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (SteamManager.Initialized && steamUserID != 0L && imageRaw.texture == startTexture)
|
|
{
|
|
LoadAvatar(steamUserID);
|
|
}
|
|
}
|
|
|
|
public void LoadAvatar(ulong newSteamID)
|
|
{
|
|
CSteamID steamIDFriend = new CSteamID(newSteamID);
|
|
if (SteamFriends.HasFriend(steamIDFriend, EFriendFlags.k_EFriendFlagImmediate) || newSteamID == SteamUser.GetSteamID().m_SteamID)
|
|
{
|
|
int largeFriendAvatar = SteamFriends.GetLargeFriendAvatar(steamIDFriend);
|
|
if (largeFriendAvatar != -1)
|
|
{
|
|
imageRaw.texture = GetSteamImageAsTexture(largeFriendAvatar);
|
|
imageRaw.transform.localScale = new Vector3(1f, -1f, 1f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private Texture2D GetSteamImageAsTexture(int iImage)
|
|
{
|
|
Texture2D texture2D = null;
|
|
if (SteamUtils.GetImageSize(iImage, out var pnWidth, out var pnHeight))
|
|
{
|
|
byte[] array = new byte[pnWidth * pnHeight * 4];
|
|
if (SteamUtils.GetImageRGBA(iImage, array, (int)(pnWidth * pnHeight * 4)))
|
|
{
|
|
texture2D = new Texture2D((int)pnWidth, (int)pnHeight, TextureFormat.RGBA32, mipChain: false, linear: true);
|
|
texture2D.LoadRawTextureData(array);
|
|
texture2D.Apply();
|
|
}
|
|
}
|
|
return texture2D;
|
|
}
|
|
}
|