挂载到场景中的空对象GameManager上就能用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LongTimeNoOperation : MonoBehaviour
{
//检测时间间隔
public float timeperiod = 20;
//上次操作时间
public float lastOperationTime;
//当前时间
float nowTime;
//无操作时间
float noOperationTime;
void Start()
{
//初始化
lastOperationTime = Time.time;
}
void Update()
{
//当前时间
nowTime = Time.time;
//检测到有操作,更新时间
if (Input.GetMouseButtonDown(0))
{
//更新时间
lastOperationTime = nowTime;
}
//更新无操作时间
noOperationTime = Mathf.Abs(nowTime - lastOperationTime);
//达到设定到时间间隔,执行操作
if (noOperationTime > timeperiod)
{
//执行操作
UIManager.instance.AutoTip();
//更新时间
lastOperationTime = nowTime;
}
}
}
|