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 Model.Enum.Platform currentPlatform; #endregion #region MonoBehaviour methods protected virtual void Awake() { selectPlatform(); } #endregion #region Private methods protected void selectPlatform() { currentPlatform = 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-2020 crosstales LLC (https://www.crosstales.com)