Version: 2020.2

Input.GetMouseButtonDown

切换到手册
public static bool GetMouseButtonDown (int button);

描述

在用户按下给定鼠标按钮的帧期间返回 true。

您需要从 Update 函数调用该函数(因为每帧都会重置状态)。 在用户释放鼠标按钮并再次按下鼠标按钮前,它不会返回 true。 button 值为 0 表示主按钮(通常为左按钮),1 表示副按钮,2 表示中间按钮。

using UnityEngine;
using System.Collections;

// Detects clicks from the mouse and prints a message // depending on the click detected.

public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) Debug.Log("Pressed primary button.");

if (Input.GetMouseButtonDown(1)) Debug.Log("Pressed secondary button.");

if (Input.GetMouseButtonDown(2)) Debug.Log("Pressed middle click."); } }