67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GradualActivator : MonoBehaviour
|
|
{
|
|
[Header("Parent που έχει τα children")]
|
|
[SerializeField] private GameObject parentObject;
|
|
|
|
[Header("Delay ανάμεσα στις ενεργοποιήσεις")]
|
|
[SerializeField] private float delayBetweenActivations = 0.02f;
|
|
|
|
[Header("Θυμήσου ποια ήταν ενεργά στην αρχή;")]
|
|
[SerializeField] private bool onlyInitiallyEnabledChildren = true;
|
|
|
|
private List<GameObject> allChildren = new List<GameObject>();
|
|
private Dictionary<GameObject, bool> initialActiveStates = new Dictionary<GameObject, bool>();
|
|
|
|
private void Awake()
|
|
{
|
|
if (parentObject == null)
|
|
{
|
|
Debug.LogError("Δεν έχεις βάλει Parent Object στο GradualActivator!");
|
|
return;
|
|
}
|
|
|
|
foreach (Transform child in parentObject.transform)
|
|
{
|
|
allChildren.Add(child.gameObject);
|
|
initialActiveStates[child.gameObject] = child.gameObject.activeSelf;
|
|
}
|
|
|
|
//Debug.Log($"[GradualActivator] Βρέθηκαν {allChildren.Count} children.");
|
|
}
|
|
|
|
public void GraduallyActivate()
|
|
{
|
|
if (!parentObject.activeSelf)
|
|
{
|
|
//Debug.Log("[GradualActivator] Parent είναι κλειστό, το ενεργοποιώ πρώτα.");
|
|
parentObject.SetActive(true);
|
|
}
|
|
StartCoroutine(GradualSetActive(true));
|
|
}
|
|
|
|
public void GraduallyDeactivate()
|
|
{
|
|
StartCoroutine(GradualSetActive(false));
|
|
}
|
|
|
|
private IEnumerator GradualSetActive(bool activate)
|
|
{
|
|
foreach (var obj in allChildren)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
if (!onlyInitiallyEnabledChildren || initialActiveStates[obj])
|
|
{
|
|
obj.SetActive(activate);
|
|
//Debug.Log($"[GradualActivator] {(activate ? "Enabled" : "Disabled")} {obj.name}");
|
|
yield return new WaitForSeconds(delayBetweenActivations);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|