using Steamworks; using UnityEngine; namespace Ashwild.Network { /// /// Turns a Steam avatar image handle into a Unity Sprite. Steam hands us raw RGBA pixels /// laid out top-to-bottom, whereas Unity textures are bottom-to-top, so the rows are flipped /// while copying. Returns null for any invalid/empty handle so callers can fall back cleanly. /// public static class SteamAvatarUtil { /// /// Builds a Sprite from a Steam image handle (e.g. from GetLargeFriendAvatar). /// public static Sprite BuildSprite(int imageHandle) { if (imageHandle <= 0) return null; if (!SteamUtils.GetImageSize(imageHandle, out uint width, out uint height)) return null; if (width == 0 || height == 0) return null; int w = (int)width; int h = (int)height; byte[] buffer = new byte[w * h * 4]; if (!SteamUtils.GetImageRGBA(imageHandle, buffer, buffer.Length)) return null; // Flip vertically: Steam rows run top→bottom, Unity expects bottom→top. Color32[] pixels = new Color32[w * h]; for (int y = 0; y < h; y++) { int srcRow = (h - 1 - y) * w * 4; int dstRow = y * w; for (int x = 0; x < w; x++) { int s = srcRow + x * 4; pixels[dstRow + x] = new Color32(buffer[s], buffer[s + 1], buffer[s + 2], buffer[s + 3]); } } Texture2D texture = new Texture2D(w, h, TextureFormat.RGBA32, false, true) { wrapMode = TextureWrapMode.Clamp }; texture.SetPixels32(pixels); texture.Apply(false, false); return Sprite.Create(texture, new Rect(0f, 0f, w, h), new Vector2(0.5f, 0.5f), 100f); } } }