标签:unity双击/单击事件
上次做项目时用到双击了,这次又想用,但是上次没总结,这次又的再写一遍,这次就直接挂出来,下次直接就能用了,附件密码:mouse;
Update里面执行:
if(Input.GetMouseButtonDown(0))
{
if(!this.mouseDownStatus)
{
this.mouseDownStatus = true;
//Debug.Log("Click !");
if(this.clickCallback != null) this.clickCallback();
if(this.mouseDownCount == 0)
{
this.lastTime = Time.realtimeSinceStartup;
}
this.mouseDownCount ++;
}
}
if(Input.GetMouseButtonUp(0))
{
//Debug.Log("Up !");
this.mouseDownStatus = false;
}
if(this.mouseDownStatus)
{
//Debug.Log("Hold !");
if(this.mouseDownCount >= 2)
{
this.currentTime = Time.realtimeSinceStartup;
if(this.currentTime - this.lastTime < 0.3f)
{
this.lastTime = this.currentTime;
this.mouseDownCount = 0;
//Debug.Log("Double Click");
if(this.doubleCallback != null) this.doubleCallback();
}
else
{
this.lastTime = Time.realtimeSinceStartup;
this.mouseDownCount = 1;
}
}
}
//双击调用方法
public class MouseResponseManager
{
public static void Register(GameObject objectItem, Action clickCallback, Action doubleCallback)
{
if(objectItem == null)
{
objectItem = new GameObject();
objectItem.name = "MouseResponseItem";
}
MouseResponseItem mouseResponseItem = objectItem.AddComponent<MouseResponseItem> ();
mouseResponseItem.Init (clickCallback, doubleCallback);
}
}
在就直接调用:
MouseResponseManager.Register(null, ()=>
{
。。。。。
}, ()=>{
。。。。。
});本文出自 “Unity_3D技术探讨” 博客,请务必保留此出处http://myselfdream.blog.51cto.com/9944020/1785566
标签:unity双击/单击事件
原文地址:http://myselfdream.blog.51cto.com/9944020/1785566