Untiy添加双鼠标功能记录

   找到了一个插件MulitpleMouse​​​​​​​,导入Unity,里面有一个MulitpleMouse脚本,通过这个脚本可以获取到电脑上插着几个USB接口的鼠标,并对鼠标进行操作检测,然后我自己写了一个脚本,可以让鼠标对UI进行互动。

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MouseManager : MonoBehaviour {

	private bool HideCursor = true;

	//public bool isClick = false;
	public float tempTime = 0;//计时器

	public EventSystem _mEventSystem;
	public GraphicRaycaster gra;

	List<RaycastResult> list = new List<RaycastResult>();

	// Use this for initialization
	void Start ()
	{
		_mEventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
		gra = GameObject.Find("CanvasUI").GetComponent<GraphicRaycaster>();
	}
	
	// Update is called once per frame
	void Update () {
		if (PublicClass.MouseOneIsPlay)//如果被点击
		{
			tempTime += Time.deltaTime;
			if (tempTime > 1.5)
			{
				tempTime = 0;
				PublicClass.MouseOneIsPlay = false;
			}
		}
		if (PublicClass.MouseTwoIsPlay)//如果被点击
		{
			tempTime += Time.deltaTime;
			if (tempTime > 1.5)
			{
				tempTime = 0;
				PublicClass.MouseTwoIsPlay = false;
			}
		}
		if (HideCursor)
		{
			Cursor.lockState = CursorLockMode.Locked;
			//SetCursorPos(0, 280);//强制设置坐标
		}
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			HideCursor = false;
		}

		if (MulitpleMouse.OnMouseButton(0, 0) && PublicClass.MouseOneIsPlay == false)
		{
			PublicClass.MouseOneIsPlay = true;
			list = GraphicRaycaster(MulitpleMouse.GetMousePosZeroOnLeftButtom(0));
			List<Button> Btns = new List<Button>();
			for (int i = 0; i < list.Count; i++)
			{
				Button btn_1 = null;
				btn_1 = list[i].gameObject.GetComponent<Button>();
				if (btn_1 != null)
				{
					Btns.Add(btn_1);
				}
			}
			if (Btns.Count>0)
			{
				if (Btns[0].GetComponent<Remove>())
				{
					Btns[0].GetComponent<Remove>().Index = 0;
				}
				if (Btns[0].GetComponent<Tool>())
				{
					Btns[0].GetComponent<Tool>().index = 0;
				}
				Btns[0].onClick.Invoke();
			}
			List<Toggle> Togs = new List<Toggle>();
			for (int i = 0; i < list.Count; i++)
			{
				Toggle tog_1 = null;
				tog_1 = list[i].gameObject.GetComponent<Toggle>();
				if (tog_1 != null)
				{
					Togs.Add(tog_1);
				}
			}
			if (Togs.Count > 0)
			{
				Togs[0].onValueChanged.Invoke(true);
			}
			Debug.Log("Togs::" + Togs.Count);
		}

		if (MulitpleMouse.OnMouseButton(1, 0) && PublicClass.MouseTwoIsPlay == false)
		{
			PublicClass.MouseTwoIsPlay = true;
		   list = GraphicRaycaster(MulitpleMouse.GetMousePosZeroOnLeftButtom(1));
			List<Button> Btns = new List<Button>();
			for (int i = 0; i < list.Count; i++)
			{
				Button btn_1 = null;
				btn_1 = list[i].gameObject.GetComponent<Button>();
				if (btn_1 != null)
				{
					Btns.Add(btn_1);
				}
			}
			if (Btns.Count > 0)
			{
				if (Btns[0].GetComponent<Remove>())
				{
					Btns[0].GetComponent<Remove>().Index = 1;
				}
				if (Btns[0].GetComponent<Tool>())
				{
					Btns[0].GetComponent<Tool>().index = 1;
				}
				Btns[0].onClick.Invoke();
			}
			List<Toggle> Togs = new List<Toggle>();
			for (int i = 0; i < list.Count; i++)
			{
				Toggle tog_1 = null;
				tog_1 = list[i].gameObject.GetComponent<Toggle>();
				if (tog_1 != null)
				{
					Togs.Add(tog_1);
				}
			}
			if (Togs.Count > 0)
			{
				Togs[0].onValueChanged.Invoke(true);
			}
		}
	}

	private List<RaycastResult> GraphicRaycaster(Vector2 pos)
	{
		var mPointerEventData = new PointerEventData(_mEventSystem);
		mPointerEventData.position = pos;
		List<RaycastResult> results = new List<RaycastResult>();

		gra.Raycast(mPointerEventData, results);
		return results;
	}
}

Unity鼠标指的是同时使用两个鼠标进行游戏操作。在 Unity 中实现鼠标需要使用一些脚本来控制两个鼠标的输入事件。 首先,在 Unity 中需要启用鼠标支持。在 Edit -> Project Settings -> Input Manager 中可以找到 Mouse Configuration,将其中的 Mouse Index 选项改为 Multiple,这样就可以同时支持鼠标了。 然后,可以创建一个脚本来控制两个鼠标的输入事件。在代码中,需要获取两个鼠标的位置,并处理它们的输入事件。下面是一个简单的示例代码: ```csharp using UnityEngine; public class DualMouseInput : MonoBehaviour { public int mouseIndex1 = 0; public int mouseIndex2 = 1; private void Update() { Vector3 mousePos1 = Input.mousePosition; Vector3 mousePos2 = Input.mousePosition; if (Input.GetMouseButtonDown(0)) { if (Input.mousePresent) { if (Input.GetMouseButtonDown(mouseIndex1)) { // 处理第一个鼠标的输入事件 } if (Input.GetMouseButtonDown(mouseIndex2)) { // 处理第二个鼠标的输入事件 } } } } } ``` 在这个脚本中,我们定义了两个变量 mouseIndex1 和 mouseIndex2,分别代表了两个鼠标的索引。然后在 Update 函数中,获取两个鼠标的位置,并判断它们的输入事件。 需要注意的是,鼠标输入在 Unity 中并不是默认支持的功能,需要通过脚本来实现。同时,鼠标输入在实际游戏中也不一定是必需的,需要根据具体情况进行判断和设计。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值