zoukankan      html  css  js  c++  java
  • Unity3D 中鼠标按下时OnMouseDown()、Input.GetMouseButtonDown()和EventType.MouseDown的响应验证

    初学unity3D,对于其中的事件响应不是很清楚,于是写了下面的代码来验证:

    1、新建.cs文件,名为testMouse.cs:

    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class testMouse : MonoBehaviour {  
    5.   
    6.     // Use this for initialization  
    7.     void Start () {  
    8.       
    9.     }  
    10.       
    11.     // Update is called once per frame  
    12.     void Update () {  
    13.         if (Input.GetMouseButtonDown(0))  
    14.         {  
    15.             Debug.Log("Input.GetMouseButtonDown response");  
    16.         }  
    17.     }  
    18.   
    19.     void OnMouseDown() {  
    20.         Debug.Log("OnMouseDown response");  
    21.     }  
    22.   
    23.     void OnGUI() {  
    24.         if (Event.current != null && Event.current.type == EventType.mouseDown) {  
    25.             Debug.Log("EventType.mouseDown response");  
    26.         }  
    27.     }  
    28. }  

    2、场景中的游戏对象很简单,只有一个Cube和主相机。

    3、将testMouse.cs组件附加到Cube上面,运行游戏,当用鼠标点击Cube时,会有如下显示:

    4、当用鼠标单击任何除Cube外的位置都会产生如下响应:

    5、这说明Event和Input.GetMousexxx事件会被任何gameobject监控到,而OnMousexxx事件只会被该脚本附加上的gameobject监控到。

    6、为什么Event事件要写在OnGUI函数里面呢?可以再这两个文档中找到线索:

    a、http://game.ceeger.com/Script/MonoBehaviour/MonoBehaviour.OnGUI.html

    b、http://game.ceeger.com/Script/Event/Event.html

    OnGUI函数在每帧会被调用多次。

  • 相关阅读:
    Python学习(十六)内置函数,递归
    Python学习(十五)json串使用,不固定参数,关键词参数
    Python学习(十四)常量,局部变量,全局变量
    python学习(十三)函数
    python学习(十二)模块
    Python学习(十一)文件指针,文件操作练习,修改文件
    mongodb聚合
    mongodb
    软件测试需要学习哪些课程
    Jmeter的使用和监控分析
  • 原文地址:https://www.cnblogs.com/lancidie/p/8036984.html
Copyright © 2011-2022 走看看