WorldSceneSettings - jimdroberts/FishMMO GitHub Wiki

Description

MonoBehaviour for configuring world scene settings, including client limits, transition visuals, day/night cycle, and object activations.


API Access

Fields

  • public int MaxClients

    The maximum number of clients allowed in this scene.

  • public Sprite SceneTransitionImage

    The image that will be displayed when entering this scene.

  • public RegionChangeFogAction DefaultSceneFog

    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

    Returns true if it's currently day time.

  • 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 DefaultSceneFog for fog transitions.
  4. Ensure all referenced GameObjects and materials are present in the scene.

Example

// Example 1: Setting up the WorldSceneSettings in a scene
// Attach WorldSceneSettings 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 DefaultSceneFog delegate for fog transitions.

// Example 2: Accessing day/night state in another script
if (worldSceneSettingsInstance.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 DefaultSceneFog delegate to synchronize environmental effects with the day/night cycle.
⚠️ **GitHub.com Fallback** ⚠️