Search Unity

Instantiating 1000+ sprites

Discussion in '2D' started by vdKa, Dec 18, 2017.

  1. vdKa

    vdKa

    Joined:
    Jan 22, 2014
    Posts:
    13
    Hi,
    Im working on a painting book project.
    I have coupe of brushes.
    One is, I instantiate prefabs on mouse position to draw. But with time I face with performance issues. Because there are a lot of intanstiated object on the screen. Tris and verts increase a lot if user draws a lot of things.
    Is there a way to achieve this issue?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, you shouldn't be instantiating sprites to paint. You should instead be updating a texture. PixelSurface (an asset I offer through the Asset Store, as well as on DigitalGift.Shop) is designed for this sort of thing, if you don't want to work out the code to update a texture yourself.

    Best,
    - Joe
     
  3. vdKa

    vdKa

    Joined:
    Jan 22, 2014
    Posts:
    13
    Hi Joe,
    Thank you for your reply.
    I can update textures by pixel. But how can I do that for a shape. I mean I can update pixels under mouse pos but how can I change pixels like shape ( brush) I use?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Use a loop (or a pair of nested loops) to update many pixels at once. For a simple square brush, just loop from (mousePos - halfWidth) to (mousePos + halfWidth) on both X and Y. For a round brush, only update pixels that are within brushRadius of mousePos. Etc.
     
  5. vdKa

    vdKa

    Joined:
    Jan 22, 2014
    Posts:
    13
    But the problem is my brushes are not simply shaped. I attached one of them.
     

    Attached Files:

  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    In that case you'll want to draw the brush texture into the painting texture. You still do this with a double loop, just copying the pixels where the brush texture alpha is nonzero (or, applying paint in proportion to the alpha value).