Skip to content

Player Profile Picture

This is an example how we can request and use the player's profile and avatar's picture.

All you need to do, is create 2 planes. One for the player's picture and the other for the player avatar's picture, and add their MeshRenderers as bound object.

Note: You need to add the components directly, if you add the gameObjects you need to call the GetComponent on the gameObject to get the mesh renderer reference. Notice on the image, it shows the icon of a MeshRenderer component and not the gameObject.

player-profile-cck.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
UnityEngine = require "UnityEngine"

function Start()

    -- Get the references to the mesh rendereres
    local playerMeshRenderer = BoundObjects.playerMeshRenderer
    local avatarMeshRenderer = BoundObjects.avatarMeshRenderer

    -- We're going to use the local player
    local player = PlayerAPI.LocalPlayer

    -- Request the profile picture image for the player
    print("Requesting the player's profile image for player " .. player.UserID)
    player.RequestProfileImage(function(texture)
        -- Call the GetProfileImage while wrapping the function in a local create function
        -- so we can send some additional context (the player's reference, and the mesh renderer)
        OnPlayerProfileImage(texture, player, playerMeshRenderer)
    end, true)

    -- If the player has the avatar loaded, request the current avatar image for the player
    if player.Avatar.IsLoaded then
        print("Requesting the avatar's profile image for avatar " .. player.Avatar.AvatarID)
        -- Call the GetProfileImage while wrapping the function in a local create function
        -- so we can send some additional context (the player's avatar reference, and the mesh renderer)
        player.Avatar.RequestImage(function(texture)
            OnAvatarImage(texture, player.Avatar, avatarMeshRenderer)
        end, true)
    end
end

function OnPlayerProfileImage(texture, player, playerMeshRenderer)
    print("Received the Player Profile Image for user " .. player.UserID .. "! " .. tostring(texture))
    if texture == nil then
        print("Unfortunately it was nil :(")
        return
    end
    -- Get the current material on the meshrenderer and set the texture
    -- The material SetTexture requires a Texture instance, and not Texture2D
    -- which is why we called GetProfileImage with the second arg true, so the
    -- texture is returned as a Texture instead of Texture2D
    local materialInstance = playerMeshRenderer.material
    materialInstance.SetTexture("_MainTex", texture)
end

function OnAvatarImage(texture, avatar, avatarMeshRenderer)
    print("Received the Avatar Profile Image for avatar " .. avatar.AvatarID .. "! " .. tostring(texture))
    if texture == nil then
        print("Unfortunately it was nil :(")
        return
    end
    -- Get the current material on the meshrenderer and set the texture
    -- The material SetTexture requires a Texture instance, and not Texture2D
    -- which is why we called GetProfileImage with the second arg true, so the
    -- texture is returned as a Texture instead of Texture2D
    local materialInstance = avatarMeshRenderer.material
    materialInstance.SetTexture("_MainTex", texture)
end