WorldDayNightCycle - jimdroberts/FishMMO GitHub Wiki

Description

Controls the day and night cycle in the game world, including skybox transitions, object activations, rotations, and fading effects. Manages the timing and visual state of day and night, and provides hooks for fog and environmental changes.


API Access

Fields

  • public RegionChangeFogAction SceneFog

    Delegate for triggering fog changes when the scene loads or transitions.

  • public bool DayNightCycle

    True if the day night cycle should run. False if not.

  • public int DayCycleDuration

    The duration of the day cycle in seconds.

  • public int NightCycleDuration

    The duration of the night cycle in seconds.

  • public Material DaySkyboxMaterial

    The skybox material used during the day cycle.

  • public Material NightSkyBoxMaterial

    The skybox material used during the night cycle.

  • public List RotateObjects

    Objects that will rotate constantly with the day night cycle.

  • public List DayObjects

    Objects enabled during the day and disabled at night.

  • public List NightObjects

    Objects enabled during the night and disabled during the day.

  • public float FadeThreshold

    The time in seconds that objects will take to fade in or out.

  • public List DayFadeObjects

    Objects that will fade away during the day.

  • public List NightFadeObjects

    Objects that will fade away at night.

  • private bool isDaytime

    True if it's currently day time in the game world. Used to determine which objects and effects should be active.

  • private float fadeTime

    The current fade time.

  • private float lastRotationAngle

    Tracks the last applied rotation angle for objects affected by the day/night cycle.

Methods

  • private void Awake()

    Unity Awake callback. Initializes the day/night cycle, sets the initial skybox, and triggers fog if needed.

  • void Update()

    Unity Update callback. Advances the day/night cycle, updates object states, rotations, and fading each frame.

  • private float GetGameTimeOfDay(DateTime now)

    Gets the current game time of day in seconds. Parameters:

    • DateTime now: The current UTC time. Returns: float (seconds into the current day/night cycle)
  • private void UpdateDayNightState(float currentGameTimeOfDay, bool ignoreCurrentState = false)

    Updates the day night state. Switches between day and night, activates/deactivates objects, and resets fade time. Parameters:

    • float currentGameTimeOfDay: The current time in the cycle.
    • bool ignoreCurrentState: If true, forces state update.
  • private void UpdateDayNightRotation(float currentGameTimeOfDay, List objects)

    Rotates objects based on the current game time of day and lerps the skybox between day and night materials. Parameters:

    • float currentGameTimeOfDay: The current time in the cycle.
    • List objects: Objects to rotate.
  • private void UpdateDayNightActivations(bool enable, List objects)

    Enables or disables all GameObjects in the provided list based on the 'enable' parameter. Parameters:

    • bool enable: True to enable objects, false to disable.
    • List objects: List of GameObjects to activate/deactivate.
  • private void UpdateDayNightFading(float gameTimeOfDay, List dayFadeObjects, List nightFadeObjects)

    Fade objects in or out based on day/night status. Parameters:

    • float gameTimeOfDay: The current time in the cycle.
    • List dayFadeObjects: Objects to fade during the day.
    • List nightFadeObjects: Objects to fade during the night.
  • private void SetAlpha(List objects, float alpha)

    Sets the alpha of the materials of objects in the list. Parameters:

    • List objects: Objects to modify.
    • float alpha: Target alpha value.

Basic Usage

Setup

  1. Attach this script to a GameObject in your scene.
  2. Assign skybox materials, object lists, and configure durations and fade thresholds in the Inspector.
  3. Optionally, assign a delegate to SceneFog for fog transitions.
  4. Ensure all referenced GameObjects and materials are present in the scene.

Example

// Example 1: Setting up the WorldDayNightCycle in a scene
// Attach WorldDayNightCycle to a GameObject.
// Assign DaySkyboxMaterial and NightSkyBoxMaterial in the Inspector.
// Populate RotateObjects, DayObjects, NightObjects, DayFadeObjects, and NightFadeObjects as needed.
// Set DayCycleDuration and NightCycleDuration for desired cycle lengths.
// Optionally, assign a SceneFog delegate for fog transitions.

// Example 2: Accessing day/night state in another script
if (worldDayNightCycleInstance.DayNightCycle) {
    // The cycle is running; check isDaytime (private) via a public method or event if needed.
}

Best Practices

  • Use realistic cycle durations to match your game's pacing and atmosphere.
  • Keep object lists organized and avoid null references.
  • Use fade transitions for smooth visual changes between day and night.
  • Assign skybox materials that visually represent day and night for better immersion.
  • Use the SceneFog delegate to synchronize environmental effects with the day/night cycle.
⚠️ **GitHub.com Fallback** ⚠️