找到了一个插件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;
}
}