FriendManager
Scripts/Managers/FriendManager.cs
The FriendManager is the core runtime component of the Universal Friend List system. It implements the IFriendManager interface and provides all friend-related functionality including managing friends, handling requests, blocking users, and coordinating with the game type system.
Inspector Fields (Serialized)
Configuration Section
| Field | Type | Default | Description |
|---|---|---|---|
_backendType | NetworkBackendType | PhotonPUN | Which network backend to use |
_useNativeImplementation | bool | true | Use native friends API or custom server |
_persistFriendList | bool | true | Save friend list between sessions |
_persistBlockList | bool | true | Save blocked users between sessions |
_refreshInterval | float | 30 | Seconds between friend list refreshes |
_maxFriends | int | 100 | Maximum number of friends allowed |
_maxBlockedUsers | int | 100 | Maximum blocked users allowed |
_defaultGameType | GameType | RoomBased | Default game type for invitations |
_autoInitializeGameType | bool | true | Auto-create game type handler on start |
UnityEvents (Inspector Bindable)
These events can be connected to UI elements or other scripts directly in the Unity Inspector without code:
| Event | Payload | When It Fires |
|---|---|---|
_onFriendListUpdated | List<Friend> | Friend list is refreshed or changed |
_onFriendRequestReceived | FriendRequest | A new incoming friend request arrives |
_onFriendRequestAccepted | FriendRequest | Your friend request was accepted |
_onFriendRequestRejected | FriendRequest | Your friend request was rejected |
_onFriendRequestCancelled | FriendRequest | Request sender cancelled |
_onFriendStatusChanged | Friend, FriendStatus | Friend's online status changed |
_onFriendAdded | Friend | A new friend was successfully added |
_onFriendRemoved | Friend | A friend was removed from the list |
_onUserBlocked | BlockEntry | A user was successfully blocked |
_onUserUnblocked | string (userId) | A user was removed from block list |
_onInvitationReceived | FriendInvitation | A game invitation was received |
Runtime Properties (Read-Only)
| Property | Type | Description |
|---|---|---|
CurrentUserId | string | The ID of the currently logged-in user |
Friends | IReadOnlyList<Friend> | Current list of friends |
PendingRequests | IReadOnlyList<FriendRequest> | Pending friend requests |
BlockedUsers | IReadOnlyList<BlockEntry> | Blocked users list |
BackendType | NetworkBackendType | Currently used backend |
IsUsingNativeImplementation | bool | Whether using native or custom |
IsInitialized | bool | Whether system is ready |
CurrentGameTypeHandler | IGameTypeHandler | Active game type handler |
DefaultGameType | GameType | Default game type setting |
C# Events (For Programmers)
Beyond UnityEvents, FriendManager also exposes standard C# events that you can subscribe to in code for more control:
| Event | Signature | Description |
|---|---|---|
OnFriendsListUpdated | Action<IReadOnlyList<Friend>> | Fired when friend list changes |
OnFriendRequestReceived | Action<FriendRequest> | New incoming request |
OnFriendRequestAccepted | Action<Friend> | Request was accepted |
OnFriendRequestRejected | Action<string> | Request rejected (userId) |
OnFriendRequestCancelled | Action<FriendRequest> | Request cancelled |
OnFriendStatusChanged | Action<Friend> | Friend status changed |
OnFriendAdded | Action<Friend> | Friend added |
OnFriendRemoved | Action<Friend> | Friend removed |
OnUserBlocked | Action<BlockEntry> | User blocked |
OnUserUnblocked | Action<string> | User unblocked |
OnInvitationReceived | Action<FriendInvitation> | Invitation received |
OnPendingRequestsRefreshed | Action<IReadOnlyList<FriendRequest>> | Requests list updated |
Example Subscription
// Subscribe to friend added event
FriendManager.Instance.OnFriendAdded += (friend) => {
Debug.Log($"New friend added: {friend.DisplayName}");
};
// Subscribe to status changes
FriendManager.Instance.OnFriendStatusChanged += (friend) => {
Debug.Log($"Friend {friend.DisplayName} is now {friend.Status}");
};
Key Methods
Initialization
| Method | Description |
|---|---|
ApplySettings(FriendManagerSettings) | Apply settings from a ScriptableObject asset |
Initialize(string userId) | Initialize with a specific user ID |
Friend Operations
| Method | Description |
|---|---|
RefreshFriendList() | Manually refresh the friend list from server |
SendFriendRequest(userId, callback) | Send a friend request |
AddFriend(userId, callback) | Directly add a friend (if allowed by backend) |
AcceptFriendRequest(requestId, callback) | Accept a pending request |
RejectFriendRequest(requestId, callback) | Reject a pending request |
CancelFriendRequest(requestId, callback) | Cancel a request you sent |
RemoveFriend(userId, callback) | Remove someone from your friends |
Block Operations
| Method | Description |
|---|---|
BlockUser(userId, callback) | Block a user |
UnblockUser(userId, callback) | Remove a user from the block list |
RefreshBlockedUsers() | Refresh the block list |
Invitation Operations
| Method | Description |
|---|---|
InviteFriend(userId, message, customData, callback) | Send an invitation to a friend |
AcceptInvitation(invitationId, callback) | Accept a game invitation |
DeclineInvitation(invitationId, callback) | Decline an invitation |
RefreshInvitations() | Refresh the invitations list |
Example Usage
Basic Friend Operations
// Get the FriendManager instance
var friendManager = FriendManager.Instance;
// Send a friend request
friendManager.SendFriendRequest("user123", (success, error) => {
if (success) {
Debug.Log("Friend request sent!");
} else {
Debug.LogError($"Failed: {error}");
}
});
// Accept a request
friendManager.AcceptFriendRequest(requestId, (success, error) => {
// Handle result
});
// Remove a friend
friendManager.RemoveFriend(friendUserId, (success, error) => {
// Handle result
});
Event-Driven UI Updates
// In your UI controller Start method
private void Start() {
var fm = FriendManager.Instance;
fm.OnFriendAdded += OnFriendAdded;
fm.OnFriendRemoved += OnFriendRemoved;
fm.OnFriendStatusChanged += OnFriendStatusChanged;
fm.OnInvitationReceived += OnInvitationReceived;
}
private void OnFriendAdded(Friend friend) {
// Add friend to your UI list
Debug.Log($"Added friend: {friend.DisplayName}");
}
private void OnFriendRemoved(Friend friend) {
// Remove from UI list
Debug.Log($"Removed friend: {friend.DisplayName}");
}
private void OnFriendStatusChanged(Friend friend) {
// Update status display (Online/Offline/InRoom)
Debug.Log($"{friend.DisplayName} is now {friend.Status}");
}
private void OnInvitationReceived(FriendInvitation invitation) {
// Show invitation popup to player
Debug.Log($"Invite from: {invitation.FromUserId}");
}
Lifecycle Notes
Scene Persistence
FriendManager automatically makes itself persistent (DontDestroyOnLoad) to survive scene transitions. Only one instance should exist at a time.
Data Persistence
If PersistFriendList is enabled, the friend list is saved to PlayerPrefs between sessions. The block list similarly persists if PersistBlockList is enabled.
Automatic Refresh
When initialized, FriendManager automatically refreshes the friend list at intervals specified by RefreshInterval. This can be disabled by setting the interval to 0 or a negative value.