/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System.Linq; using UnityEngine; using Object = UnityEngine.Object; namespace OmiLAXR { /// /// Base class for all components that participate in the OmiLAXR pipeline system. /// Provides common utility methods for finding Unity objects in a version-compatible way. /// public abstract class PipelineComponent : MonoBehaviour, IPipelineComponent { protected virtual void OnEnable() { ApplicationShutdownManager.Register(this); } /// /// Finds the first object of the specified type in the scene. /// Handles different Unity versions by using the appropriate API calls. /// /// Whether to include inactive GameObjects in the search /// Type of object to find /// The first object of the specified type, or null if none exists protected static T FindObject(bool includeInactive = false) where T : Object #if UNITY_2023_1_OR_NEWER => FindFirstObjectByType(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude); #else => FindObjectOfType(includeInactive); #endif /// /// Finds all objects of the specified type in the scene. /// Handles different Unity versions by using the appropriate API calls. /// Provides filtering for inactive objects based on the includeInactive parameter. /// /// Whether to include inactive GameObjects in the search /// Type of objects to find /// Array of objects of the specified type protected static T[] FindObjects(bool includeInactive = false) where T : Object { #if UNITY_2023_1_OR_NEWER return FindObjectsByType( includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, FindObjectsSortMode.None ); #else return FindObjectsOfType() .Where(o => { if (!o) return false; if (includeInactive) return true; if (o is GameObject go) return go.activeSelf; if (o is Component c) return c.gameObject.activeSelf; return true; // fallback if neither GameObject nor Component }) .ToArray(); #endif } /// /// Implements a lazy-loading singleton pattern that's compatible across Unity versions. /// Returns the existing instance if available, otherwise finds it in the scene. /// /// Reference to the cached instance variable /// Type of the singleton instance /// The singleton instance protected static T GetInstance(ref T instance) where T : Object { #if UNITY_2023_1_OR_NEWER return instance ??= FindFirstObjectByType(); #else if (instance == null) instance = FindObjectOfType(); return instance; #endif } protected virtual void OnAppQuit() { // do nothing } } }