UIPetControl - jimdroberts/FishMMO GitHub Wiki
UIPetControl manages the UI elements and interactions for a player's pet in the FishMMO client. It displays the pet's name and health, subscribes to pet events, and provides UI controls for pet commands such as follow, stay, summon, and release.
-
public TMP_Text PetNameLabel
The label displaying the pet's name.
-
public Slider PetHealth
The slider displaying the pet's health.
-
public override void OnPostSetCharacter()
Called after the character is set. Subscribes to pet controller events and updates pet UI.
-
public override void OnPreUnsetCharacter()
Called before the character is unset. Unsubscribes from pet controller events and resets pet UI.
-
public void PetController_OnPetSummoned(Pet pet)
Handles pet summoned event. Updates pet UI and makes it visible. Parameters: Pet pet – The summoned pet.
-
public void PetController_OnPetDestroyed()
Handles pet destroyed event. Hides the pet UI.
-
private bool HasPet()
Returns true if the character has a pet. Returns: bool – True if pet exists, false otherwise.
-
public void OnFollowPet()
Sends a follow command to the pet.
-
public void OnStayPet()
Sends a stay command to the pet.
-
public void OnSummonPet()
Sends a summon command to the pet.
-
public void OnReleasePet()
Sends a release command to the pet.
- Ensure the UI GameObject has a UIPetControl component attached.
- Assign the PetNameLabel and PetHealth fields in the Inspector.
- The character must implement IPetController and be set on this control.
- The component subscribes to pet events automatically on character set.
// Example: Setting up UIPetControl in a scene
// 1. Attach UIPetControl to a UI GameObject.
// 2. Assign the PetNameLabel and PetHealth references in the Inspector.
// 3. Set the character implementing IPetController.
UIPetControl petControl = GetComponent<UIPetControl>();
petControl.PetNameLabel = ...; // Assign TMP_Text
petControl.PetHealth = ...; // Assign Slider
petControl.SetCharacter(playerCharacter); // Set character
- Always assign PetNameLabel and PetHealth in the Inspector to avoid null references.
- Ensure the character implements IPetController for full functionality.
- Unsubscribe from events in OnPreUnsetCharacter to prevent memory leaks.
- Use the provided OnFollowPet, OnStayPet, OnSummonPet, and OnReleasePet methods for UI button actions.