关闭

unity中各种双击的实现

标签: unity双击
6922人阅读 评论(0) 收藏 举报
分类:

鼠标的双击
复制代码

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; 
    
}
2
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

unity的鼠标双击和移动触摸屏的双击

鼠标的双击相对比较简单,百度一下可以搜到很多,如下: using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;...
  • qq563129582
  • qq563129582
  • 2017-06-13 14:58
  • 1352

【Unity实用小方法】鼠标双击的判断

using UnityEngine; using System.Collections; public class DoubleClick : MonoBehaviour { //计时器,...
  • poda_
  • poda_
  • 2015-11-11 16:06
  • 3731

unity双击脚本

  • 2018-01-04 09:43
  • 5KB
  • 下载

unity鼠标双击

要实现对物体模型的双击,首先要确保模型能感应OnMouseDown()事件。导入的.fbx模型文件是不能感应该事件的,一个比较好的解决办法是创建一个cube,将其MeshRender属性取消掉,使之成...
  • woailvmengmeng
  • woailvmengmeng
  • 2014-05-29 15:59
  • 822

Unity双击响应事件代码

  • 2016-04-15 09:57
  • 672B
  • 下载

【Unity】怎么判断鼠标双击

private float delay = 0.5f; private float firstClickTime = 0; private bool oneClick = false; ...
  • sinat_20559947
  • sinat_20559947
  • 2015-10-19 22:04
  • 7517

Unity: Unity3D 模拟鼠标单击/双击

该文来自:http://www.omuying.com/article/123.aspx 功能代码: using UnityEngine; using System.Collections; pub...
  • zhangbp
  • zhangbp
  • 2016-03-10 11:26
  • 2783

unity 双击物体拉近例子

  • 2014-04-18 16:50
  • 734KB
  • 下载

Unity3D之触摸输入单击与双击研究

新建一个plane物体,tag设为Terrain,新建一个Cube物体,添加Rigidbody组件,tag设为Player,创建一个脚本TouchTest02,将该脚本挂到Cube物体上。本示例实现如...
  • ybhjx
  • ybhjx
  • 2015-12-23 11:10
  • 2523

Unity自定义UI组件(四)双击按钮、长按按钮

Unity中实现按钮双击、长击按钮,特点方便创建,容易监听,像UGUI提供的Button一样便捷,并且在不同的工程之间可以完全复用。
  • qq_29579137
  • qq_29579137
  • 2017-05-31 18:49
  • 1792
    个人资料
    • 访问:469540次
    • 积分:5030
    • 等级:
    • 排名:第6517名
    • 原创:4篇
    • 转载:312篇
    • 译文:0篇
    • 评论:14条
    最新评论