Skip to content

Instantly share code, notes, and snippets.

@garrynewman
Created January 10, 2018 11:18
Show Gist options
  • Save garrynewman/cb5ed2d7e9c144708e997a53075c9b81 to your computer and use it in GitHub Desktop.
Save garrynewman/cb5ed2d7e9c144708e997a53075c9b81 to your computer and use it in GitHub Desktop.
using UnityEngine;
using Facepunch.Steamworks;
using UnityEngine.UI;
//
// To change at runtime set SteamId then call Fetch()
//
public class SteamAvatar : MonoBehaviour
{
public ulong SteamId;
public Friends.AvatarSize Size;
public Texture FallbackTexture;
void Start()
{
Fetch();
}
public void Fetch()
{
if (SteamId == 0) return;
if (Client.Instance == null)
{
ApplyTexture(FallbackTexture);
return;
}
Client.Instance.Friends.GetAvatar(Size, SteamId, OnImage);
}
private void OnImage( Facepunch.Steamworks.Image image )
{
if ( image == null )
{
ApplyTexture(FallbackTexture);
return;
}
var texture = new Texture2D(image.Width, 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, image.Height - y, new UnityEngine.Color( p.r / 255.0f, p.g / 255.0f, p.b / 255.0f, p.a / 255.0f ) );
}
texture.Apply();
ApplyTexture(texture);
}
private void ApplyTexture(Texture texture)
{
var rawImage = GetComponent<RawImage>();
if (rawImage != null)
rawImage.texture = texture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment