栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
{
    /// 
    /// 单击
    /// 
    /// 
    void OnLeftMouseClick();

    /// 
    /// 长按
    /// 
    /// 
    void OnLeftMouseHold();

    /// 
    /// 双击
    /// 
    /// 
    void OnLeftMouseDblclick();

    /// 
    /// 滚轮滑动
    /// 
    /// 
    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()
    {

    }
}

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

四、测试

视频好像暂时传不了

你们可以自己测一下

暂时就先截图吧 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号