unity 上下左右滑动屏幕

本文介绍了一个Unity中的触摸输入控制器类,该类通过检测鼠标点击来模拟触摸操作,并根据手指移动的方向触发不同的动作反馈,如向上、向下、向左或向右。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using DG.Tweening;
using System;
using UnityEngine.EventSystems;

public class jarodInputController : MonoBehaviour/* , IDragHandler*/
{
   // public RectTransform yigong;
    private float fingerActionSensitivity = Screen.width * 0.05f; //手指动作的敏感度,这里设定为 二十分之一的屏幕宽度.
                                                                  //
    private float fingerBeginX;
    private float fingerBeginY;
    private float fingerCurrentX;
    private float fingerCurrentY;
    private float fingerSegmentX;
    private float fingerSegmentY;
    //
    private int fingerTouchState;
    //
    private int FINGER_STATE_NULL = 0;
    private int FINGER_STATE_TOUCH = 1;
    private int FINGER_STATE_ADD = 2;
    public Toggle shengcan;
    public Toggle shigong;

    public bool IsGongyi=false;

    public bool IsRight = false;


    public TestButton testButton;
    void Start()
    {
       
        fingerActionSensitivity = Screen.width * 0.05f;

        fingerBeginX = 0;
        fingerBeginY = 0;
        fingerCurrentX = 0;
        fingerCurrentY = 0;
        fingerSegmentX = 0;
        fingerSegmentY = 0;

        fingerTouchState = FINGER_STATE_NULL;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            if (fingerTouchState == FINGER_STATE_NULL)
            {
                fingerTouchState = FINGER_STATE_TOUCH;
                fingerBeginX = Input.mousePosition.x;
                fingerBeginY = Input.mousePosition.y;
            }
        }
        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            fingerCurrentX = Input.mousePosition.x;
            fingerCurrentY = Input.mousePosition.y;
            fingerSegmentX = fingerCurrentX - fingerBeginX;
            fingerSegmentY = fingerCurrentY - fingerBeginY;

        }


        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;

            if (fingerDistance > (fingerActionSensitivity * fingerActionSensitivity))
            {
                toAddFingerAction();
            }
        }

        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            fingerTouchState = FINGER_STATE_NULL;
        }
    }

    private void toAddFingerAction()
    {

        fingerTouchState = FINGER_STATE_ADD;

        if (Mathf.Abs(fingerSegmentX) > Mathf.Abs(fingerSegmentY))
        {
            fingerSegmentY = 0;
        }
        else
        {
            fingerSegmentX = 0;
        }

        if (fingerSegmentX == 0)
        {
            if (fingerSegmentY > 0)
            {
                //Debug.Log("up");
            }
            else
            {
                //Debug.Log("down");
            }
        }
        else if (fingerSegmentY == 0)
        {
            if (fingerSegmentX > 0)
            {
               Debug.Log("right");
                 
            }
            else
            {

              Debug.Log("left");

            }
        }

    }
}

Unity引擎中,要实现鼠标在屏幕滑动的功能,你需要创建一个UI元素(如Canvas、Image等),然后关联一些脚本来处理输入事件。以下是基本步骤: 1. **创建UI元素**: - 打开Unity,选择“GameObject” > “UI” > “Canvas”,将Canvas添加到场景中作为游戏的UI容器。 - 可能还需要一个RectTransform组件,它可以让你更精确地控制鼠标滑动区域的位置和大小。 2. **设置Input Module**: - 在项目窗口检查"Assets" > "Editore Settings" > "Input Manager",配置鼠标输入,确保"Mouse Pointer"模式设置为"Cursor". 3. **编写脚本**: - 创建一个新的C#脚本,例如`MouseScrollHandler`,并将其附加到UI元素上。在脚本中,你可以监听`OnPointerMove`或`OnDrag`事件,这两个事件会在鼠标移动或按住时触发。 ```csharp using UnityEngine; using UnityEngine.UI; public class MouseScrollHandler : MonoBehaviour { public RectTransform scrollArea; // 鼠标滑动区域 void OnPointerDown(PointerEventData eventData) { if (scrollArea.Contains(eventData.position)) startDragPosition = eventData.position; } void OnPointerUp(PointerEventData eventData) { if (scrollArea.Contains(startDragPosition)) endDragPosition = eventData.position; startDragPosition = Vector2.zero; } void OnPointerDrag(PointerEventData eventData) { if (!scrollArea.Contains(eventData.position)) return; float delta = (eventData.position.x - startDragPosition.x); scrollArea.Translate(delta * Vector2.up, Space.World); // 沿Y轴滚动 } private Vector2 startDragPosition, endDragPosition; } ``` 这个脚本会根据鼠标在指定区域内的移动,让RectTransform跟随鼠标上下滚动。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

诗远

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

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

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

打赏作者

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

抵扣说明:

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

余额充值