UIFriendList - jimdroberts/FishMMO GitHub Wiki
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.
-
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.
-
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, setsFriendID
, wires remove callback, sets name asynchronously viaClientNamingSystem
, 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.
- Add
UIFriendList
to a UI GameObject under a Canvas. - Assign
FriendParent
to the RectTransform that will hold friend entries (use a layout group for automatic layout). - Assign
FriendPrefab
to aUIFriend
prefab withName
,Status
, and a remove button wired toOnButtonRemoveFriend
. - Ensure the local
IPlayerCharacter
and itsIFriendController
are available so the control can subscribe and receive friend add/remove events.
var uiFriendList = GetComponent<UIFriendList>();
uiFriendList.FriendPrefab = friendEntryPrefab;
uiFriendList.FriendParent = friendListParent;
// UIFriendList subscribes to the friend controller and updates automatically.
- 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.