Hello Guest

Author Topic: Changing alpha of a tree of sprite at once?  (Read 4571 times)

mamamia

  • Guest
Changing alpha of a tree of sprite at once?
« on: March 04, 2013, 07:29:35 pm »
Hi,

I'm creating a transition for a set of tabs in my game. When I press a tab button I want to fade out the current tab page and fade in the new page selected.

Each page is composed of a tree of sprites, I mean, a parent gameobject that have a sprite component and have some children that are sprites too (they can have other children sprites, etc...). So, I would like to know if there's any way to change alpha in parent and make it affect children alphas like it happens when modifying parent position, rotation, scale, etc...

The only solution I have found for this is to get all the sprites of the tree with GetComponentsInChildren and set alfa accordly, do anybody know if it could be done more easily?

Thanks in advance
« Last Edit: March 04, 2013, 08:21:03 pm by mamamia »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing alpha of a tree of sprite at once?
« Reply #1 on: March 04, 2013, 10:15:54 pm »
No, there isn't at the moment.


If I needed to do it, I'd do this - on Start on the root object, you create a copy of the material in all children, and switch to one which has a multiplied constant color term. You will then will be able to change the constant color term on the material to change all children. Nice, easy, and super efficient at runtime.

mamamia

  • Guest
Re: Changing alpha of a tree of sprite at once?
« Reply #2 on: March 05, 2013, 09:27:51 am »
Hi!,

I think I got you. You mean you get the material for the collection and a copy is created that is spread at runtime over all the children. Then changing there the alpha of the shared material will affect to all of them. Yup, this is a good solution. The problem is when all sprites does not fit in one collection :/. I think if this happens only solution would be to GetChildren and change alpha of each sprite, don't you?.

Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing alpha of a tree of sprite at once?
« Reply #3 on: March 05, 2013, 09:41:58 pm »
Nah, if you had more than one material, you create a dictionary on the base object, and map each material to its instance counterpart. So each material only appears once in the list. Now you animate color on these materials. Much easier to manage than individual colors.

Big problem with individual colors is that you can't stack them - i.e. if an object had initial alpha = 0.5f, how do you know to scale from there?