- Home /
Changing rect of UI parent with mask component to reveal masked child object
It seems a simple functionality. I have an image child object utilizing the parent's mask component. During runtime, i could change the rect of the parent image to change the size of the mask to reveal more of the child's image, however this changes the child objects size too, so it doesn't really work without resizing the child images rect transform within code in the opposite direction. Is there a simpler way to accomplish this with just the UI components and coding the resizing of the parents size only?
EDIT: I guess within the code every time i adjust the parent's rect transform i could disable and re-enable the mask, though its incredibly un-optimized and ridiculous, especially when adjusting the size every update.
EDIT: It looks like in the 2D experimental preview theres sprite masking without parenting objects... hmm... whats the 2D experimental preview? lol
Answer by elenzil · Dec 09, 2016 at 12:23 AM
i could change the rect of the parent image to change the size of the mask to reveal more of the child's image, however this changes the child objects size too
this strongly suggests you haven't set up the RectTransform anchors & offsets the right way for what you want.
try making the min & max anchor positions all be [0.5, 0.5].
yea i guess your kinda right, ins$$anonymous$$d of anchoring in the middle, i could anchor the child image to one of the edges and then when scaling the parent from the opposite edge the child wont distort. It still doesn't work when scaling from multiple sides or moving the parent masks transform ins$$anonymous$$d just scaling. There was a movable, scalable sprite mask in the 2d preview build and then they replaced it with parent/child masks again. I dont understand why they don't just use a reference to a mask image/sprite ins$$anonymous$$d of needing a heirarchy. Oh well, for my purposes anchoring an edge works, thanks.
EDIT: Actually anchors don't really work, because when using an image ins$$anonymous$$d of a blank mask, no matter how its anchored, the image of the parent, and therefor the mask shape will change and distort. I think there isn't really a working solution because changing the transform will always change the shape of the mask. The only solution is to use no image and have a perfectly square mask, or to use the sprite reveal functionality Unity advertised and never implemented.
Answer by Victory_Class · May 20 at 09:12 PM
I am trying to do a similar thing. I have my mask animated to reveal the child. But the child animates with the mask. Besides counter animating the child to the parent, I don't know any other way to fix this.