Get mousePosition more frequently than every frame?
Hello everyone. I am to create a painting game on Unity. I use SetPixels and Apply methods on a sprite to paint it, and I calculate the pixel under the mouse and pass it into SetPixels method. So my problem is that mouse position is frame-dependent, so it updates only every frame, and I need to update it as fast as possible to draw all every position. I tried doing that in FixedUpdate() and making FixedUpdate more frequent, but anyway Input.mousePosition is updated frame-dependently. I also tried to use Event.current.mousePosition in OnGUI() but it makes no difference, it is still too slowly updated. Even when I have up to 200 fps in Unity Editor there are problems with it anyway and it is far not smooth. Any ideas? Here's my simplified code:
void Draw() //this method is invoked every frame
{
//some calculations of x and y
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
Note: this app has to go to mobile platforms later, so I am thinking to use Input.GetTouch instead of mousePosition, but I have doubts about it being updated faster than mousePosition.
Even though this is quite an old question, I'm interested in how you have addressed this issue.
Your answer
Follow this Question
Related Questions
Mouse position from new InputSystem is not correct and is not the same as Input.mousePosition 0 Answers
How to get mouse click world position in the scene view in editor script? 3 Answers
How to control global mouse 0 Answers
Input.mousePosition have issue for use Touch devices : please help 1 Answer