Esempio n. 1
0
    //Thanks to Karl @ Stunlock Studios for giving me their function as he implemented utils.GetImage(Size/RGBA) into our library
    public Texture2D GetTextureFromSteamID(SteamID steamId)
    {
        IFriends friends = Steamworks.SteamInterface.Friends;
        IUtils   utils   = Steamworks.SteamInterface.Utils;

        ImageHandle avatarHandle = friends.GetLargeFriendAvatar(steamId);

        if (avatarHandle.IsValid)
        {
            uint width, height;
            if (utils.GetImageSize(avatarHandle, out width, out height))
            {
                Texture2D texture = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, true);
                Color32[] buffer  = new Color32[width * height];

                GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

                try
                {
                    System.IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);

                    if (utils.GetImageRGBA(avatarHandle, bufferPtr, (int)width * (int)height * 4))
                    {
                        // Flip vertical
                        for (int x = 0; x < width; x++)
                        {
                            for (int y = 0; y < height / 2; y++)
                            {
                                Color32 temp = buffer[x + (width * y)];
                                buffer[x + (width * y)] = buffer[x + (width * (height - 1 - y))];
                                buffer[x + (width * (height - 1 - y))] = temp;
                            }
                        }

                        texture.SetPixels32(buffer);
                        texture.Apply();
                    }
                }
                finally
                {
                    bufferHandle.Free();
                }
                return(texture);
            }
        }

        return(null);
    }