Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

texture.SetPixel slow

Discussion in '2D' started by DJVDJV, Jan 29, 2019.

  1. DJVDJV

    DJVDJV

    Joined:
    Sep 11, 2014
    Posts:
    70
    Yo,

    I need to draw circle shape on texture.

    I use now: texture.SetPixel..But it is super slow.

    as:
    public static void Circle(this Texture2D tex, int x, int y, int r, Color color)
    {

    float rSquared = r * r;

    for (int u = 0; u < tex.width; u++)
    {
    for (int v = 0; v < tex.height; v++)
    {
    if ((x - u) * (x - u) + (y - v) * (y - v) < rSquared)
    {
    tex.SetPixel(u, v, color);
    }
    }
    }
    }


    I saw: texture.SetPixels32 also...
    But I have no idea how to draw a circle with it?


    So:
    Is there a more efficient way to draw pixels on texture (if I want circle) that first one?

    PS:
    In my situation, I can NOT use shaders or line drawings
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    I tried doing this for days and gave up, there is a free asset called free draw that you could look through the code and see how it works, take snippets, and learn from.