Author Topic: UIScrollview with dynamic content and clip  (Read 4692 times)

e-tip

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 10
    • View Profile
UIScrollview with dynamic content and clip
« on: July 09, 2014, 04:18:32 AM »
Hi everyone!
i'm facing some refresh issues with a menu i'm developing
The situation is this

I' have an UIPanel ( red stroke in the image ) that clips an area of 400x1476
as a child of this uipanel i have an UIWidget that i use for tweening.
as child of this UIWidget i have two UIScrollviews that are one on the side of the other.
content of the scrollviews are generated dynamically via code.
when i click on a button on the first scrollview i tween the UIWidget 400 px on the right moving the second scrollview inside the clip area.
The problem here is that in some cases not all my buttons are generated as you can see in this image.

in this case you can see on the left what i get and on the right what should i get.
i've done a function
  1.         public void updatepanels (){
  2.                 materialScrollView.SendMessage ("OnEnable");
  3.                 subMaterialScrollView.SendMessage ("OnEnable");
  4.         }
and call this when i begin my animation seems to solve in part the problem as this resolves the issue only in the editor while in the device is still present
Does anyone have a definitive solution ? 
« Last Edit: July 09, 2014, 08:33:43 AM by e-tip »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollview with dynamic content and clip
« Reply #1 on: July 09, 2014, 05:23:31 PM »
There is never any reason to call OnEnable. It's called whenever you add widgets. Note that you must be using NGUITools.AddChild to instantiate elements, not Instantiate(). What code are you using to create them?

e-tip

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UIScrollview with dynamic content and clip
« Reply #2 on: July 10, 2014, 02:58:21 AM »
Hi ArenMook, thank you for your reply.
If i would not call onEnable the situation is even worst than what i'm getting now.
The code i use to add prefabs to the grid is the following
  1. BetterList< Transform > childs = subMaterialGrid.GetComponentInChildren<UIGrid> ().GetChildList ();
  2.                         if (childs.size > 0) {
  3.                                 for (int i = 0; i < childs.size; i++) {
  4.                                         DestroyImmediate (childs [i].gameObject);
  5.                                 }
  6.                         }
  7.                         GClasses.MaterialConfig[] availableTextures = book_manager.GetComponent<book_manager> ().getAvailableTexturesForMaterial (sender.GetComponent<MaterialData> ().materialName ());
  8.                         for (int i = 0; i < availableTextures.Length; i++) {
  9.                                         GameObject button = NGUITools.AddChild (subMaterialGrid, subMaterialbuttonPrefab);
  10.                                         button.GetComponentInChildren<MaterialData> ().setMaterialConfig (availableTextures [i]);
  11.                                         button.GetComponentInChildren<UILabel> ().text = availableTextures [i].description;
  12.                                         button.GetComponentInChildren<UILabel> ().gameObject.AddComponent<UIDragScrollView> ();
  13.                                         UIEventListener.Get (button.GetComponentInChildren<UILabel> ().gameObject).onClick += textureSelected;
  14.                                         button.GetComponentInChildren<UITexture> ().mainTexture = Resources.Load ("Textures/Buttons/btn_" + availableTextures [i].texture_name) as Texture;
  15.                                         button.GetComponentInChildren<UITexture> ().depth = 5;
  16.                         }
  17.                         subMaterialGrid.GetComponentInChildren<UIGrid> ().Reposition ();
after that code i call
  1. menuContainer.GetComponentInChildren<TweenPosition>().PlayForward();
menuContainer is the blue bordered widget.
To display the scrollview on the right.
I've found i hope temporary workaround that is call
  1. if (showingSubMaterialMenu) {
  2.                         NGUITools.SetActive (subMaterialScrollView, false);
  3.                         NGUITools.SetActive (subMaterialScrollView, true);
  4.                 } else {
  5.                         NGUITools.SetActive (materialScrollView, false);
  6.                         NGUITools.SetActive (materialScrollView, true);
  7.                 }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollview with dynamic content and clip
« Reply #3 on: July 10, 2014, 08:58:10 PM »
1. Never use DestroyImmediate. It's pretty much an editor-only function. Check Unity's documentation. Use NGUITools.Destroy.

2. GetComponentInChildren<UILabel>() -- this is terrible practice. If you need to access a child, create a script with public references that you will set to the objects you need. Then retrieving the right references is as simple as GetComponent<YourScript>().someLabel.text = "abc";

3. If you need UIDragScrollView, it should already be there. Why are you adding it?

4. Why do you get UIGrid on a child? subMaterialGrid should already have it, so it should be subMaterialGrid.GetComponent<UIGrid>().Reposition().

e-tip

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UIScrollview with dynamic content and clip
« Reply #4 on: July 31, 2014, 03:15:22 AM »
Thanks ArenMook.
As previously stated i've found a workaround by setting inactive and after that active my container. Maybe it's not the best thing i can do but it works.
Thanks for your precious suggestions