简体   繁体   English

如何获得Unity的帧率独立触摸位置?

[英]How do I get frame-rate independent touch position for Unity?

I am to make a mobile painting game in Unity and I've encountered a serious problem: the Input class in Unity is frame-dependent. 我要在Unity制作一个移动绘画游戏,我遇到了一个严重的问题:Unity中的Input类是依赖于帧的。 Thus I can't get the position of touch frequent enough to make my application draw smoothly; 因此,我不能经常获得触摸的位置,以使我的应用程序顺利绘制; as a result I get something like just points on the background, not connected between each other. 结果我得到的东西就像背景上的点,而不是彼此之间的连接。

I tried to just connect the points that are detected in Unity, and than my result was just the same points connected with lines, of course. 我试图连接在Unity中检测到的点,当然,我的结果只是与线相关的点。 I was trying this in Unity Editor with about 180-200 fps, and on mobile phone with 30-50 fps it looks even worse. 我在Unity Editor中尝试使用大约180-200 fps,而在30-50 fps的手机上,它看起来更糟糕。 I expect that I have to get the touch positions somehow in android studio or Xcode, and only then use them in my C# code in Unity editor. 我希望我必须在android studio或Xcode中以某种方式获得触摸位置,然后才在Unity编辑器中的C#代码中使用它们。

Am I thinking right to use extern from Unity tools, or there is another easier way to do it directly in Unity? 我是否正在考虑使用Unity工具中的extern,或者还有另一种更简单的方法可以直接在Unity中使用它? If there is none and I am right, can somebody give me some links to guides/tutorials how to do it and integrate it with Unity? 如果没有,我是对的,有人可以给我一些链接指南/教程如何做到并将它与Unity集成? I have never worked outside of Unity and have no experience in integration some external tools with it. 我从来没有在Unity之外工作,也没有经验集成一些外部工具。

Note: I've tried FixedUpdate without any luck - it doesn't matter how often I try to get the position variables, it is about how often they are updated; 注意:我尝试过没有任何运气的FixedUpdate - 我尝试获取位置变量的频率并不重要,而是关于它们更新的频率; I also tried Event.current.mousePosition(in unity editor) in OnGUI method, but it also gave me no difference. 我也在OnGUI方法中尝试了Event.current.mousePosition(在统一编辑器中),但它也没有给我带来任何区别。

Upd: As I have already said, I need to get positions more frequently than the Input class gives me. Upd:正如我已经说过的,我需要比Input类给我的更频繁地获取位置。 It updates not fast enough! 它的更新速度不够快! Here's what I get without connecting the points. 这是我得到的,没有连接点。 The image shows the mousePosition detection frequency in 180-200 fps. 图像显示mousePosition检测频率为180-200 fps。 On phones it is even slower! 在手机上它甚至更慢! 在此输入图像描述

Upd: Here is my simplified code. Upd:这是我的简化代码。

void Draw() //this method is invoked every frame
     {
         //some calculations of x and y based on Input variables
         currentMousePosition = new Vector2( x, y); //current mouse position on sprite
         if(currentMousePosition != previousMousePosition)
             {
                 while(currentMousePosition != previousMousePosition)
                 {
                 mySprite.texture.SetPixels((int)previousMousePosition.x, (int)previousMousePosition.y, 3,3, myColorArray);
                                 if (currentFrameMousePos.x > previousFrameMousePos.x)
                                     previousFrameMousePos.x++;
                                 if (currentFrameMousePos.x < previousFrameMousePos.x)
                                     previousFrameMousePos.x--;
                                 if (currentFrameMousePos.y > previousFrameMousePos.y)
                                     previousFrameMousePos.y++;
                                 if (currentFrameMousePos.y < previousFrameMousePos.y)
                                     previousFrameMousePos.y--;
                   }
             } else mySprite.texture.SetPixels((int)currentMousePosition.x, (int)currentMousePosition.y, 3,3, myColorArray);
             previousMousePosition = currentMousePosition;
     }
     //mySprite.texture.Apply() is invoked independently in another place to improve performance

The issue is, it is not possible to queue up touch positions that occurred mid frame so by "Quickly" sliding your finger you will miss certain texels on your image. 问题是,不可能排队发生在帧中间的触摸位置,因此通过“快速”滑动手指,您将错过图像上的某些纹素。 You should look at this line formula Bresenham's line algorithm . 你应该看看这个线公式Bresenham的线算法 This is super fast, and all integer math. 这是超快的,所有整数数学。 Inside your Update() function call this method. 在Update()函数内部调用此方法。

    Vector2 oldPoint;
    public void UpdateDrawPoint(Vector2 newPoint){
        BresenhamLine(newPoint, oldPoint);
        oldPoint = newPoint;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM