Author Topic: Problem when adding item to scrollview by code  (Read 9374 times)

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Problem when adding item to scrollview by code
« on: August 14, 2014, 08:43:06 AM »
Hi.

So I am testing adding items to scroll view via code when runtime.

I searched little, wrote like below with NGUI example7-Scroll View(Panel) scene.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ScrollStudy : MonoBehaviour {
  5.  
  6.         public UIGrid grid;
  7.         public UIPanel scroll;
  8.         void Start () {
  9.         }
  10.         public GameObject pref;
  11.         GameObject newItem;
  12.         void Update () {
  13.                 if(Input.GetKeyDown("q")){
  14.                         Generate();
  15.                 }
  16.         }
  17.         public void Generate(){
  18.                 newItem = GameObject.Instantiate(pref, transform.position, transform.rotation) as GameObject;
  19.                 newItem.transform.parent = grid.gameObject.transform;
  20.                 grid.Reposition();
  21.                 NGUITools.MarkParentAsChanged(grid.gameObject);
  22.                 newItem.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
  23.         }
  24. }
  25.  

but when add new item, some existing item shows abnormal behaviour like disappearing etc.

see this video, http://youtu.be/xTVx_C8tF6k

Thanks.


Rajken

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 12
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #1 on: August 15, 2014, 02:41:55 AM »
Hi,
I think the problem could be that you are setting the scale to late or that you are passing in a rotation to the Instatiate.
An easier way of adding children to a gameobject is to use the function NGUITools.AddChild. I would also just call the grid.repositionNow = true; instead of the grid.Reposition(); because then its done in the next update.

So it should look something like this.

  1. public UIScrollView scrollView;
  2. public void Generate()
  3. {
  4.     // Adds a new instance of the pref to grid
  5.     newItem = NGUITools.AddChild(grid.gameobject, pref);
  6.     // Do what you want with newItem like setting anchor or other things
  7.     // Will update the position for all children in the grid at next update or lateupdate
  8.     grid.repositionNow = true;
  9.  
  10.     // Good to also call ResetPosition from the scrollview to Reset Clipping Position
  11.     scrollView.ResetPosition ()
  12. }
  13.  

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #2 on: August 15, 2014, 02:56:06 AM »
Thx for reply, but your method also does not work, problem stills.

Instead I found disappeared gameobject can be return by just turn it off and on again.

So though ignorant method, I create custom method like below, call after grid.Reposition();
and problem gone.

  1. Transform[] gos;
  2. public void RefreshGo(){
  3.                 gos = grid.gameObject.GetComponentsInChildren<Transform>();
  4.                 foreach(Transform go in gos){
  5.                         go.gameObject.SetActive(false);
  6.                         go.gameObject.SetActive(true);
  7.                 }
  8.         }

Rajken

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 12
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #3 on: August 15, 2014, 04:08:53 AM »
:(
Is the prefab that is created disabled at start?
Or did you do any changes to the hierachy for that example?
Or check if there is a widget in the same gameobject as the Grid component? There should not be one.

I use a an older version then the latest one on the server but never needed to do that. Also calling GetComponentsInChildren is not good for performance.

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #4 on: August 15, 2014, 05:07:25 AM »
I just tested with NGUI official example7, UIGrid have no widget.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #5 on: August 15, 2014, 08:05:03 AM »
Instantiating new UI elements doesn't add them to the managing panel right away, so resetting the scroll view so soon does nothing because those widgets don't actually exist yet as far as the scroll view is concerned.

You need to call CreatePanel() on the widgets if you want them to be registered right away.

So NGUITools.AddChild the item, gameObject.BroadcastMessage("CreatePanel"), then Reposition the grid and ResetPosition the scroll view.

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #6 on: August 15, 2014, 09:00:32 AM »
Instantiating new UI elements doesn't add them to the managing panel right away, so resetting the scroll view so soon does nothing because those widgets don't actually exist yet as far as the scroll view is concerned.

You need to call CreatePanel() on the widgets if you want them to be registered right away.

So NGUITools.AddChild the item, gameObject.BroadcastMessage("CreatePanel"), then Reposition the grid and ResetPosition the scroll view.

  1. NGUITools.AddChild(grid.gameObject, pref);
  2.                 grid.gameObject.BroadcastMessage("CreatePanel");
  3.                 scroll.gameObject.BroadcastMessage("CreatePanel");
  4.                 grid.Reposition();
  5.                 scroll.ResetPosition();
  6.  

I tried like above, but problem stand still. Until I drag and make it clip, sprite does not show well.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #7 on: August 15, 2014, 09:38:24 AM »
1. I opened the Example 7.
2. Deleted all items except the first one.
3. Made a prefab out of the first item.
4. Attached this script to the grid:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public GameObject prefab;
  6.  
  7.         void Update ()
  8.         {
  9.                 if (Input.GetKeyDown(KeyCode.C))
  10.                 {
  11.                         GameObject go = NGUITools.AddChild(gameObject, prefab);
  12.                         go.BroadcastMessage("CreatePanel");
  13.                         GetComponent<UIGrid>().Reposition();
  14.                         GetComponentInParent<UIScrollView>().ResetPosition();
  15.                 }
  16.         }
  17. }
5. Referenced the prefab from #3 on the script as the "prefab".
6. Hit Play(), Press 'C' a few times. items get added, scroll view gets updated, scroll bar gets updated. Everything works as expected.

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #8 on: August 15, 2014, 08:25:14 PM »
thx.

very strange...

I also tested scene copied from example7, and at there I followed your way, but problem occur still.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #9 on: August 16, 2014, 10:03:04 AM »
What version of NGUI?

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: Problem when adding item to scrollview by code
« Reply #10 on: August 16, 2014, 10:21:00 AM »