Painting issue, drawing is not consistent, how to fix?

Hello everyone! Im currently trying to paint a texture, a bit of code and it works, but i have a problem, if i move my mouse too quickly, painting has gaps, how can i fix this? Thanks in advance!

 Texture2D tex = GetComponent<Image>().sprite.texture;
         RectTransform rect = GetComponent<RectTransform>();  
         Vector2 pos;  
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, null, out pos);
         pos = new Vector2(pos.x + rect.sizeDelta.x / 2, pos.y + rect.sizeDelta.y / 2);
         for (int i = 0; i < brushSize; i++)
         {
             for (int j = 0; j < brushSize; j++)
             {
                 tex.SetPixel(Mathf.FloorToInt(pos.x - 1 * brushSize / 2 + j), Mathf.FloorToInt(pos.y + 1 * brushSize / 2 - i), color);
             }
         }   
         tex.Apply();

Looks pretty consistent- it’s painting a square texture area once per frame, so moving faster means more distance between Last and Current positions in a single frame, leaving gaps. If you want to paint smoothly, you’ll need to create an imaginary line between Last and Current positions, then paint that texture every X distance traveled along that line, where X is the distance of 1 pixel (in screen coordinate measurements).

This means that dragging quickly from one side to the other can run this method dozens of times in a single frame, which may not be very pretty as far as performance goes (I don’t think SetPixel is cheap), but I’m unaware of ways to optimize it except to say you’ll likely need to keep track of each pixel you’ve filled in a single frame and check against that list each paint operation so that you don’t use SetPixel more than one per location.

Hope that helps a bit.