You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.4 KiB
62 lines
1.4 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
public class Timer : MonoBehaviour |
|
{ |
|
public Image imgGauage; |
|
public Text txtCount; |
|
[Space(10)] |
|
public float limit; |
|
public bool isPlaying = false; |
|
float timeSum = 0; |
|
int timeSum2 = 0; |
|
[Range(0, 1f)] |
|
public float timeNext = 0.1f; |
|
System.Action OnEnd; |
|
public void OnInitialized(float limit, System.Action OnEnd) |
|
{ |
|
this.limit = limit; |
|
this.OnEnd = OnEnd; |
|
|
|
secBefore = 0; |
|
timeSum = 0; |
|
timeSum2 = 0; |
|
|
|
imgGauage.fillAmount = 1f; |
|
txtCount.text = $"{(int)limit}"; |
|
|
|
isPlaying = true; |
|
InvokeRepeating("Elapse", timeNext, timeNext); |
|
} |
|
|
|
int secBefore = 0; |
|
[Space(10)] |
|
public SoundPlayer soundClock; |
|
void Elapse() |
|
{ |
|
if (!gameObject.activeInHierarchy) |
|
CancelInvoke(); |
|
|
|
timeSum += timeNext; |
|
timeSum2 = (int)timeSum; |
|
imgGauage.fillAmount = 1 - (timeSum - timeSum2); |
|
txtCount.text = $"{(int)(limit - timeSum2)}"; |
|
|
|
if (secBefore != (int)(limit - timeSum2)) |
|
{ |
|
SoundManager.Instance.EffectPlay(soundClock.gameObject); |
|
secBefore = (int)(limit - timeSum2); |
|
} |
|
|
|
if (limit < timeSum) |
|
{ |
|
isPlaying = false; |
|
CancelInvoke(); |
|
|
|
if (OnEnd != null) |
|
OnEnd(); |
|
} |
|
} |
|
}
|
|
|