/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System.ComponentModel; using OmiLAXR.Components; using UnityEngine; namespace OmiLAXR.TrackingBehaviours.System { /// /// Monitors GameObject state changes such as destruction, enabling, and disabling. /// Works in conjunction with GameObjectStateWatcher components to provide real-time notifications /// of GameObject lifecycle events throughout the application. /// [AddComponentMenu("OmiLAXR / 3) Tracking Behaviours / Game Objects State Tracking Behaviour")] [Description("Tracks if a game object state is changed (e.g. is destroyed).")] public class GameObjectStateTrackingBehaviour : TrackingBehaviour { /// /// Enable tracking of GameObject destruction events. /// When true, OnDestroyedGameObject events will be triggered when watched objects are destroyed. /// public bool watchOnDestroyed = true; /// /// Enable tracking of GameObject enable events. /// When true, OnEnabledGameObject events will be triggered when watched objects become active. /// public bool watchOnEnabled = true; /// /// Enable tracking of GameObject disable events. /// When true, OnDisabledGameObject events will be triggered when watched objects become inactive. /// public bool watchOnDisabled = true; /// /// Event triggered when a watched GameObject is destroyed. /// Provides both the GameObjectStateWatcher and the destroyed GameObject reference. /// public readonly TrackingBehaviourEvent OnDestroyedGameObject = new TrackingBehaviourEvent(); /// /// Event triggered when a watched GameObject becomes enabled/active. /// Includes the watcher component and the newly enabled GameObject. /// public readonly TrackingBehaviourEvent OnEnabledGameObject = new TrackingBehaviourEvent(); /// /// Event triggered when a watched GameObject becomes disabled/inactive. /// Provides access to both the watcher and the disabled GameObject. /// public readonly TrackingBehaviourEvent OnDisabledGameObject = new TrackingBehaviourEvent(); /// /// Called after GameObjectStateWatcher components are filtered and ready for monitoring. /// Sets up event bindings based on the enabled tracking options. /// /// Array of GameObjectStateWatcher components to monitor protected override void AfterFilteredObjects(GameObjectStateWatcher[] gameObjects) { // Set up bindings for each detected GameObject state watcher foreach (var go in gameObjects) { // Bind to destruction events if enabled if (watchOnDestroyed) { OnDestroyedGameObject.Bind(go.onDestroyed, g => { // Forward the destruction event with watcher and GameObject references OnDestroyedGameObject.Invoke(this, go, g); }); } // Bind to enable events if enabled if (watchOnEnabled) { OnEnabledGameObject.Bind(go.onEnabled, g => { // Forward the enable event with context OnEnabledGameObject.Invoke(this, go, g); }); } // Bind to disable events if enabled if (watchOnDisabled) { OnDisabledGameObject.Bind(go.onDisabled, g => { // Forward the disable event with context OnDisabledGameObject.Invoke(this, go, g); }); } } } } }