Region - jimdroberts/FishMMO GitHub Wiki

Description

Region is a networked MonoBehaviour for FishMMO that represents a region in the game world. It manages region hierarchy, collider setup, and triggers region actions when players enter, stay, or exit. Regions can be nested, support custom actions, and integrate with Unity's terrain and collider systems for flexible area definitions.


API Access

Fields

  • public Region Parent

    The parent region in the hierarchy. Used for nested region logic.

  • [NonSerialized] public List Children

    The child regions nested under this region. Managed at runtime.

  • public string Name { get; }

    The name of the region, taken from the GameObject's name.

  • public Collider Collider

    The collider that defines the region's bounds.

  • public Terrain Terrain

    Optional terrain reference. If set, overrides collider bounds to match terrain size (requires BoxCollider).

  • public List OnRegionEnter

    Actions to invoke when a player enters the region.

  • public List OnRegionStay

    Actions to invoke while a player stays in the region.

  • public List OnRegionExit

    Actions to invoke when a player exits the region.

  • private NetworkTrigger networkTrigger

    The NetworkTrigger component used to detect player entry, stay, and exit events.

#if UNITY_EDITOR

  • public Color GizmoColor

    The color used to draw the region's gizmo in the editor. #endif

Properties

  • public string Name { get; }

    The name of the region, taken from the GameObject's name.

Methods

  • void Awake()

    Initializes the region, sets up collider, terrain bounds, and event handlers for network triggers.

#if UNITY_EDITOR

  • void OnDrawGizmos()

    Draws the region's collider gizmo in the editor for visualization. #endif

  • private void NetworkCollider_OnEnter(Collider other)

    Handles logic when a player enters the region's collider. Children regions take priority over parents. Parameters: - Collider other: The collider of the entering object. Returns: void. No exceptions are thrown.

  • private void NetworkCollider_OnStay(Collider other)

    Handles logic while a player stays within the region's collider. Parameters: - Collider other: The collider of the staying object. Returns: void. No exceptions are thrown.

  • private void NetworkCollider_OnExit(Collider other)

    Handles logic when a player exits the region's collider. Parameters: - Collider other: The collider of the exiting object. Returns: void. No exceptions are thrown.


Basic Usage

Setup

  1. Add the Region component to a GameObject in your scene.
  2. Assign a Collider (and optionally a Terrain) to define the region's bounds.
  3. Set up region hierarchy by assigning Parent and managing Children as needed.
  4. Add RegionAction assets to OnRegionEnter, OnRegionStay, and OnRegionExit lists to define region behaviors.

Example

// Example 1: Setting up a region in the Unity Editor
// 1. Add a Region component to a GameObject.
// 2. Assign a Collider and configure its bounds.
// 3. Optionally assign a Terrain and set up Parent/Children relationships.
// 4. Add RegionAction assets to the OnRegionEnter, OnRegionStay, and OnRegionExit lists.

// Example 2: Accessing region actions in code
region.OnRegionEnter.Add(customRegionAction);
region.OnRegionExit.Add(anotherRegionAction);

Best Practices

  • Use nested regions for complex area logic, ensuring children take priority over parents.
  • Always assign a Collider and set it as a trigger for proper region detection.
  • Use RegionAction assets to modularize and reuse region behaviors.
  • Document region purposes and action lists for maintainability.
⚠️ **GitHub.com Fallback** ⚠️