Reading and Updating User Properties

Prerequisite

How to Check if the User is Anonymous?

To check if the user is anonymous, use GetSocial.User class:

1
2
3
using GetSocialSdk.Core;
// ...
bool anonymous = GetSocial.User.IsAnonymous;

Get User Information

If you want to get user’s name or avatar, you can use relevant getters on GetSocial User:

1
2
3
4
5
// Get User's name:
GetSocial.User.DisplayName

// Get User's avatar URL:
GetSocial.User.AvatarUrl

Change User Name or Avatar

You can change the display name and/or avatar for the GetSocial user at anytime. Provided display name and avatar will be displayed on activities, comments or any other UI related to the user.

To update display name of the user:

1
2
3
4
5
6
7
GetSocial.User.SetDisplayName("Happy Tiger",
    () => {
        Debug.Log ("Name successfully changed: " + GetSocial.User.DisplayName);
    },
    error => {
        Debug.Log ("Name change failed, error: " + error);
    });

To update avatar URL of the user:

1
2
3
4
5
6
7
GetSocial.User.SetAvatarUrl("https://example.com/avatar.png",
    () => {
        Debug.Log ("Avatar successfully changed: " + GetSocial.User.AvatarUrl);
    },
    error => {
        Debug.Log ("Avatar update failed, error: " + error);
    });

Use HTTPS for avatar urls

On iOS you have to make sure that the urls of the avatars you use are either https or the domain you are using is added as an exception on the info.plist according to the new App Transport Security.

Also, you can update an avatar using Texture2D object. Recommended resolution is 300x300px. Bigger images will be automatically downscaled on the backend:

1
2
3
4
5
6
7
8
Texture2D avatarImage = ...; // get your image here
GetSocial.User.SetAvatar(avatarImage,
    () => {
        Debug.Log ("Avatar successfully changed: " + GetSocial.User.AvatarUrl);
    },
    error => {
        Debug.Log ("Avatar update failed, error: " + error);
    });

Custom User Properties

Custom properties are string to string map of any information you want to store, located on our servers.

Custom properties have some limitation:

  • Max key length is 32 symbols.
  • Max value length is 1024 symbols.
  • Min value length is 1 symbol, you can not set an empty property, it will just remove a current value if it exists.

This is valid for both Public and Private properties.

Private Properties

To set a private property:

1
2
3
4
5
6
7
GetSocial.User.SetPrivateProperty(key, value, () =>
{
    Debug.Log("Successfully set a private property");
}, (error) =>
{
    Debug.Log("Failed to set a private property");
});

In case if user already had a value for a current key, it will be overriden. You can prevent it with checking if property already exists:

1
bool hasPrivateProperty = GetSocial.User.HasPrivateProperty(key);

You can get the current property value:

1
string currentValue = GetSocial.User.GetPrivateProperty(key);

If you’re not interesting in keeping property for a user, remove it:

1
2
3
4
5
6
7
GetSocial.User.RemovePrivateProperty(key, value, () =>
{
    Debug.Log("Successfully removed a private property");
}, (error) =>
{
    Debug.Log("Failed to remove a private property");
});

You can get all properties as unmodifiable copy of origin dictionary:

1
Dictionary<string, string> userProperties = GetSocial.User.AllPublicProperties;

Note

You can not modify properties with this dictionary. Also, changes in user properties won’t affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest to use GetSocial SDK methods.

Public Properties

You have similar methods to work with public properties, as for private ones:
SetPublicProperty, GetPublicProperty, HasPublicProperty and RemovePublicProperty.

In addition to this, Public properties are visible for other users, so if you have some PublicUser entity, you can get his public properties:

1
string currentValue = GetSocial.User.GetPrivateProperty(key);

Or, you can check does user have a property:

1
bool hasPrivateProperty = GetSocial.User.HasPrivateProperty(key);

You can get all properties as unmodifiable copy of origin dictionary:

1
Dictionary<string, string> userProperties = GetSocial.User.AllPrivateProperties;

Note

You can not modify properties with this dictionary. Also, changes in user properties won’t affect your copy, so you need to refresh it after any user changes. To have up-to-date information, we suggest to use GetSocial SDK methods.

Subscribe to User Lifecycle Changes

If you want to be notified when SDK gets initialized or when Switch user occurs, you may subscribe to these events.

In order to do this, subscribe to the updates in the Awake() method of your MonoBehaviour implementation:

1
2
3
4
5
6
7
8
9
void Awake()
{
    GetSocial.User.SetOnUserChangedListener(() => {
        Debug.Log ("User is anonymous:  " + GetSocial.User.IsAnonymous);
        Debug.Log ("User's displayName: " + GetSocial.User.DisplayName);
        Debug.Log ("User's avatarURL:   " + GetSocial.User.AvatarUrl);
        Debug.Log ("User's identites:   " + GetSocial.User.AuthIdentities.ToDebugString());
    });        
}

Give us your feedback! Was this article helpful?

😀 🙁