Author Topic: Dynamically Added UIGrid and UITable Children Ignoring Panel Alpha  (Read 3796 times)

Delerium

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
I'm having an issue:

If I dynamically instantiate a prefab as a child of a UIGrid
Then change the UIPanel's alpha
The dynamically instantiated children do not fade with the UIPanel and the rest of the widgets

When I have the prefab as a child of the UIGrid in the Editor, it works fine.

It also doesn't work if I drag the prefab manually and make it a child of the UIGrid at runtime.

I noticed that NData has the same problem with their NguiItemSourceBinding component demo (ItemsList).  None of the UIPanels in the hierarchy of the UITable's alphas will make the dynamically added children fade.

A quick way to duplicate it manually:

NGUI > Create 2D UI
NGUI > Create Panel
NGUI > Create Grid

Drag a prefab from the NGUI > Examples > Atlases > Wooden folder onto the grid.

At runtime, change the Alpha of the panel, and it will fade.
Manually drag another of the same prefab and put it on the Grid
Adjust the alpha again, and the new prefab does not fade

I've also hit execute on the Grid at runtime, and tried it with a UITable as well.

If anyone has any suggestions on what I am doing wrong, please let me know.  If there is something I need to do to the prefab after instantiating it in code, that would be fine for a solution since I am instantiating them from code in my original example and experiencing the same issue as manually adding them in the editor at runtime.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamically Added UIGrid and UITable Children Ignoring Panel Alpha
« Reply #1 on: January 05, 2014, 10:41:16 AM »
Seems to work just fine here. I tried the following:

1. New scene.
2. Created the UI hierarchy (ALT+SHIFT+W, delete the widget)
3. Added a new game object under UIRoot (ALT+SHIFT+N)
4. Attached a UIGrid to it and the following test script:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public GameObject prefab;
  6.  
  7.         void OnEnable ()
  8.         {
  9.                 while (transform.childCount > 0)
  10.                         NGUITools.Destroy(transform.GetChild(0).gameObject);
  11.  
  12.                 NGUITools.AddChild(gameObject, prefab);
  13.                 NGUITools.AddChild(gameObject, prefab);
  14.                 NGUITools.AddChild(gameObject, prefab);
  15.                
  16.                 GetComponent<UIGrid>().Reposition();
  17.         }
  18. }
Hitting Play, then modifying the panel's alpha affects all children as expected. I did notice however, that when I am already playing and happen to drag & drop a prefab, it won't take the panel's alpha into consideration.

I've fixed it locally by making mParentFound variable in UIRect 'protected', and setting it to 'false' inside UIWidget.CreatePanel inside the "if" statement within.

Delerium

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Dynamically Added UIGrid and UITable Children Ignoring Panel Alpha
« Reply #2 on: January 05, 2014, 04:13:51 PM »
I was still having the issue even in with your example, but updating to the latest version of NGUI resolved the issue.  I'm not sure how old mine was in this project, it may have been a relatively earlier version of 3.  Works great now, thanks!