unity——长时间不操作检测

一、协程控制自动执行无操作监测(StartCoroutine("AutoCheck");)

1、使用协程控制程序自动执行判断是否进行无操作监测;

2、优化布尔值设置,将基于Update执行的持续性监测程序可通过外接控制;

既可根据手动设置布尔值,也可根据控制协程来确定是否执行无操作监测;

3、不同平台下,自定义监测那些操作

if (Application.isEditor),if (Input.GetMouseButtonDown(0)),编辑器下鼠标左键点击操作

if (Application.isMobilePlatform),if(Input.touchCount > 0),移动端触屏操作

参考自:Unity 长时间无操作检测

原文通过布尔值判断和Update监测,并根据不同平台判断不同操作,来实现程序是否一定时间内无操作。 

using System.Collections;
using UnityEngine;

/// <summary>
/// 长时间不操作检测,方法1
/// 改编自,原文链接:https://blog.csdn.net/baidu_39447417/article/details/105446393
/// </summary>
public class LongTimeNoOperation : MonoBehaviour
{
    public bool isAllowCheck = false;//是否允许无操作检测
    public bool check = false;//是否无操作检测
    public float maxTimeOffset = 30;//检测时间间隔
    public float lasterTime;//上次的时间
    public float nowTime;//当前时间

    /// <summary>
    /// 执行无操作监测
    /// </summary>
    public IEnumerator AutoCheck()
    {
        yield return new WaitUntil(() => isAllowCheck);
        lasterTime = Time.time; //设置初始时间
        while (true)
        {
            //等到允许无操作监测时,且之前的无操作监测结束时
            yield return new WaitUntil(() => !check && isAllowCheck);
            //开启新一轮无操作监测
            check = true;//开启无操作监测
        }
    }

    private void OnEnable()
    {
        StartCoroutine("AutoCheck");
    }

    public void Update()
    {
        if (check) CheckOperate();
    }

    /// <summary>
    /// 进行无操作监测
    /// </summary>
    public void CheckOperate()
    {
        //正在该轮操作监测中,停止开启新一轮操作监测
        isAllowCheck = false;
        //当前时间
        nowTime = Time.time;

        //如果有操作则更新上次操作时间为此时
        if (Application.isEditor)//在编辑器环境下
        {
            //若点击鼠标左键/有操作,则更新触摸时间
            if (Input.GetMouseButtonDown(0)) lasterTime = nowTime;
        }
        //非编辑器环境下,触屏操作
        //Input.touchCount在pc端没用,只在移动端生效
        //Application.isMobilePlatform在pc和移动端都生效
        else if (Application.isMobilePlatform)
        {
            //有屏幕手指 接触
            if (Input.touchCount > 0) lasterTime = nowTime;//更新触摸时间
        }

        //判断无操作时间是否达到指定时长,若达到指定时长无操作,则执行TakeOperate
        float offsetTime = Mathf.Abs(nowTime - lasterTime);
        if (offsetTime > maxTimeOffset) TakeOperate();

        //该轮操作监测结束,开启新一轮操作监测
        isAllowCheck = true;
        check = false;
    }

    /// <summary>
    /// 当长时间无操作时执行这个操作
    /// </summary>
    public void TakeOperate() {
        Debug.Log("长时间无操作,接下来重新进行无操作监测");
    }
}

二、最简洁的控制无操作监测(仅Update())

1、仅Update控制,无需其他过多操作;

2、设置了该脚本(挂脚本的物体)DontDestroyOnLoad(this),不被销毁,

不担心切换场景之类情况中断监测;

3、相比于上个方法使用点击左键和是否触屏判断是否有操作,

这里使用,Input.anyKey,更大范围的监测判断是否有操作执行;

4、该方法会时时刻刻监测是否长时间无操作,不利于根据需求控制开启或取消无操作监测;

原文参考:Unity 检测程序长时间无操作

using UnityEngine;

/// <summary>
/// 长时间不操作检测,方法2
/// 原文链接:https://blog.csdn.net/qq_39849535/article/details/115005101
/// </summary>
public class LongTimeNoInput : MonoBehaviour
{
	public float maxTimeOffset = 30;//检测时间间隔
	public float lasterTime;
	public float nowTime;
	private float offsetTime;

	private void Start()
	{
		//初始上次操作时间为程序开始时间,之后随进行操作时更新
		lasterTime = Time.time;

		//挂有这个这个脚本的物体不随其他情况发生销毁,持续保留
		DontDestroyOnLoad(this);
	}

	void Update()
	{
		//记录更新当前时间
		nowTime = Time.time;

		//将“最后一次操作时的时间”更新为这次操作时的时间
		if (Input.anyKey) lasterTime = nowTime;

		//比较此时和最近一次操作的时间间隔,判断无操作时间是否超过指定时长
		offsetTime = Mathf.Abs(nowTime - lasterTime);
		if (offsetTime > maxTimeOffset) TakeFun();
	}

	/// <summary>
	/// 如果长时间不操作,执行该行为
	/// </summary>
	public void TakeFun()
    {
		Debug.Log("长时间无操作 offsetTime");
	}
}

三、外部操作控制开启或取消无操作监测

if (Input.GetKey(KeyCode.J))

1、通过外界键盘输入指定指令(这里是键盘输入“K”,“J”),

控制开启或取消执行无操作监测。

if (Input.GetKey(KeyCode.J)){}

2、区别于上面第一种使用bool值判断控制,这种外界输入指令控制操作更方便;

3、区别于上面第二种仅使用Update方法执行无操作监测,这种更容易控制执行或取消,

而非执行之后全部由Update方法控制。同时保留操作控制简单性。

参考链接:Unity长时间无操作判断

using UnityEngine;

/// <summary>
/// 长时间不操作检测,方法3
/// 改编自,原文链接:https://blog.csdn.net/lvxiaohu/article/details/119899414
/// </summary>
public class NoOperation : MonoBehaviour
{
    public float TimeOffset = 4;//检测时间间隔
    public float lasterTime;//上次的时间
    public bool isOpenCheck = false;//是否检测

    void Update()
    {
        //点击键盘J键,开始进行无操作监测
        if (Input.GetKey(KeyCode.J))
        {
            lasterTime = Time.time; //设置监测的初始时间
            isOpenCheck = true;
            Debug.Log("开始进行无操作监测");
        }
        //点击键盘K键,取消无操作监测
        if (Input.GetKey(KeyCode.K))
        {
            lasterTime = Time.time; //更新最后操作时间为取消无操作监测的时间
            isOpenCheck = false;
            Debug.Log("取消无操作监测");
        }

        //不进行无操作监测则直接退出
        if (isOpenCheck == false) return;

        //进行无操作监测
        float nowTime = Time.time;
        if (Application.isEditor)
        {
            if (Input.GetMouseButtonDown(0))
            {
                lasterTime = nowTime;//更新触摸时间
            }
        }
        else if (Application.isMobilePlatform)
        {
            //当屏幕有手指接触,或有鼠标键盘按键输入时,更新上次操作时间为此时
            if (Input.touchCount > 0 || Input.anyKey) lasterTime = nowTime;
        }

        //判断是否满足长时间无操作,满足则执行
        float offsetTime = Mathf.Abs(nowTime - lasterTime);
        if (offsetTime > TimeOffset) TakeFunc();
    }

    public void TakeFunc()
    {
        Debug.Log("长时间无操作时间");
        isOpenCheck = false;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43908355/article/details/124839000