纳金网
标题:
Unity3D控制鼠标隐藏
[打印本页]
作者:
王者再临
时间:
2014-10-18 03:16
标题:
Unity3D控制鼠标隐藏
今天学习的是控制鼠标指针的隐藏,相信大家都在看视频时发现,如果选择了全屏播放模式,过一会儿如果不移动鼠标的话,鼠标指针就会慢慢隐藏,便于大家欣赏大片
。
今天就带来一个脚本来实现这个功能。只要在场景中将如下脚本拖入任意游戏对象,运行场景,就可以看到效果了。
脚本如下:
using UnityEngine;
using System.Collections;
public class PointerManager : MonoBehaviour
{
//定义一个鼠标状态的枚举
public enum PointerState
{
stateInvisible,
stateVisible,
stateSetInvisible,
stateSetVisible
}
public PointerState pointerState = PointerState.stateSetInvisible;
public float timeOutReset = 2f;
private bool hideOverride = false;
private float timeOutCounter;
private Vector3 currentMousePosition;
void Start()
{
//获得当前鼠标位置,以便判断是否移动了鼠标
currentMousePosition = Input.mousePosition;
}
void Update()
{
switch (pointerState)
{
case PointerState.stateInvisible:
if (!hideOverride)
//移动了鼠标,保持指针可见状态
if (Input.mousePosition != currentMousePosition)
pointerState = PointerState.stateSetVisible;
break;
case PointerState.stateVisible:
//开始计时,如果鼠标长时间不动,则隐藏鼠标指针
if (timeOutCounter > 0f)
{
timeOutCounter -= Time.deltaTime;
Debug.Log(timeOutCounter);
}
else pointerState = PointerState.stateSetInvisible;
break;
case PointerState.stateSetInvisible:
//隐藏鼠标指针
Screen.showCursor = false;
currentMousePosition = Input.mousePosition;
//转换状态
pointerState = PointerState.stateInvisible;
break;
case PointerState.stateSetVisible:
//指针可见时,重新计时
timeOutCounter = timeOutReset;
Screen.showCursor = true;
pointerState = PointerState.stateVisible;
break;
}
}
}
作者:
hyui
时间:
2014-10-18 03:53
Good to learn from code!
欢迎光临 纳金网 (http://old.narkii.com/club/)
Powered by Discuz! X2.5