前言
如果有更好的写法或是代码有什么错误等等,还请大佬教教我。
一、New Input System配置
下载安装哪些就自己搜下怎么整吧,我这就不写了,直接写怎么配置。
首先右键—>创建—>Input Actions
这个是详细配置。
创建一个空物体
为物体添加MouseInputPlayer C#脚本(下方会写,此处先创建一个空的脚本文件)
为物体添加Player Input组件
按上图进行绑定
二、脚本配置
MouseInputPlayer.cs脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
public class MouseInputPlayer : MonoBehaviour
{
private RaycastHit hit;
private bool hitBool; //是否点击到目标
private IMouseInputPlayer mouseClickInterface; //接口
private ObjectProperties mIPScriptName; // 物体属性脚本
private bool ifMouseClickInterface; // 是否存在
private bool ifObjectProperties; // 是否存在
public void OnLeftBottonClick(InputAction.CallbackContext context)
{
switch (context.phase)
{
case InputActionPhase.Started:
{
//Debug.Log("操作开始");
mouseClickInterface = null; //初始化接口
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); //射线
hitBool = Physics.Raycast(ray, out hit); //返回是否点击到目标
if (hitBool)
{
//Debug.Log("操作开始");
ifObjectProperties = hit.collider.gameObject.TryGetComponent(out objectProperties); // 获取物体属性脚本
ifMouseClickInterface = hit.collider.gameObject.TryGetComponent(out mouseClickInterface); //设置接口
}
}
break;
case InputActionPhase.Performed:
{
if (hitBool && ifObjectProperties)
{
if (context.interaction is MultiTapInteraction)
{
//Debug.Log("执行双击逻辑");
if (objectProperties.ifLeftMouseDblclick && ifMouseClickInterface)
{
mouseClickInterface.OnLeftMouseDblclick();
}
}
else if (context.interaction is HoldInteraction)
{
//Debug.Log("执行长按逻辑");
if (objectProperties.ifLeftMouseHold && ifMouseClickInterface)
{
mouseClickInterface.OnLeftMouseHold();
}
}
else
{
//列表中只有MultiTapInteraction和HoldInteraction对应的两种Interaction。
//故不会走到这个else里。
}
}
}
break;
case InputActionPhase.Canceled:
{
if (context.interaction is MultiTapInteraction)
{
//Debug.Log("执行点击逻辑");
if (hitBool && ifObjectProperties)
{
if (objectProperties.ifLeftMouseClick && ifMouseClickInterface)
{
mouseClickInterface.OnLeftMouseClick();
}
}
}
}
break;
}
}
}
IMouseInputPlayer.cs 接口 (无需挂载在任何物体上)(滚轮滑动方法暂时没写,后面在加上)
public interface IMouseInputPlayer
{
/// <summary>
/// 单击
/// </summary>
/// <param name="context"></param>
void OnLeftMouseClick();
/// <summary>
/// 长按
/// </summary>
/// <param name="context"></param>
void OnLeftMouseHold();
/// <summary>
/// 双击
/// </summary>
/// <param name="context"></param>
void OnLeftMouseDblclick();
/// <summary>
/// 滚轮滑动
/// </summary>
/// <param name="context"></param>
void OnLeftMouseRoller();
}
ObjectProperties.cs 物体属性脚本(挂载在需要的物体上)
(2022-2-22 更新优化了脚本,去掉了ObjectProperties.cs中的MIPScriptName变量)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectProperties : MonoBehaviour
{
public bool ifLeftMouseClick = false; //是否执行对应脚本单击事件
public bool ifLeftMouseDblclick = false; //是否执行对应脚本双击事件
public bool ifLeftMouseHold = false; //是否执行对应脚本长按事件
}
三、不同物体的单击、双击、长按事件测试脚本
首先是一个Cube
(2022-2-22 更新优化了脚本,去掉了ObjectProperties.cs中的MIPScriptName变量)
CubeMouseClick.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeMouseClick : MonoBehaviour, IMouseInputPlayer
{
public void OnLeftMouseClick()
{
Debug.Log("CubeMouseClick执行点击逻辑");
}
public void OnLeftMouseDblclick()
{
Debug.Log("CubeMouseClick执行双击逻辑");
}
public void OnLeftMouseHold()
{
Debug.Log("CubeMouseClick执行长按逻辑");
}
public void OnLeftMouseRoller()
{
}
}
接下来是一个Sphere
(2022-2-22 更新优化了脚本,去掉了ObjectProperties.cs中的MIPScriptName变量)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereMouseClick : MonoBehaviour, IMouseInputPlayer
{
public void OnLeftMouseClick()
{
Debug.Log("SphereMouseClick执行点击逻辑");
}
public void OnLeftMouseDblclick()
{
Debug.Log("SphereMouseClick执行双击逻辑");
}
public void OnLeftMouseHold()
{
Debug.Log("SphereMouseClick执行长按逻辑");
}
public void OnLeftMouseRoller()
{
}
}
准备工作完成,接下来开始测试
四、测试
视频好像暂时传不了
你们可以自己测一下
暂时就先截图吧
这是两个不同的,点击空白地方是没有事件执行的,也可以自己写一个默认事件,比如视角的移动旋转什么的