unity3d - Unity 2D: dynamically create a hole in a image -
i using unity 2d , trying achieve simple effect. making candy crush saga - game.
i have grid items. in every level grid field can have different shapes, created @ runtime (a dark grey area in middle of background). background still image (blue sky , green hills). when pieces (for example blue pentagon) fall down top must hidden until enter grid field area (dark grey); in practice background image (sky , hills) no more background, foreground hole represented grey area. grey field composed tiles sprite sheet.
i have prepared picture, cannot load here unfortunately yet.
i have no idea how achieve effect in unity. have idea?
the simple solution create static levels graphics hole, not want create them because waste of time , waste of memory, want able create effect @ runtime.
i thinking creating dynamic bitmap mask hole shape using sprite sheet. using bitmap mask material example applied image in foreground , make hole.
i hope clear enough.
thank you! camillo
click on texture in unity editor
change "texture type" "texture" "advanced"
check "read/write enabled" checkbox
change "format" form "automatic compressed" "rgba 32 bit"
i attached component raw image (you can attach else, change "rawimage image" part)
this create hole dimensions 100x100 @ position 10,10 of image, make sure texture @ least 110x110 large.
using unityengine; using unityengine.ui; public class holeinimage : monobehaviour { public rawimage image; void start() { // change original: texture2d texture = image.texture texture2d; // use change copy (and not original) //texture2d texture = instantiate(image.maintexture) texture2d; //image.maintexture = texture; color[] colors = new color[100*100]; for( int = 0; < 100*100; ++i ) colors[i] = color.clear; texture.setpixels( 10, 10, 100, 100, colors ); texture.apply(false); } }
edit:
hole defined 1 or more sprites:
do same these sprites: (advanced, read/write enabled, rgba 32 bit)
for example: if sprite white , hole defined black:
for( int = 0; < 100*100; ++i ) colors[i] = color.clear;
change to:
texture2d holetex; // set in editor, must have same dimensions (100x100 in example) color[] hole = holetex.getpixels(); for( int = 0; < 100*100; ++i ) { if( hole[i] == color.black ) // sprite black, there hole colors[i] = color.clear; }
Comments
Post a Comment