当前位置: 首页 > wzjs >正文

企业名录在哪里查询新乡网站关键词优化

企业名录在哪里查询,新乡网站关键词优化,网站开发与网页制作,wordpress最大上传2m实现步骤 在Unity中实现一个功能,当鼠标或触摸超过一定时间没有操作时,自动返回待机界面。 检测输入 首先,我需要检测用户的输入,无论是鼠标还是触摸。Unity的Input系统可以检测到鼠标和触摸事件,比如Input.GetAxis…

实现步骤

在Unity中实现一个功能,当鼠标或触摸超过一定时间没有操作时,自动返回待机界面。

检测输入

首先,我需要检测用户的输入,无论是鼠标还是触摸。Unity的Input系统可以检测到鼠标和触摸事件,比如Input.GetAxis(“Mouse X”)或者Input.touchCount。

  • 鼠标移动:通过 Input.GetAxis 检测光标移动。
  • 鼠标点击:检测左键(0)、右键(1)按下。
  • 触摸事件:识别触摸开始(Began)和移动(Moved),忽略静止(Stationary)。
// 检测鼠标或触摸输入private bool IsInputDetected(){// 鼠标移动检测if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0){return true;}// 鼠标点击检测if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)){return true;}// 触摸检测if (Input.touchCount > 0){Touch touch = Input.GetTouch(0);if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved){return true;}}return false;}

计时器

当有输入的时候,我需要重置一个计时器。如果没有输入,计时器就会一直累加,直到超过设定的超时时间,比如30秒,这时候就触发返回待机界面的动作。

  • 设置超时时间:在Inspector面板中调整 timeoutDuration(默认为30秒)。
void Update(){if (IsInputDetected()){currentIdleTime = 0f; // 检测到输入,重置计时}else{currentIdleTime += Time.deltaTime; // 无输入,累计时间if (currentIdleTime >= timeoutDuration){// 超时触发返回待机}}}

返回待机界面

  • 待机界面:根据项目需求选择加载场景或激活UI:
    • 场景切换:确保待机场景(如StandbyScene)在Build Settings中。
    • UI激活:取消注释UI相关代码,并在Inspector中绑定对应的UI对象。
// 返回待机界面private void ReturnToStandby(){// 示例:重新加载待机场景,替换为你的逻辑SceneManager.LoadScene("StandbyScene");// 或激活UI元素// standbyUI.SetActive(true);}

完整代码

using UnityEngine;
using UnityEngine.SceneManagement;public class IdleDetector : MonoBehaviour
{[SerializeField] private float timeoutDuration = 30f; // 超时时间(秒)private float currentIdleTime;void Update(){if (IsInputDetected()){currentIdleTime = 0f; // 检测到输入,重置计时}else{currentIdleTime += Time.deltaTime; // 无输入,累计时间if (currentIdleTime >= timeoutDuration){ReturnToStandby(); // 超时触发返回待机}}}// 检测鼠标或触摸输入private bool IsInputDetected(){// 鼠标移动检测if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0){return true;}// 鼠标点击检测if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)){return true;}// 触摸检测if (Input.touchCount > 0){Touch touch = Input.GetTouch(0);if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved){return true;}}return false;}// 返回待机界面private void ReturnToStandby(){// 示例:重新加载待机场景,替换为你的逻辑SceneManager.LoadScene("StandbyScene");// 或激活UI元素// standbyUI.SetActive(true);}
}

优化

  • 在非活动界面暂停检测,减少不必要的计算。
  • 用UnityEvent实现超时触发
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;public class IdleDetector : MonoBehaviour
{[SerializeField] private float timeoutDuration = 30f; // 超时时间(秒)private float currentIdleTime;[SerializeField] private bool isDetector = false;[SerializeField] private UnityEvent onIdleEvent;//启用检测public void SetDetectorState(bool state){isDetector = state;}//重置时间private void ResetTimer(){currentIdleTime = 0f; }void Update(){if(!isDetector)return;if (IsInputDetected()){ResetTimer();// 检测到输入,重置计时}else{currentIdleTime += Time.deltaTime; // 无输入,累计时间if (currentIdleTime >= timeoutDuration){onIdleEvent?.Invoke();ResetTimer();// 如果需要持续检测,可以重置计时器}}}// 检测鼠标或触摸输入private bool IsInputDetected(){// 鼠标移动检测if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0){return true;}// 鼠标点击检测if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)){return true;}// 触摸检测if (Input.touchCount > 0){Touch touch = Input.GetTouch(0);if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved){return true;}}return false;}
}

测试

  • 挂载脚本:将脚本挂载到场景中一个持久存在的对象(如空对象 IdleDetector)。
    在这里插入图片描述
http://www.dtcms.com/wzjs/565031.html

相关文章:

  • 莱芜网站建设开发公司甘肃省建设厅网站官网
  • 网站下方链接图标怎么做WordPress京东返利
  • 注销网站 注销主体简单的网站开发的软件
  • 怎么查看网站的域名天津建设工程投标信息
  • 怎么免费建个人网站网站上线详细步骤
  • 广告点击网站源码卡片风格网站
  • 网站申请界面怎么做成都网站制作芜湖厂商
  • ks免费刷粉网站推广低价建设工程网站资质人员查询
  • 南山网站设计线google关键词分析工具
  • 地矿局网站建设方案义乌创博网络科技有限公司
  • 陕西网站制作电话一个人做网站 没有人写文章怎么办
  • 结合七牛云做视频网站合肥网站备案
  • ios移动网站开发详解wordpress文章版权声明
  • 网站备案后打不开肇庆百度快速排名
  • 自己做网站多少钱wordpress list
  • 做自媒体搬运文章的网站wordpress主题导入Demo
  • 温州网站建设制作公司培训会网站建设
  • 惠东东莞网站建设网站html模板下载
  • 网站建设入门教程视频一起生活小程序怎么注册
  • 网站维护费用计入什么科目学校网站源码php
  • 网站开发与建设的原则步步高网站建设报告
  • 太原网站建设报价网站标签设计
  • 鲤城区建设局网站网站建设备案费用
  • 网站可信认证必做免费招聘网站招聘
  • 世界上有一个wordpress站点wordpress论坛样式
  • 网站域名的意思做二手车网站需要什么手续
  • 中国做网站公司湘潭网站制作
  • 购买友情链接网站重庆建网站企业有哪些
  • 内蒙古网站开发公司wordpress ftp
  • 网站为什么要seo?wordpress数组