Unity 5多点触控滑动事件

时间:2016-06-04 17:32:40

标签: c# mobile unity3d multi-touch

我正在使用Unity的默认Cross平台操纵杆控件的稍微修改版本,它在unity 5.3中可用,我正在尝试创建一个事件触发器,当我在操纵杆上滑动时会触发该事件触发器(初始点击必须启动离开操纵杆)

 public SwipeDirectionEnum GetSwipeDirection(Vector3 startPosition, Vector3 endingPosition);

我已经知道如何创建逻辑来检测滑动的方向,我们将这个方法存根定义为

var originalText = [
    "Sample Text",
    "More Text"
];

var extraString = "extra text to be appended";

originalText.forEach(function(_, i, aa) {
    aa[i] += ' ' + extraString;
});

console.log(originalText);

我遇到问题的唯一部分是如何在控件外部建立点击,通过与操纵杆控件相交或部分交叉来滑动端点?

1 个答案:

答案 0 :(得分:0)

我编写了一个脚本来执行此操作,以防将来有人想要这样做。您将需要修改TouchPad.cs,这将使用framecache来查找几帧之间的中间方向,这样它就可以适当地支持多点触控。

将其添加到TouchPad.Cs

  $('.submenu-button').click(function() {
    $(this).next().toggleClass('open').children('li').toggleClass('open');    
  });

修改on指针以清除缓存:

  bool isTouchPadCoordinatorInitialized = false;
    TouchPadEventCoordinator touchPadCoordinator;

    public void InitializeTouchPadCoordinator(string horizontalAxis, string verticalAxis, Action swipeUp = null, Action swipeRight = null,
        Action swipeDown = null, Action swipeLeft = null, Action swipeMultiUp = null, 
        Action swipeMultiRight = null, Action swipeMultiDown = null, Action swipeMultiLeft = null)
    {
        touchPadCoordinator = new TouchPadEventCoordinator(horizontalAxis, verticalAxisName, swipeUp, swipeRight, 
                                                            swipeDown, swipeLeft, swipeMultiUp, swipeMultiRight, 
                                                            swipeMultiDown, swipeLeft);
        isTouchPadCoordinatorInitialized = true;
    }

将更新修改为如下所示:

    public void OnPointerUp(PointerEventData data)
    {
        m_Dragging = false;
        m_Id = -1;
        UpdateVirtualAxes(Vector3.zero);

        if(isTouchPadCoordinatorInitialized)
        {
            touchPadCoordinator.ClearDirectionCache();
        }

    }

TouchPadCoordinator文件如下所示:

    void Update()
    {
        if (!m_Dragging)
        {
            return;
        }

        if(isTouchPadCoordinatorInitialized)
        {
            touchPadCoordinator.UpdateSwipe();
        }
   // The rest of the update function goes here

使用它看起来像这样:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using System;
using System.Collections.Generic;
using System.Linq;

namespace UnityStandardAssets.CrossPlatformInput
{
    public class TouchPadEventCoordinator : MonoBehaviour
    {

        public int maxFrameCache = 7;

        string _horizontalAxisName;
        string _verticalAxisName;

        List<SwipeDirection> _directionCache = new List<SwipeDirection>();
        Dictionary<SwipeDirection, Action> _directionEventMap = new Dictionary<SwipeDirection, Action>();

        public Action SwipeUp { get; set; }
        public Action SwipeRight { get; set; }
        public Action SwipeDown { get; set; }
        public Action SwipeLeft { get; set; }
        public Action SwipeMultiUp { get; set; }
        public Action SwipeMultiRight { get; set; }
        public Action SwipeMultiDown { get; set; }
        public Action SwipeMultiLeft { get; set; }

        public TouchPadEventCoordinator(string horizontalAxisName, string verticalAxisName, Action swipeUp = null, Action swipeRight = null,
            Action swipeDown = null, Action swipeLeft = null, Action swipeMultiUp = null,
            Action swipeMultiRight = null, Action swipeMultiDown = null, Action swipeMultiLeft = null)
        {
            _horizontalAxisName = horizontalAxisName;
            _verticalAxisName = verticalAxisName;

            SwipeUp = swipeUp;
            SwipeDown = swipeDown;
            SwipeLeft = swipeLeft;
            SwipeRight = swipeRight;
            SwipeMultiUp = swipeMultiUp;
            SwipeMultiDown = swipeMultiDown;
            SwipeMultiLeft = swipeMultiLeft;
            SwipeMultiRight = swipeMultiRight;

            _directionEventMap.Add(SwipeDirection.Up, SwipeUp);
            _directionEventMap.Add(SwipeDirection.Down, SwipeDown);
            _directionEventMap.Add(SwipeDirection.Left, SwipeLeft);
            _directionEventMap.Add(SwipeDirection.Right, SwipeRight);
            _directionEventMap.Add(SwipeDirection.MultiUp, SwipeMultiUp);
            _directionEventMap.Add(SwipeDirection.MultiDown, SwipeMultiDown);
            _directionEventMap.Add(SwipeDirection.MultiLeft, SwipeMultiLeft);
            _directionEventMap.Add(SwipeDirection.MultiRight, SwipeMultiRight);
        }

        public void ProcessSwipeEvent(SwipeDirection swipeDirection)
        {
            if (false == Enum.IsDefined(typeof(SwipeDirection), swipeDirection))
            {
                throw new Exception("Event Not defined");
            }

            _directionCache.Add(swipeDirection);

            if (_directionCache.Count == maxFrameCache)
            {
                var averageSwipeDirection = GetAverageSwipeDirection();
                Action swipeEvent = _directionEventMap[averageSwipeDirection];

                if (null != swipeEvent)
                {
                    swipeEvent();
                }

                ClearDirectionCache();
            }

        }

        public SwipeDirection GetAverageSwipeDirection()
        {
            return _directionCache.GroupBy(x => x)
                     .Select(directionGroup => new
                     {
                         directionGroup.Key,
                         Count = directionGroup.Count()
                     }).OrderByDescending(x => x.Count)
                     .FirstOrDefault().Key;
        }

        public void ClearDirectionCache()
        {
            _directionCache.Clear();
        }

        public enum SwipeDirection
        {
            Up,
            Right,
            Down,
            Left,
            MultiUp,
            MultiRight,
            MultiDown,
            MultiLeft,
        }

        public void UpdateSwipe()
        {
            var horizontal = CrossPlatformInputManager.GetAxis(_horizontalAxisName);
            var vertical = CrossPlatformInputManager.GetAxis(_verticalAxisName);

            //No Touch Occured
            if (vertical == 0 && horizontal == 0)
            {
                return;
            }

            if (Input.touchCount > 1)
            {

            }

            //If they swipe at exactly a 45 register as horizontal so atleast we registered a hit
            if (Mathf.Abs(horizontal) >= Mathf.Abs(vertical))
            {
                HandleHorizontalMovement(horizontal);
            }
            else
            {
                HandleVerticalMovement(vertical);
            }
        }

        protected void HandleVerticalMovement(float vertical)
        {
            if (vertical > 0)
            {
                ProcessSwipeEvent(SwipeDirection.Up);
            }
            else
            {
                ProcessSwipeEvent(SwipeDirection.Down);
            }
        }

        protected void HandleHorizontalMovement(float horizontal)
        {
            if (horizontal > 0)
            {
                ProcessSwipeEvent(SwipeDirection.Right);
            }
            else
            {
                ProcessSwipeEvent(SwipeDirection.Left);
            }
        }

        protected void HandleDepthMovement(float horizontal, float vertical)
        {
            //establish the most prodiment direction
            if (Mathf.Abs(horizontal) >= Mathf.Abs(vertical))
            {
                if (horizontal > 0)
                {
                    ProcessSwipeEvent(SwipeDirection.MultiRight);
                }
                else if (horizontal < 0)
                {
                    ProcessSwipeEvent(SwipeDirection.MultiLeft);
                }
            }
            else
            {
                if (vertical > 0)
                {
                    ProcessSwipeEvent(SwipeDirection.MultiUp);
                }
                else if (vertical < 0)
                {
                    ProcessSwipeEvent(SwipeDirection.MultiDown);
                }
            }
        }
    }
}

正如注释SwipeEvent是在调用滑动时将执行的操作。

就像一条记录一样,由于我没有移植到移动设备,因此尚未使用多次滑动事件进行测试,因此请自行承担使用风险。我在手机上测试游戏后会更新此内容