首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Touch.rawPosition - Touch.Position而不是Touch.position -(之前保存的) Touch.position?

Touch.rawPosition - Touch.Position而不是Touch.position -(之前保存的) Touch.position?
EN

Stack Overflow用户
提问于 2021-04-21 17:46:04
回答 1查看 68关注 0票数 0

假设我的目标是使用Touch类获得swipe移动的长度。

使用以下命令有什么区别

代码语言:javascript
运行
复制
 if(touchPhase == touchPhase.Began)
    {
    touchStart = touch.Position;
    }
    else if(touchPhase == touchPhase.Ended) {
    Vector2 length = touch.Position - touchStart;
    }

代码语言:javascript
运行
复制
if(touchPhase == touchPhase.Ended) {
    Vector2 length = touch.RawPosition - touch.Position;
    }

从文档中可以看出,当拖动触摸触点时,rawPosition不会改变,因此代码应该可以工作。

我需要一个小小的解释。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-04-21 23:37:08

据我所知,行为应该是平等的。正如你所说的,Touch.RawPosition就是

触摸触点在屏幕空间像素坐标中的第一个位置。

原始位置返回触摸接触的原始位置,并且在拖动触摸时不会更改。如果您需要触摸的当前位置,请参阅Touch.position

除了第二个代码片段中的之外,应该是

代码语言:javascript
运行
复制
Vector2 length = touch.Position - touch.RawPosition;

增量始终是当前位置减去之前的位置。

你可以简单地使用下面的命令来验证自己。

代码语言:javascript
运行
复制
// In general rather use switch for enums
switch(touchPhase)
{
    case TouchPhase.Began:
        touchStart = touch.Position;
        break;

    case TouchPhase.Ended:
        Debug.Assert(touchStart == touch.RawPosition, "Not equal :O", this);
        Vector2 length = touch.Position - touchStart;
        Vector2 length2 = touch.Position - touch.RawPosition;
        // Obviously redundant - if the first assert already passed then this will as well
        Debug.Assert(length == length2, "Not equal :O", this)
        break;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67193280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档