Author Topic: using pools of ui elements problem...read on! (no addchild issue)  (Read 2047 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Hi! I am trying to use pools of ui prefab elements to avoid constant instantiation and destroy of objects. Everything works fine,
i am spawning elements to table which is under scrollview, i also modified NGUITools.AddChild method (rather made another one) that
doesn't create gameobject but uses one that is passed (spawned beforehand).

here is the modified method:

  1. static public GameObject AddChild(this GameObject parent, GameObject prefab)
  2.         {
  3.             if (prefab != null && parent != null)
  4.             {
  5.                 var t = prefab.transform;
  6.                 t.parent = parent.transform;
  7.                 t.localPosition = Vector3.zero;
  8.                 t.localRotation = Quaternion.identity;
  9.                 t.localScale = Vector3.one;
  10.                 prefab.layer = parent.layer;
  11.             }
  12.             return prefab;
  13.         }

not much going on here.

here is the method i am using to spawn elements to table:
  1. ...
  2. for (var i = 0; i < weaponConfigs.Length; i++)
  3.         {
  4.             var pooledWeaponItem = Game.Pools.GetPool(PoolType.UI).Spawn(Item.transform, Vector3.zero, Quaternion.identity);
  5.            
  6.             var item = UIExtensions.AddChild(_table.gameObject, pooledWeaponItem.gameObject);
  7.  
  8.             var controller = item.GetComponent<WeaponItem>();
  9. ....
  10. }
  11.  

and everything works fine but the problem is when element that was in the pool and need to be reused is spawned to table but is not visible (this happens
depending on how much elements are in the pool...it happens earlier if there is less elements in the pool)

I figured out when i disable and enable again sprite it appears visible and then when i disable and enable gameobject of the spawned item itself it appears
in the table and starts working.

i tried
  1.         _scroll.panel.SetDirty();
  2.         _scroll.panel.RebuildAllDrawCalls();
  3.  

but it doesn't work, also i tried
different combinations of
NGUITools.SetActive for spawned element but it doesn't work...what seems to be the problem? Any ideas?

thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: using pools of ui elements problem...read on! (no addchild issue)
« Reply #1 on: January 09, 2016, 10:46:22 AM »
widget.MarkParentAsChanged()

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: using pools of ui elements problem...read on! (no addchild issue)
« Reply #2 on: January 09, 2016, 11:44:54 AM »
Thanks! This goes to _scroll.panel.MarkParentAsChanged();
Right?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: using pools of ui elements problem...read on! (no addchild issue)
« Reply #3 on: January 11, 2016, 04:04:04 PM »
It should be called on whatever you've reparented. You can also just do a broadcast on the object to inform all of its children.