Author Topic: [SOLVED] Something wrong with these FadeIn/Out methods?  (Read 2373 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
[SOLVED] Something wrong with these FadeIn/Out methods?
« on: September 24, 2013, 11:05:28 AM »
I wrote a method to animate a panel's widgets alpha:

  1.         public static UITweener AnimateAlpha(UIPanel panel, float animTime, float from, float to)
  2.         {
  3.                 int len = panel.widgets.size;
  4.                 if (len > 0)
  5.                 {
  6.                         for (int i = 0; i < len-1; i++)
  7.                         {
  8.                                 var w = panel.widgets[i];
  9.                                 TweenAlpha.Begin(w.gameObject, animTime, to);
  10.                         }
  11.                         return TweenAlpha.Begin(panel.widgets[panel.widgets.size - 1].gameObject, animTime, to);
  12.                 }
  13.                 return null;
  14.         }
  15.  

And this (which I give it something like myPanel.GetComponentsInChildren<UIPanel>() just to go recursively down):

  1.         public static UITweener AnimateAlpha(UIPanel[] panels, float animTime, float from, float to)
  2.         {
  3.                 int len = panels.Length;
  4.                 if (len > 0)
  5.                 {
  6.                         for (int i = 0; i < len - 1; i++)
  7.                         {
  8.                                 var panel = panels[i];
  9.                                 AnimateAlpha(panel, animTime, @from, to);
  10.                         }
  11.                         return AnimateAlpha(panels[panels.Length - 1], animTime, @from, to);
  12.                 }
  13.                 return null;
  14.         }
  15.  

I use it like this:

  1.         private void FadeIn()
  2.         {
  3.                 float from = 0.01f, to = 1f;
  4.                 inventoryPanel.gameObject.SetActive(true);
  5.                 UIWidget.AnimateAlpha(inventoryPanel.GetComponentsInChildren<UIPanel>(), animTime, from, to);
  6.         }
  7.         private void FadeOut()
  8.         {
  9.                 float from = 1f, to = 0.01f;
  10.                 UIWidget.AnimateAlpha(inventoryPanel.GetComponentsInChildren<UIPanel>(), animTime, from, to).onFinished += OnFadeOutFinished;
  11.         }
  12.  
  13.         private void OnFadeOutFinished(UITweener tweener)
  14.         {
  15.                 inventoryPanel.gameObject.SetActive(false);
  16.         }
  17.  

Now it works well when fading out, however when fading in things go a little bit funky, and strange, the panel gets active, I see the widgets alpha at full, but I don't see them. They become visible only when I hover over them (See attachment)

Any idea what's going on? - Is there a better way to fade in/out?

Thanks.

« Last Edit: September 25, 2013, 08:02:55 AM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Something wrong with these FadeIn/Out methods?
« Reply #1 on: September 24, 2013, 01:08:17 PM »
What for? TweenAlpha can already tween panels, and there is no need to do it recursively. Parent panel's alpha affects children.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Something wrong with these FadeIn/Out methods?
« Reply #2 on: September 25, 2013, 01:12:19 AM »
Thanks a bunch. Ahh, I totally forgot about panels' alpha property. I just tried it out, it seems that it doesn't affect children panels.

So I guess I have to do this?

  1. var panels = GetComponentsInChildren<UIPanel>();
  2. foreach (var p in panels)
  3.     // tweak p.alpha...
  4.  

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Something wrong with these FadeIn/Out methods?
« Reply #3 on: September 25, 2013, 01:36:23 AM »
OK thanks I fixed I used a combination of both the methods.

I know you've mentioned this somewhere but I can't remember where, why doesn't a UITexture change its alpha?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [SOLVED] Something wrong with these FadeIn/Out methods?
« Reply #4 on: September 25, 2013, 07:48:07 PM »
Panel alpha affects children panels.

UITextures must be using Unlit/Transparent Colored shader in order to be affected by alpha.