WXMC/.svn/pristine/a8/a8ce54846c113d24d510a6f4773b23921b6550bc.svn-base

65 lines
2.0 KiB
Plaintext
Raw Normal View History

2024-12-04 16:18:46 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UI.Tables;
using System.Collections.Generic;
using System.Linq;
namespace UI.Dates
{
public class DatePickerTimerComponent : MonoBehaviour
{
public List<DelayedAction> delayedActions = new List<DelayedAction>();
public void DelayedCall(float delay, Action action, MonoBehaviour target, bool forceEvenIfTargetIsInactive)
{
this.enabled = true;
delayedActions.Add(new DelayedAction { timeToExecute = Time.unscaledTime + delay, action = action, target = target, forceEvenIfTargetIsInactive = forceEvenIfTargetIsInactive });
}
private void Update()
{
List<DelayedAction> actionsToExecute = null;
foreach (var action in delayedActions)
{
if (Time.unscaledTime >= action.timeToExecute)
{
if (actionsToExecute == null) actionsToExecute = new List<DelayedAction>();
actionsToExecute.Add(action);
}
}
if (actionsToExecute == null || actionsToExecute.Count == 0) return;
foreach (var action in actionsToExecute)
{
try
{
if ((action.forceEvenIfTargetIsInactive)
|| (action.target != null && action.target.gameObject.activeInHierarchy))
{
action.action.Invoke();
}
}
finally
{
delayedActions.Remove(action);
}
}
// stop calling update if we have nothing scheduled (DelayedCall will re-enable this)
if (delayedActions.Count == 0) this.enabled = false;
}
}
public class DelayedAction
{
public float timeToExecute;
public Action action;
public MonoBehaviour target;
public bool forceEvenIfTargetIsInactive;
}
}