using System; using System.Collections.Generic; using UnityEngine; using OHM.UnityToolkit.Pooling; using UnityEngine.Serialization; namespace Ambiance { public class AmbianceShow : AmbianceSpecialBase { [Serializable] public class OptionalObject { public Transform position; public PooledObject objectToInstantiate; } [SerializeField] private string id; [SerializeField] [FormerlySerializedAs("randomShow")] private bool _randomShow; [SerializeField] [Tooltip("position, prefab")] [FormerlySerializedAs("objectsList")] private List _objectsList; public override void OnStart() { base.OnStart(); AmbianceController.Instance.OnAmbianceChange.AddListener(Apply); } public override void Apply() { if (GetComponent() == null && AmbianceController.Instance == null) { return; } AmbianceDef ambiance = AmbianceController.Instance.CurrentAmbianceToUse; if (ambiance == null) { return; } bool show = ambiance.showElementsIds.Contains(id); DisableObject(); if (!show) { return; } int index = _randomShow ? UnityEngine.Random.Range(0, _objectsList.Count) : 0; if (_objectsList.Count == 0) { return; } OptionalObject slot = _objectsList[index]; if (slot == null || slot.objectToInstantiate == null || slot.position == null) { return; } PoolsManager.InstantiatePrefab(slot.objectToInstantiate, slot.position, slot.position.position, slot.position.rotation, startActive: true); } private void DisableObject() { for (int i = 0; i < base.transform.childCount; i++) { base.transform.GetChild(i).gameObject.SetActive(false); } } } }