using System.Linq; using UnityEngine; namespace Crosstales.Common.Util { /// Enables or disable game objects and scripts for a given platform. public class PlatformController : MonoBehaviour { #region Variables ///Selected platforms for the controller. [Header("Configuration")] [Tooltip("Selected platforms for the controller.")] public System.Collections.Generic.List Platforms; ///Enable or disable the 'Objects' for the selected 'Platforms' (default: true). [Tooltip("Enable or disable the 'Objects' for the selected 'Platforms' (default: true).")] public bool Active = true; ///Selected objects for the controller. [Header("GameObjects")] [Tooltip("Selected objects for the controller.")] public GameObject[] Objects; ///Selected scripts for the controller. [Header("MonoBehaviour Scripts")] [Tooltip("Selected scripts for the controller.")] public MonoBehaviour[] Scripts; protected Crosstales.Common.Model.Enum.Platform _currentPlatform; #endregion #region MonoBehaviour methods protected virtual void Awake() { if (enabled) selectPlatform(); } private void Start() { //do nothing, just allow to enable/disable the script } #endregion #region Private methods protected void selectPlatform() { _currentPlatform = Crosstales.Common.Util.BaseHelper.CurrentPlatform; activateGameObjects(); activateScripts(); } protected void activateGameObjects() { if (Objects?.Length > 0) { bool active = Platforms.Contains(_currentPlatform) ? Active : !Active; foreach (GameObject go in Objects.Where(go => go != null)) { go.SetActive(active); } } } protected void activateScripts() { if (Scripts?.Length > 0) { bool active = Platforms.Contains(_currentPlatform) ? Active : !Active; foreach (MonoBehaviour script in Scripts.Where(script => script != null)) { script.enabled = active; } } } #endregion } } // © 2017-2023 crosstales LLC (https://www.crosstales.com)