52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class MyCoroutine : MonoBehaviour
|
||
|
|
{
|
||
|
|
protected Coroutine m_Co_for1Sec, m_Co;
|
||
|
|
|
||
|
|
protected void Set_Repeat_for1sec(Action act)
|
||
|
|
{
|
||
|
|
Cancel_Repeat_forsec();
|
||
|
|
m_Co_for1Sec = StartCoroutine(Co_1sec(act));
|
||
|
|
}
|
||
|
|
IEnumerator Co_1sec(Action act)
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
yield return new WaitForSeconds(1f);
|
||
|
|
act();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
protected void Cancel_Repeat_forsec()
|
||
|
|
{
|
||
|
|
if (!DSUtil.CheckNull(m_Co_for1Sec))
|
||
|
|
{
|
||
|
|
StopCoroutine(m_Co_for1Sec);
|
||
|
|
m_Co_for1Sec = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected void Set_Coroutine(Action _act, float _time)
|
||
|
|
{
|
||
|
|
if (gameObject.activeInHierarchy)
|
||
|
|
StartCoroutine(Run_Coroutine(_act, _time));
|
||
|
|
}
|
||
|
|
|
||
|
|
IEnumerator Run_Coroutine(Action _act, float _time)
|
||
|
|
{
|
||
|
|
yield return new WaitForSeconds(_time);
|
||
|
|
_act?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
protected void Cancel_CurCoroutine() { if (!DSUtil.CheckNull(m_Co)) StopCoroutine(m_Co); }
|
||
|
|
protected void Set_Coroutine_withCancel(Action _act, float _time)
|
||
|
|
{
|
||
|
|
if (gameObject.activeInHierarchy)
|
||
|
|
{
|
||
|
|
Cancel_CurCoroutine();
|
||
|
|
m_Co = StartCoroutine(Run_Coroutine(_act, _time));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|