unity3d 学习笔记___虚拟轴、鼠标事件、双击事件的应用处理

Input 虚拟轴的应用:

设置:edit - projectSetting- Input

虚拟轴的优点,可以映射多组按键,使input更加的平滑


unity3d 学习笔记___虚拟轴、鼠标事件、双击事件的应用处理_第1张图片


Demo:

using UnityEngine;
using System.Collections;

public class InputTest : MonoBehaviour {

	public float speed = 10;
	public float angleSpeed = 100;

	void Update () {
	
		float translation = Input.GetAxis("Vertical") * speed * Time.deltaTime;
		float rotation = Input.GetAxis("Horizontal") * angleSpeed * Time.deltaTime;

		transform.Translate(new Vector3(0,0,translation));
		transform.Rotate(new Vector3(0,rotation,0));


		if(Input.GetButton("Fire1"))
		{
			print("Fire1 hold.");
		}

		if(Input.GetKeyDown("a"))
		{
			print("a down.");
		}
		if(Input.GetKey("a")) //KeyCode.A
		{
			print("a hold.");
		}
		if(Input.GetKeyUp("a"))
		{
			print("a up.");
		}

		//0 、1、2 分别对应鼠标左右中键
		if(Input.GetMouseButton(0))print("mouse Hold.");
		if(Input.GetMouseButtonDown(0))print("mouse down.");
		if(Input.GetMouseButtonUp(0))print("mouse up.");

	}


	///另外也可以通过MonoBehaviour 中的方法对鼠标进行处理(适用于gameObject上鼠标事件回调)
	void OnMouseEnter()
	{
		print("OnMouseEnter");
	}

	void OnMouseDrag()
	{
		print("OnMouseDrag");
	}

	void OnMouseExit()
	{
		print("OnMouseExit");
	}

	void OnMouseOver()
	{
		print("OnMouseOver");
	}

	void onMouseDown()
	{
		print("onMouseDown");
	}


	void OnMouseUp()
	{
		print("OnMouseUp");
	}



	/// GUI中对鼠标的处理
	void OnGUI()
	{
		Event e = Event.current;
		if(e.isMouse && e.clickCount == 2)
		{
			print ("double clicked.");
		}
	}
}




你可能感兴趣的:(unity3d)