2022-05-24 01:59:50 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
2022-05-29 15:57:49 +02:00
|
|
|
[RequireComponent(typeof(AudioSource))]
|
2022-05-25 11:17:16 +02:00
|
|
|
public class PanelHiderButton : MonoBehaviour
|
2022-05-24 01:59:50 +02:00
|
|
|
{
|
|
|
|
private int colliderCount = 0;
|
|
|
|
private bool isLocked = false;
|
|
|
|
private float timer = 0f;
|
|
|
|
private bool actionTaken = false;
|
2022-05-25 11:17:16 +02:00
|
|
|
private Renderer r;
|
2022-05-24 01:59:50 +02:00
|
|
|
|
|
|
|
[Header("Settings")]
|
|
|
|
[SerializeField]
|
|
|
|
private float holdTime = 1f;
|
|
|
|
|
|
|
|
[Header("Components")]
|
|
|
|
[SerializeField]
|
|
|
|
private RawImage statusImg;
|
|
|
|
[SerializeField]
|
|
|
|
private Image timerRing;
|
|
|
|
[SerializeField]
|
2022-05-24 03:25:05 +02:00
|
|
|
private List<GameObject> panelButtons;
|
2022-05-24 01:59:50 +02:00
|
|
|
|
|
|
|
[Header("Assets")]
|
|
|
|
[SerializeField]
|
|
|
|
private Texture lockImg;
|
|
|
|
[SerializeField]
|
|
|
|
private Texture unlockImg;
|
|
|
|
[SerializeField]
|
|
|
|
private AudioClip lockSound;
|
|
|
|
[SerializeField]
|
|
|
|
private AudioClip unlockSound;
|
|
|
|
|
2022-05-29 15:55:37 +02:00
|
|
|
private AudioSource audioSrc;
|
2022-05-24 01:59:50 +02:00
|
|
|
private void Start()
|
|
|
|
{
|
2022-05-29 15:55:37 +02:00
|
|
|
audioSrc = GetComponent<AudioSource>();
|
2022-05-25 11:17:16 +02:00
|
|
|
r = GetComponent<Renderer>();
|
2022-05-24 09:21:47 +02:00
|
|
|
statusImg.texture = isLocked ? lockImg : unlockImg;
|
2022-05-24 01:59:50 +02:00
|
|
|
audioSrc.clip = lockSound;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerEnter(Collider _)
|
|
|
|
{
|
2022-05-25 11:17:16 +02:00
|
|
|
r.material.color = Color.white;
|
2022-05-24 01:59:50 +02:00
|
|
|
++colliderCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerExit(Collider _)
|
|
|
|
{
|
2022-05-25 11:17:16 +02:00
|
|
|
r.material.color = Color.gray;
|
2022-05-24 01:59:50 +02:00
|
|
|
colliderCount = Mathf.Clamp(colliderCount - 1, 0, colliderCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
if (colliderCount >= 1)
|
|
|
|
{
|
|
|
|
timer += Time.unscaledDeltaTime;
|
|
|
|
float ratio = Mathf.Clamp(timer, 0, holdTime) / holdTime;
|
|
|
|
|
|
|
|
timerRing.fillAmount = Mathf.Pow(ratio, 3f);
|
|
|
|
|
|
|
|
if (ratio >= 1 && !actionTaken)
|
|
|
|
{
|
|
|
|
isLocked = !isLocked;
|
2022-05-24 03:25:05 +02:00
|
|
|
|
2022-05-24 01:59:50 +02:00
|
|
|
foreach (var btn in panelButtons)
|
|
|
|
{
|
2022-05-24 03:25:05 +02:00
|
|
|
btn.SetActive(!isLocked);
|
2022-05-24 01:59:50 +02:00
|
|
|
}
|
|
|
|
actionTaken = true;
|
2022-05-24 09:21:47 +02:00
|
|
|
|
|
|
|
timerRing.color = Color.cyan;
|
|
|
|
statusImg.texture = isLocked ? lockImg : unlockImg;
|
|
|
|
|
|
|
|
audioSrc.clip = isLocked ? lockSound : unlockSound;
|
|
|
|
audioSrc.Play();
|
2022-05-24 01:59:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timer = 0;
|
|
|
|
timerRing.fillAmount = 0;
|
|
|
|
timerRing.color = Color.white;
|
|
|
|
actionTaken = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|