unity中各种双击的实现
来源:互联网 发布:野荸荠淘宝 编辑:程序博客网 时间:2024/05/23 01:19
鼠标的双击
复制代码
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;
}
移动设备上触屏的双击
复制代码
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;
}
}
同样,移动端点击某一确定物体的双击事件
复制代码
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;
}
}
}
}
复制代码
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
鼠标的双击
复制代码上面是双击屏幕的事件,假如想双击某个物体
复制代码意思就是,当鼠标进入你想点击的物体后,双击才有效果,否则鼠标未进入物体,或者exit时,b = false,双击无效果。
移动设备上触屏的双击
复制代码移动端GetMouseButtonDown是有效果的,所以这样利用t1,t2,记录两次单击的时间间隔,假如小于0.2s,则被判断为双击。
同样,移动端点击某一确定物体的双击事件
复制代码
对于上面t1和t2可以定义为System.DateTime类型,这样t2 = System.DateTime.Now;相减后用TimeSpan.如下:
复制代码最后是,关于GUI的双击,原理都类似:
复制代码
复制代码
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;
}
移动设备上触屏的双击
复制代码
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;
}
}
同样,移动端点击某一确定物体的双击事件
复制代码
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;
}
}
}
}
复制代码
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;
}
}
0 0
- unity中各种双击的实现
- unity中各种双击的实现
- unity中各种双击
- unity 学习 双击一个物体的实现
- flex中双击事件的实现
- Android framework 中双击 home 的实现
- Android framework中双击home 的实现
- Android framework中双击home 的实现
- Android framework中双击home 的实现
- Unity中各种常用文件夹的用处
- 关于unity的鼠标双击和移动触摸屏的双击
- 关于unity的鼠标双击和移动触摸屏的双击
- unity的鼠标双击和移动触摸屏的双击
- cocos2d-x2.1.5中实现手势识别(各种形状,单击,双击)
- unity中跳跃的实现
- Unity 里双击脚本打不开的解决方法
- unity3d中实现双击效果
- Android中实现双击事件
- java面试题及答案(基础题122道,代码题19道)
- supermap 三维 Iserver 发布三维缓存 缺少部分图层问题
- 【hdu】【线段树入门】I Hate It
- C语言小试题:找出M行N列矩阵的行最大值,存入数组并输出。
- 写给 iOS 程序员看的 C++(2)
- unity中各种双击的实现
- 对Spring 及SpringMVC的理解
- linux目录下各文件夹作用
- 性能优化工具
- mfc中的切分窗口
- oracle 索引的缺点和优点
- 三大国际主流项目管理体系
- 如何优化服务器的性能
- 图片的存储及读取.