- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
今天学习的是控制鼠标指针的隐藏,相信大家都在看视频时发现,如果选择了全屏播放模式,过一会儿如果不移动鼠标的话,鼠标指针就会慢慢隐藏,便于大家欣赏大片。
今天就带来一个脚本来实现这个功能。只要在场景中将如下脚本拖入任意游戏对象,运行场景,就可以看到效果了。
脚本如下:
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;
}
}
}
|
|