Event.button 鼠标按钮

var button : int

Description描述

Which mouse button was pressed.

哪一个鼠标按钮被按下。

0 means left mouse button, 1 means right mouse button, 2 means middle mouse button. Used in EventType.MouseDown, EventType.MouseUp events.

0表示鼠标左键,1表示鼠标右键,2表示鼠标中键。在EventType.MouseDownEventType.MouseUp事件中使用。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void OnGUI() {
		Event e = Event.current;
		if (e.button == 0 && e.isMouse)
			Debug.Log("Left Click");
		else
			if (e.button == 1)
				Debug.Log("Right Click");
			else
				if (e.button == 2)
					Debug.Log("Middle Click");
				else
					if (e.button > 2)
						Debug.Log("Another button in the mouse clicked");

	}
}
// Detect which mouse button is currently pressed and print it.
//检测当前按下的鼠标按钮并打印
function OnGUI() {
	var e : Event = Event.current;
	if(e.button == 0 && e.isMouse){
		Debug.Log("Left Click");
	} else if(e.button == 1) {
		Debug.Log("Right Click");
	} else if (e.button == 2) {
		Debug.Log("Middle Click");
	} else if (e.button > 2) {
		Debug.Log("Another button in the mouse clicked");
	}
}
最后修改:2010年12月31日 Friday 23:11

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。