unity3d 事件

1. 键盘事件

public class keyboard : MonoBehaviour {

    private int pressTime = 0;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
        if(Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("keyDown A");
        }

        if (Input.GetKeyUp (KeyCode.A)) {
            Debug.Log("KeyUp A");
            pressTime = 0;
        }

        if (Input.GetKey (KeyCode.W)) {
            Debug.Log("LongKeyPress Timer:" + pressTime);
            pressTime++;
        }

        if (Input.anyKey) {
            Debug.Log("any key LongPress");
        }

        if (Input.anyKeyDown) {
            Debug.Log("any keydown");
        }
    }
}
 
组合键思路:
      一旦玩家按下了某键后,便开启时间计时,记录一段时间内玩家的按键信息,然后与正确的比较,超时或者按错一个失败。
 
2. 鼠标按键事件
public class mouse : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
        if (Input.GetMouseButtonDown (0)) {
            Debug.Log("press down mouse left");
        }

        if (Input.GetMouseButtonDown (1)) {
            Debug.Log("press down mouse right");
        }

        if (Input.GetMouseButtonDown (2)) {
            Debug.Log("press down mouse middle");
        }

        if (Input.GetMouseButtonUp (0)) {
            Debug.Log("press up mouse left");
        }

        if (Input.GetMouseButton (0)) {
            //Input.mousePosition -->三维坐标
            Debug.Log("press long down left" + Input.mousePosition);        
        }
    }
}

3. 自定义事件

    自定义按键以组合的方式出现,可以设置多个按键同时影响。 Edit—>Project Setting –> input

原文地址:https://www.cnblogs.com/zhangweia/p/3649456.html