UIFriendList - jimdroberts/FishMMO GitHub Wiki

Description

A UI control that manages the player's friend list UI. It instantiates and updates UIFriend entries, subscribes to the friend controller events, and handles adding/removing friends with confirmation dialogs and server broadcasts.


API Access

Fields

  • public RectTransform FriendParent

    Parent RectTransform used to contain instantiated friend entries (e.g., a list container).

  • public UIFriend FriendPrefab

    Prefab used to create individual friend entries (UIFriend).

  • public Dictionary<long, UIFriend> Friends

    Mapping of friend character ID -> instantiated UIFriend entry. Managed by this control.

Methods

  • public override void OnDestroying()

    Called when the UI is destroyed. Cleans up instantiated friend entries, clears delegates, and empties the dictionary.

  • public override void OnPostSetCharacter()

    Called after the character reference is set. Subscribes to the IFriendController events (OnAddFriend, OnRemoveFriend).

  • public override void OnPreUnsetCharacter()

    Called before the character reference is unset. Unsubscribes from friend controller events.

  • public void FriendController_OnAddFriend(long friendID, bool online)

    Handler for adding/updating a friend entry. Instantiates UIFriend if missing, sets FriendID, wires remove callback, sets name asynchronously via ClientNamingSystem, and updates online status text.

  • public void FriendController_OnRemoveFriend(long friendID)

    Handler for removing a friend entry. Removes from dictionary, clears delegates, and destroys the entry GameObject.

  • private void OnButtonRemoveFriend(long friendID)

    Invoked when the UI remove action is triggered. Opens a confirmation dialog and broadcasts a FriendRemoveBroadcast to the server if confirmed.

  • public void OnButtonAddFriend()

    Opens a name input dialog, validates the name, resolves the character ID, and broadcasts a FriendAddNewBroadcast to request adding the friend.


Basic Usage

Setup

  1. Add UIFriendList to a UI GameObject under a Canvas.
  2. Assign FriendParent to the RectTransform that will hold friend entries (use a layout group for automatic layout).
  3. Assign FriendPrefab to a UIFriend prefab with Name, Status, and a remove button wired to OnButtonRemoveFriend.
  4. Ensure the local IPlayerCharacter and its IFriendController are available so the control can subscribe and receive friend add/remove events.

Example

var uiFriendList = GetComponent<UIFriendList>();
uiFriendList.FriendPrefab = friendEntryPrefab;
uiFriendList.FriendParent = friendListParent;
// UIFriendList subscribes to the friend controller and updates automatically.

Best Practices

  • Use a layout group (VerticalLayoutGroup + ContentSizeFitter) on FriendParent to keep entries ordered and sized correctly.
  • Pool UIFriend instances if the friend list changes frequently to reduce allocations and noise in the GC.
  • Keep UI update callbacks lightweight; name resolution is asynchronous—avoid heavy work in the callback.
  • Validate inputs from the add-friend dialog and show clear error messages via UIChat or dialogs.
  • Clear delegates on destroyed entries to avoid memory leaks and dangling callbacks.
⚠️ **GitHub.com Fallback** ⚠️