Unity3D New Input System 鼠标左键单击、双击、长按配置及实现接口多态用法(一)

前言

 

如果有更好的写法或是代码有什么错误等等,还请大佬教教我。

一、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()
    {

    }
}

准备工作完成,接下来开始测试

四、测试

视频好像暂时传不了

你们可以自己测一下

暂时就先截图吧 

这是两个不同的,点击空白地方是没有事件执行的,也可以自己写一个默认事件,比如视角的移动旋转什么的

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊脑袋_YA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值