作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
目前,ScrollRect
在移动设备上的多点触控方面存在严重缺陷。
如果您自己尝试一下,您会发现只要将两根手指放在屏幕上,内容就会跳来跳去,并产生一些意想不到的行为。
有什么解决办法吗?目前,this是我找到的唯一解决方案,但在某些情况下它仍然存在问题,最重要的是,它无法确定屏幕上所有手指的平均输入位置(或 MultiTouchPosition
)。
这是我对 UnityUIExtensions
bitbucket 中的 MultiTouchScrollRect.cs
脚本的修改版本,但每次我将下一个手指放在屏幕上时它都会跳转:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MultiTouchScrollRect : ScrollRect
{
private int minimumTouchCount = 1, maximumTouchCount = 2, pointerId = -100;
public Vector2 MultiTouchPosition
{
get
{
Vector2 position = Vector2.zero;
for (int i = 0; i < Input.touchCount && i < maximumTouchCount; i++)
{
position += Input.touches[i].position;
}
position /= ((Input.touchCount <= maximumTouchCount) ? Input.touchCount : maximumTouchCount);
return position;
}
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (Input.touchCount >= minimumTouchCount)
{
pointerId = eventData.pointerId;
eventData.position = MultiTouchPosition;
base.OnBeginDrag(eventData);
}
}
public override void OnDrag(PointerEventData eventData)
{
if (Input.touchCount >= minimumTouchCount)
{
eventData.position = MultiTouchPosition;
if (pointerId == eventData.pointerId)
{
base.OnDrag(eventData);
}
}
}
public override void OnEndDrag(PointerEventData eventData)
{
if (Input.touchCount >= minimumTouchCount)
{
pointerId = -100;
eventData.position = MultiTouchPosition;
base.OnEndDrag(eventData);
}
}
}
感谢您的宝贵时间!
最佳答案
对于那些感兴趣的人,这是我编写的解决问题的扩展 ScrollRect 类:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace DanielLochner.Assets
{
public class MultiTouchScrollRect : ScrollRect
{
#region Fields
private int minimumTouchCount = 1, maximumTouchCount = 2, initialTouchCount = 0;
#endregion
#region Properties
public Vector2 MultiTouchPosition
{
get
{
Vector2 position = Vector2.zero;
for (int i = 0; i < Input.touchCount && i < maximumTouchCount; i++)
{
position += Input.touches[i].position;
}
position /= ((Input.touchCount <= maximumTouchCount) ? Input.touchCount : maximumTouchCount);
return position;
}
}
#endregion
#region Methods
private void Update()
{
if (SystemInfo.deviceType == DeviceType.Handheld || UnityEditor.EditorApplication.isRemoteConnected)
{
if (Input.touchCount > 0)
{
if (initialTouchCount == 0)
{
initialTouchCount = Input.touchCount;
}
}
else
{
initialTouchCount = 0;
}
}
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (SystemInfo.deviceType == DeviceType.Handheld || UnityEditor.EditorApplication.isRemoteConnected)
{
if (Input.touchCount >= minimumTouchCount && Input.touchCount == initialTouchCount)
{
eventData.position = MultiTouchPosition;
base.OnBeginDrag(eventData);
}
}
else if (SystemInfo.deviceType == DeviceType.Desktop)
{
base.OnBeginDrag(eventData);
}
}
public override void OnDrag(PointerEventData eventData)
{
if (SystemInfo.deviceType == DeviceType.Handheld || UnityEditor.EditorApplication.isRemoteConnected)
{
if (Input.touchCount >= minimumTouchCount && Input.touchCount == initialTouchCount)
{
eventData.position = MultiTouchPosition;
base.OnDrag(eventData);
}
}
else if (SystemInfo.deviceType == DeviceType.Desktop)
{
base.OnDrag(eventData);
}
}
public override void OnEndDrag(PointerEventData eventData)
{
if (SystemInfo.deviceType == DeviceType.Handheld || UnityEditor.EditorApplication.isRemoteConnected)
{
if (Input.touchCount >= minimumTouchCount)
{
eventData.position = MultiTouchPosition;
base.OnEndDrag(eventData);
}
}
else if (SystemInfo.deviceType == DeviceType.Desktop)
{
base.OnEndDrag(eventData);
}
}
#endregion
}
}
关于c# - 修复 Unity 中的 ScrollRect 多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221113/
我是一名优秀的程序员,十分优秀!