Search Unity

Punching holes in sprites

Discussion in '2D' started by sonluong1987, Jul 23, 2014.

  1. sonluong1987

    sonluong1987

    Joined:
    May 10, 2014
    Posts:
    8
    Hello, I'm making a game which requires some custom drawing. Basically I have one sprite placed on top of another, and I want to programmatically create multiple rectangle holes in the top sprite in order to reveal the one underneath as the game goes on. What do I need to do in order to achieve this? Thank you.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    either render-to-texture (rendertextures in pro, or readpixels in free), or draw to the texture in main memory (e.g. byte array) and do setpixels32() and apply() to upload the changes. .. maybe breaking one big texture into smaller ones and only uploading the ones which changed (dirty rects algorithm). Or maybe have lots of rectangle sprites and just remove some to create holes?
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You could use a mask shader and modify the masking layer.
     
  4. sonluong1987

    sonluong1987

    Joined:
    May 10, 2014
    Posts:
    8
    I have tried the SetPixels32() method and it's working out great. I'm a total shader newbie so I guess I'll go the texture way. Hopefully it won't cause a performance hit. Thank you both for your help, you have saved me a lot of time :) !
     
  5. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    SetPixels32 is definitely relatively slow but it may be fast enough for your needs.