Author Topic: Deleting and adding items to ScrollView - weird bug  (Read 15379 times)

milox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Deleting and adding items to ScrollView - weird bug
« on: June 17, 2014, 01:00:34 PM »
I'm using newest NGUI on newest Unity 4.5.1 . I have a ScrollView (based on Panel example from NGUI examples, with Grid, UiCenterOnChild  etc) where I add fresh items and delete old ones on every OnEnable event. Previously this worked without any problems:
  1.                                 void ResetList ()
  2.                                 {
  3.                                                 childGrid.Reposition ();
  4.  
  5.  
  6.                                                 foreach (UIDragScrollView ob in gameObject.GetComponentsInChildren<UIDragScrollView>()) {
  7.                        
  8.                                                                 if (ob.gameObject.name != "ModelItem") {
  9.                                                                                 //ob.transform.parent = null;
  10.                                                                                 NGUITools.Destroy (ob.gameObject);
  11.                                                                 }
  12.                        
  13.                                                 }
  14.  
  15.                                                 childGrid.Reposition ();
  16.                                 }

It's called on every OnEnable event
Now, I'm getting a weird error - basically, when it's called, all my UI dissapears and there are a bunch of new UIRoot objects created in the scene - seriously like 10 or so UIRoot objects are instantly created with a child camera, all my UI disappears from Game View - it still exists in hierarchy and in Scene View.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #1 on: June 17, 2014, 02:04:48 PM »
How do you add items? Can you post that code? Make sure you use NGUITools.AddChild(parent, prefab).

milox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #2 on: June 17, 2014, 04:33:28 PM »
I am using NGUITools.AddChild
Here's a fragment of code used to populate the list. It's called in OnEnable, right after clearing the list. The ScrollView list enabled and disabled by simply calling SetActive on a gameobject.

  1. if (cacheList == null) {
  2.                                                                 Debug.LogError ("PopulateList: cacheList is NULL");
  3.                                                                 return;
  4.                                                 }
  5.                                                 if (modelItem == null) {
  6.                        
  7.                                                                 foreach (UIDragScrollView ob in gameObject.GetComponentsInChildren<UIDragScrollView>()) {
  8.  
  9.                                                                                 if (ob.gameObject.name == "ModelItem") {
  10.                                                                                                 modelItem = ob.gameObject;
  11.                                                                                 }
  12.  
  13.                                                                 }
  14.                                                 }
  15.                                                 if (parentModelItem == null) {         
  16.                                                                 parentModelItem = modelItem.transform.parent.gameObject;
  17.        
  18.                                                 } else {
  19.                                                                 modelItem.transform.parent = parentModelItem.transform;
  20.                                                 }
  21.                                                                
  22.                                                 childGrid.Reposition ();
  23.  
  24.  
  25.                                                 switch (PanelType) {
  26.                                                 case PanelType.STORE:
  27.  
  28.                                                                 foreach (ItemInfoSerializableBase item in cacheList) {
  29.  
  30.                                                                                 Debug.Log ("Item Destination: " + item.ItemDestination.ToString ());
  31.                                                                                 if (item.ItemDestination == ItemDestination.MARKET_ONLY) {
  32.                                                                                                 GameObject newItem = NGUITools.AddChild (parentModelItem, modelItem);
  33.                                                                                                 newItem.SetActive (true);
  34.                                                                                                 newItem.name = item.Name;
  35.                                                                                                 UIItemID itId = newItem.AddComponent<UIItemID> ();
  36.                                                                                                 itId.TransformCache = new List<Transform> ();
  37.                                                                                                 itId.TransformCache.AddRange (newItem.GetComponentsInChildren<Transform> ());                                                                  
  38.  
  39.                                                                                                 foreach (Transform t in itId.TransformCache) {
  40.                                                                                                                 switch (t.name.Trim ()) {
  41.                                                                                                                 case ICON_SPOT:
  42.                                                                                                                                 t.GetComponent<UISprite> ().spriteName = item.ItemIcon;
  43.                                                                                                                                 if (item.ItemT == ItemType.Potion) {
  44.                                                                                                                                                 t.GetComponent<UISprite> ().SetDimensions (70, 120);
  45.                                                                                                                                 }
  46.                                                                                                                                 break;

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #3 on: June 18, 2014, 04:55:22 PM »
What is "parentModelItem"? You can only instantiate UI elements underneath a UIPanel. If there is no UIPanel on a parent object, NGUI force-creates one for you, which may even create an entire UI structure to go with it. The instantiated object should already be active, by the way. There is no need to SetActive(true) on it.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #4 on: August 29, 2014, 11:23:14 AM »
I just upgraded to the latest NGUI and am getting the same issue.  And I can't tell from the thread how to solve it, so bumping.

Milox, did you get it working?

ArenMook, I am following the example in Scroll View (Panel).  So the UIPanel is attached to the ScrollView, and the instantiated UI elements are being added and destroyed underneath the UIGrid. 

ScrollView (Panel)
   UIGrid (no Panel)
      Object 1
      Object 2
      etc.

So that's probably why NGUI is now force creating new UIRoots when I use NGUITools.AddChild (UIGrid, newItem).  But how to fix???

Should we add UIPanels to the UIGrids as well?

Or is it better/possible to add/destroy the objects directly underneath ScrollView.  I'm wondering if that will mess up how UIGrid works.

milox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #5 on: August 29, 2014, 12:15:12 PM »
It seems in the latest version of NGUI the problem is fixed. But before I was using this hack to clear old items:
  1.                         void ResetList ()
  2.                                 {
  3.                                        
  4.                                                 List<GameObject> oldItems = new List<GameObject> ();
  5.  
  6.                                                 foreach (UIDragScrollView ob in gameObject.GetComponentsInChildren<UIDragScrollView>()) {                              
  7.                                                                        
  8.                                                                                
  9.                                                                                 ob.gameObject.transform.parent = null;
  10.                                                                                 oldItems.Add (ob.gameObject);
  11.                                                                                 ob.gameObject.SetActive (false);
  12.                                                                
  13.                        
  14.                                                 }
  15.                                                 foreach (GameObject go in oldItems) {
  16.                                                                 NGUITools.Destroy (go);
  17.                                                 }
  18.                                                 childGrid.repositionNow = true;
  19.                                        
  20.                                 }

That worked for me.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #6 on: August 29, 2014, 04:50:53 PM »
It looks from Aren's post that the problem is in adding, not destroying, but thanks for the hack on the destroy bit too.  Odd that you couldn't just unparent and then immediately Destroy ob.  But I'll play with it.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #7 on: September 03, 2014, 10:32:56 AM »
Updating since I finally had a chance to update to Unity 4.5.3 so I have the latest of both Unity and NGUI.  Unfortunately, I still get the phantom UIRoot/Camera adding to my scene.

On the positive side, I have a new idea why.  I have multiple UIScrollViews in my scene.  I am also using the tab pages layout from Example 13, and have a scrollview for the content of each tab.  So the problem might be related to UIToggle and UIToggled Objects interacting with UIScrollview interacting with NGUITools.AddChild

When I launch this scene and do not change tabs, everything works.  But if I change the tabs and then access the script to refresh the content of the scrollviews (both on the active and inactive tabbed pages), I get the phantom UIRoot/Camera.  So I'm guessing trying to AddChild underneath a UIToggled object after UIToggle changes state may be the issue. 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #8 on: September 03, 2014, 11:57:46 AM »
You shouldn't add children to a disabled object, if that's what you're trying to do. If not, I'd need a repro case to have a look at.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #9 on: September 03, 2014, 04:07:49 PM »
That's probably what I'm doing…BUT…

…I'm happy to do it another, better way if you have a suggestion

…it worked fine prior to NGUI 3.5.7 and Unity 4.5.1

…this implies we can't update content on inactive tabs at runtime which doesn't sound right

I'll try to work up a repro, but here are the basics:
   start a scene with Example 13, Tabs
   insert a scrollview from Example 7, Scroll View Panel into the content section of each Tab page
   when the scene is playing, run a script that re-populates the items in the UIGrid for each scroll view.  FYI, my usage scenario is getting the latest list of open and closed matches from the server.
   if you run the scene before switching tabs, things should work.  If you switch tabs before running the script, NGUITools.AddChild will create a phantom UIRoot/Camera, probably because you're trying to add a child to a disabled object on the hidden tab.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #10 on: September 04, 2014, 10:44:20 AM »
Why would you update content of something that's not visible? That doesn't make much sense. You should be updating the content in OnEnable of a script attached to the same game object as the panel you're enabling. Disabled panel should mean that it's not there, and there is no point to update it -- which is done automatically if the script that updates it is on a game object that also gets disabled in the process.

Livealot

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #11 on: September 08, 2014, 10:38:20 AM »
Why you ask?  Two reasons: 
1. I'm a bad coder
2. I'm a lazy coder    :D

The OnEnable approach is much better so thanks for pointing me in that direction.

Since I'm still bad and lazy, I'm now also using ...  if (MyGrid.activeInHierarchy) … before calling any loops on the children of the grid like NGUITools.AddChild or NGUITools.Destroy, or calling Reposition on the grid or ResetPosition on the scrollview

Thanks again for your help!

gg67

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #12 on: November 10, 2014, 02:17:06 AM »
I'm running into a similar (if not the same issue). I'm using OnEnabled() on my Grid gameobject to populate my scroll view with the most recent data. The first time I switch to the tab with the scroll view, everything is great. However, if I click on another tab (disabling the scroll grid list) and then clicking the scroll grid list again, multiple UIRoots are created.

Any idea what's up?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Deleting and adding items to ScrollView - weird bug
« Reply #13 on: November 10, 2014, 08:41:35 PM »
Depends on how you do your instantiation. NGUITools.AddChild should set the parent as soon as it's created. You shouldn't be doing anything in Awake() or OnEnable() of instantiated objects. You should delay it until Start() -- it will ensure that it occurs after the parent has been set.