Unity 鼠标移动监听



Move.cs

鼠标移动事件的脚本代码。

void OnMouseDrag()
	{
		Debug.Log("MouseInArea");
		transform.position += Vector3.right * Time.deltaTime * Input.GetAxis("Mouse X");
		transform.position += Vector3.up * Time.deltaTime * Input.GetAxis("Mouse Y");
		
	}
	
	void OnMouseDown()
	{
		Debug.Log("TouchDown");
	}
	
	void OnMouseUp()
	{
		Debug.Log("TouchUp");
	}
	
	void OnMouseEnter()
	{
		Debug.Log("EnterObject");
	}
	
	void OnMouseExit()
	{
		Debug.Log("ExitObject");
	}
	
	void OnMouseOver()
	{
		Debug.Log("StayObject");
	}


创建Cube的代码

	//图片资源
	Texture2D texture = null;

	void Start()
	{
		//创建材质,设置为默认的着色器
		Material mat = new Material(Shader.Find("Transparent/Diffuse"));
		//加载图片资源
		texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/psb17.jpg",typeof(Texture2D));
		//将图片资源赋值给材质
		mat.mainTexture = texture;
		//将材质保存在本地
		AssetDatabase.CreateAsset(mat,"Assets/changmen.mat");
		//创建一个Cube
		GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
		//为Cube加载材质
		obj.renderer.material = mat;
		//给Cube与脚本Move相关联
		obj.AddComponent("Move");
	}



你可能感兴趣的:(Unity3D,Unity3D)