【转载】unity中各种双击的实现

原文地址:http://blog.csdn.net/ycl295644/article/details/46816699

鼠标的双击 复制代码

1
2
3
4
5
function OnGUI(){ 
     if(Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2){ 
         print("double click"); 
    
}
上面是双击屏幕的事件,假如想双击某个物体 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private var b = false; 
function OnGUI(){ 
     if(Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2){ 
         if(b){ 
             print("double click " + transform.name); 
        
    
function OnMouseEnter(){ 
     b = true; 
function OnMouseExit(){ 
     b =false; 
}
意思就是,当鼠标进入你想点击的物体后,双击才有效果,否则鼠标未进入物体,或者exit时,b = false,双击无效果。 
移动设备上触屏的双击 
复制代码
1
2
3
4
5
6
7
8
9
10
11
private var t1:double; 
private var t2:double; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         t2 = Time.realtimeSinceStartup; 
         if(t2 - t1 < 0.2){ 
             print("double click"); 
        
         t1 = t2; 
    
}
移动端GetMouseButtonDown是有效果的,所以这样利用t1,t2,记录两次单击的时间间隔,假如小于0.2s,则被判断为双击。 
同样,移动端点击某一确定物体的双击事件 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private var t1:double; 
private var t2:double; 
public var cam:Camera; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         var ray:Ray = cam.ScreenPointToRay(Input.mousePosition); 
         var hit:RaycastHit; 
         if(Physics.Raycast(ray,hit)){ 
             if(hit.transform.name == gameObject.transform.name){ 
                 t2 = Time.realtimeSinceStartup; 
                 if(t2 - t1 < 0.2){ 
                     print("double click " + transform.name); 
                
                 t1 = t2; 
            
        
    
}
对于上面t1和t2可以定义为System.DateTime类型,这样t2 = System.DateTime.Now;相减后用TimeSpan.如下: 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private var t1:System.DateTime; 
private var t2:System.DateTime; 
public var cam:Camera; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         var ray:Ray = cam.ScreenPointToRay(Input.mousePosition); 
         var hit:RaycastHit; 
         if(Physics.Raycast(ray,hit)){ 
             if(hit.transform.name == gameObject.transform.name){ 
                 t2 = System.DateTime.Now; 
                 if(t2 - t1 < System.TimeSpan(0,0,0,0,200)){ 
                     print("double click " + transform.name); 
                
                 t1 = t2; 
            
        
    
}
最后是,关于GUI的双击,原理都类似: 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private var b = false; 
private var t = 0.0; 
function OnGUI(){ 
     if(GUI.Button(Rect(10,10,80,50),"click me")){ 
         t = Time.time; 
         if(b){ 
             print("it's doublic click"); 
             b = false; 
         }else{ 
             b = true; 
        
    
      
function FixedUpdate(){ 
     if(t + 0.2 < Time.time){ 
         b = false; 
    
}

 

转载:http://game.ceeger.com/forum/read.php?tid=19009&fid=2

 

鼠标的双击 复制代码
1
2
3
4
5
function OnGUI(){ 
     if(Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2){ 
         print("double click"); 
    
}
上面是双击屏幕的事件,假如想双击某个物体 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private var b = false; 
function OnGUI(){ 
     if(Event.current.isMouse && Event.current.type == EventType.MouseDown && Event.current.clickCount == 2){ 
         if(b){ 
             print("double click " + transform.name); 
        
    
function OnMouseEnter(){ 
     b = true; 
function OnMouseExit(){ 
     b =false; 
}
意思就是,当鼠标进入你想点击的物体后,双击才有效果,否则鼠标未进入物体,或者exit时,b = false,双击无效果。 
移动设备上触屏的双击 
复制代码
1
2
3
4
5
6
7
8
9
10
11
private var t1:double; 
private var t2:double; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         t2 = Time.realtimeSinceStartup; 
         if(t2 - t1 < 0.2){ 
             print("double click"); 
        
         t1 = t2; 
    
}
移动端GetMouseButtonDown是有效果的,所以这样利用t1,t2,记录两次单击的时间间隔,假如小于0.2s,则被判断为双击。 
同样,移动端点击某一确定物体的双击事件 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private var t1:double; 
private var t2:double; 
public var cam:Camera; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         var ray:Ray = cam.ScreenPointToRay(Input.mousePosition); 
         var hit:RaycastHit; 
         if(Physics.Raycast(ray,hit)){ 
             if(hit.transform.name == gameObject.transform.name){ 
                 t2 = Time.realtimeSinceStartup; 
                 if(t2 - t1 < 0.2){ 
                     print("double click " + transform.name); 
                
                 t1 = t2; 
            
        
    
}
对于上面t1和t2可以定义为System.DateTime类型,这样t2 = System.DateTime.Now;相减后用TimeSpan.如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private var t1:System.DateTime; 
private var t2:System.DateTime; 
public var cam:Camera; 
function Update(){ 
     if(Input.GetMouseButtonDown(0)){ 
         var ray:Ray = cam.ScreenPointToRay(Input.mousePosition); 
         var hit:RaycastHit; 
         if(Physics.Raycast(ray,hit)){ 
             if(hit.transform.name == gameObject.transform.name){ 
                 t2 = System.DateTime.Now; 
                 if(t2 - t1 < System.TimeSpan(0,0,0,0,200)){ 
                     print("double click " + transform.name); 
                
                 t1 = t2; 
            
        
    
}
最后是,关于GUI的双击,原理都类似: 复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private var b = false; 
private var t = 0.0; 
function OnGUI(){ 
     if(GUI.Button(Rect(10,10,80,50),"click me")){ 
         t = Time.time; 
         if(b){ 
             print("it's doublic click"); 
             b = false; 
         }else{ 
             b = true; 
        
    
      
function FixedUpdate(){ 
     if(t + 0.2 < Time.time){ 
         b = false; 
    
}
 
1
 
0

转载于:https://www.cnblogs.com/cokefenta/p/6668971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值