Unity - long time no operation detection

1. Coroutine control automatically executes no-operation monitoring (StartCoroutine("AutoCheck");)

1. Use the coroutine control program to automatically execute to judge whether to perform no-operation monitoring;

2. Optimize the Boolean value setting, and the continuous monitoring program that will be executed based on Update can be controlled externally;

Whether to perform no-operation monitoring can be determined by manually setting the Boolean value or by controlling the coroutine;

3. Under different platforms, customize and monitor those operations

if (Application.isEditor), if (Input.GetMouseButtonDown(0)), left mouse button click operation under the editor

if (Application.isMobilePlatform), if (Input.touchCount > 0), mobile terminal touch screen operation

Reference from: Unity long time no operation detection

The original article uses Boolean value judgment and Update monitoring, and judges different operations according to different platforms to realize whether the program has no operations within a certain period of time. 

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("长时间无操作,接下来重新进行无操作监测");
    }
}

2. The most concise control without operation monitoring (only Update())

1. Only Update control, no other operations are required;

2. Set the script (the object that hangs the script) DontDestroyOnLoad(this), not to be destroyed,

Don't worry about interrupting monitoring when switching scenes and the like;

3. Compared with the previous method, use the left button and whether to touch the screen to judge whether there is an operation,

Here, Input.anyKey is used to monitor and judge whether there is an operation in a wider range;

4. This method will monitor whether there is no operation for a long time at all times, which is not conducive to controlling the opening or canceling of no-operation monitoring according to demand;

Original reference: Unity detection program does not operate for a long time

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");
	}
}

3. External operation control enables or cancels no-operation monitoring

if (Input.GetKey(KeyCode.J))

1. Enter the specified command through the external keyboard (here is the keyboard input "K", "J"),

Controls enable or disable execution of no-op monitoring.

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

2. Different from the first one above that uses bool value to judge and control, this kind of external input command control operation is more convenient;

3. Different from the second method above that only uses the Update method to perform no-operation monitoring, this method is easier to control execution or cancellation,

Rather than being executed, all are controlled by the Update method. While retaining operational control simplicity.

Reference link: Judgment by Unity for a long time without operation

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;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43908355/article/details/124839000