MIF_E31222344/Malukuu/Assets/Script/HideOnLost.cs

74 lines
1.7 KiB
C#

using UnityEngine;
using Vuforia;
using System.Collections;
public class HideOnLost : MonoBehaviour
{
private ObserverBehaviour observer;
private Coroutine hideCoroutine;
[SerializeField]
private float hideDelay = 0f; // Durasi delay sebelum menghilangkan objek saat marker hilang
private void Start()
{
observer = GetComponent<ObserverBehaviour>();
if (observer != null)
{
observer.OnTargetStatusChanged += OnStatusChanged;
}
else
{
Debug.LogError("ObserverBehaviour tidak ditemukan di " + gameObject.name);
}
}
private void OnDestroy()
{
if (observer != null)
{
observer.OnTargetStatusChanged -= OnStatusChanged;
}
}
private void OnStatusChanged(ObserverBehaviour behaviour, TargetStatus status)
{
Debug.Log($"{gameObject.name} status: {status.Status}");
if (status.Status == Status.TRACKED)
{
if (hideCoroutine != null)
{
StopCoroutine(hideCoroutine);
hideCoroutine = null;
}
SetChildrenActive(true);
}
else
{
// Mulai coroutine delay untuk menghilangkan objek
if (hideCoroutine == null)
{
hideCoroutine = StartCoroutine(HideAfterDelay(hideDelay));
}
}
}
private IEnumerator HideAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
SetChildrenActive(false);
hideCoroutine = null;
}
private void SetChildrenActive(bool active)
{
foreach (Transform child in transform)
{
child.gameObject.SetActive(active);
}
}
}