Friend Manager
Available in the Steamworks Complete and Foundation asset.
This tool simply exposes common features present in the API to the inspector.
This is not required to use these features it is simply a helper tool allowing user's who are more comfortable working with editor inspectors and game object rather than classic C# objects and scripting to make use of the related feature.

Namespace

1
using HeathenEngineering.SteamworksIntegration;
Copied!

Definition

1
public class FriendManager : MonoBehaviour
Copied!

Events

evtGameConnectedChatMsg

1
public GameConnectedFriendChatMsgEvent evtGameConnectedChatMsg
Copied!
Received a message from a friend, this willy be raised if you first set the ListenForFriendsMessages to true.
The handler for this event would look like the following.
1
public void HandleFriendChatMsg(UserData friend,
2
string message,
3
EChatEntryType type)
4
{
5
//Do Work
6
}
Copied!
You can see the various options for EChatEntryType in Valve's documentation as linked.

evtRichPresenceUpdated

1
public FriendRichPresenceUpdateEvent evtRichPresenceUpdated
Copied!
Received a notfication of rich presence change
The handler for this event would look like the following
1
public void HandleRichPresenceUpdated(FriendRichPresenceUpdate_t update)
2
{
3
//Do Work
4
}
Copied!
The FriendRichPresenceUpdate_t structure is defined in Steamworks namespace space and discribes the friend ID and AppId related to the event.

evtPersonaStateChanged

1
public PersonaStateChangeEvent evtPersonaStateChanged
Copied!
Received a notification of persona state change
The handler for this event would look like the following
1
public void HandlePersonaStateChanged(PersonaStateChange_t stateChange)
2
{
3
//Do Work
4
}
Copied!
The PersonaStateChange_t structure is defined in the Steamworks namespace and discribes the user the change is related to as well as the changes.

Fields and Attributes

ListenForFriendsMessages

1
public bool ListenForFriendsMessages { get; set; }
Copied!
This indicates rather or not the system is listening for freind chat messages. Note that friend chat messages will only be raised in game if you set this to true.

Methods

Get Friends

1
public UserData[] GetFriends(EFriendFlags flags);
Copied!
Get lists of user friends, The flags argument is of type EFriendFlags and can be used to modify what subset is returned.
This is the same as calling API.Friends.Client.GetFriends(flags);

Get Coplay Friends

1
public UserData[] GetCoplayFriends();
Copied!
Gets the list of friends the user has recently played with

Get Friend Message

1
public string GetFriendMessage(UserData userId,
2
int index,
3
out EChatEntryType type);
Copied!
You should use this in responce to evtGameConnectedChatMsg to fetch the body of the message and the type of the message.
You shouldn't need to call this as we will call it for you and raise the evtGameConnectedChatMsg with the results.

SendMessage

1
public bool SendMessage(UserData friend, string message);
Copied!
Sends the target friend a string message. If this returns false it means the local user is rate or chat limited by Valve and cannot send a message.

How To

Create an in-game Friends List

The UI work is left up to you ... the following code will demonstrate fetching friends and how you might read the data contained within.
Note that the "Immediate" flag is your "normal" friends such as what you see in your Steam Client's friend list.
1
UserData[] friends = friendManager.GetFriends(EFriendFlags.k_EFriendFlagImmediate);
Copied!
Now that we have an array of our friends we can simply iterate over that and populate our UI
1
foreach(UserData friend in friends)
2
{
3
//freind.Name;
4
//friend.Avatar;
5
//etc.
6
}
Copied!
Be sure to read and understand all the features of the UserData object. You can also use tools like SetUserAvatar and SetUserName to simplify fetching and keeping up to date the friend's avatar image and display name.

Create an in-game Friend Chat

The UI work is left up to you ... the following code will demonstrate enabling friend chat, listening for chat messages and sending messages to that friend.
First we should set up our event handler to listen for friend chat messages. You can do this in the Unity inspector or in code.
1
friendManager.evtGameConnectedChatMsg.AddListener(HandleChatMsg);
Copied!
The handler for that would look like this
1
private void HandleChatMsg(UserData friend, string message, EChatEntryType type)
2
{
3
if(type == EChatEntryType.k_EChatEntryTypeChatMsg)
4
{
5
//We got a messaage from friend.Name so display it
6
}
7
}
Copied!
In the above example we are only paying attention to chat message types of ChatEntry. You can see all the different types here and what they mean.
Before the system will raise the evtGameConnectedChatMsg event we need to tell it to listen for messages.
1
friendManager.ListenForFriendsMessages = true;
Copied!
Finally in order to send a friend a chat message we use the UserData of that friend. Lets assume we have a UserData named friend and that is who we want to send the message to.
1
friend.SendMessage("Hello Friend");
Copied!
We could also send the message via the friendManager such as
1
friendManager.SendMessage(friend, "Hello Friend");
Copied!
This is simply an example you would of course want to give the user a means to input text to send to your friend so you would likely drive this from a InputField's on submit or similar.