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

FieldTypeDefaultDescription
_backendTypeNetworkBackendTypePhotonPUNWhich network backend to use
_useNativeImplementationbooltrueUse native friends API or custom server
_persistFriendListbooltrueSave friend list between sessions
_persistBlockListbooltrueSave blocked users between sessions
_refreshIntervalfloat30Seconds between friend list refreshes
_maxFriendsint100Maximum number of friends allowed
_maxBlockedUsersint100Maximum blocked users allowed
_defaultGameTypeGameTypeRoomBasedDefault game type for invitations
_autoInitializeGameTypebooltrueAuto-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:

EventPayloadWhen It Fires
_onFriendListUpdatedList<Friend>Friend list is refreshed or changed
_onFriendRequestReceivedFriendRequestA new incoming friend request arrives
_onFriendRequestAcceptedFriendRequestYour friend request was accepted
_onFriendRequestRejectedFriendRequestYour friend request was rejected
_onFriendRequestCancelledFriendRequestRequest sender cancelled
_onFriendStatusChangedFriend, FriendStatusFriend's online status changed
_onFriendAddedFriendA new friend was successfully added
_onFriendRemovedFriendA friend was removed from the list
_onUserBlockedBlockEntryA user was successfully blocked
_onUserUnblockedstring (userId)A user was removed from block list
_onInvitationReceivedFriendInvitationA game invitation was received

Runtime Properties (Read-Only)

PropertyTypeDescription
CurrentUserIdstringThe ID of the currently logged-in user
FriendsIReadOnlyList<Friend>Current list of friends
PendingRequestsIReadOnlyList<FriendRequest>Pending friend requests
BlockedUsersIReadOnlyList<BlockEntry>Blocked users list
BackendTypeNetworkBackendTypeCurrently used backend
IsUsingNativeImplementationboolWhether using native or custom
IsInitializedboolWhether system is ready
CurrentGameTypeHandlerIGameTypeHandlerActive game type handler
DefaultGameTypeGameTypeDefault 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:

EventSignatureDescription
OnFriendsListUpdatedAction<IReadOnlyList<Friend>>Fired when friend list changes
OnFriendRequestReceivedAction<FriendRequest>New incoming request
OnFriendRequestAcceptedAction<Friend>Request was accepted
OnFriendRequestRejectedAction<string>Request rejected (userId)
OnFriendRequestCancelledAction<FriendRequest>Request cancelled
OnFriendStatusChangedAction<Friend>Friend status changed
OnFriendAddedAction<Friend>Friend added
OnFriendRemovedAction<Friend>Friend removed
OnUserBlockedAction<BlockEntry>User blocked
OnUserUnblockedAction<string>User unblocked
OnInvitationReceivedAction<FriendInvitation>Invitation received
OnPendingRequestsRefreshedAction<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

MethodDescription
ApplySettings(FriendManagerSettings)Apply settings from a ScriptableObject asset
Initialize(string userId)Initialize with a specific user ID

Friend Operations

MethodDescription
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

MethodDescription
BlockUser(userId, callback)Block a user
UnblockUser(userId, callback)Remove a user from the block list
RefreshBlockedUsers()Refresh the block list

Invitation Operations

MethodDescription
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.