Author Topic: Draggable UIPanel not seeing added widgets  (Read 17081 times)

cfj

  • Guest
Draggable UIPanel not seeing added widgets
« on: July 11, 2012, 07:11:45 PM »
I have a draggable UIPanel setup that I dynamically add buttons to using NGUITools.AddChild(testUIGrid, testPrefab).  I see the buttons being created in the scene.  I can click them and even hover the mouse over them.  But, they do not render until I scroll the draggable panel or adjust the clipping center or size on the UIPanel.  Before I scroll or adjust the clipping, I also see the UIPanel shows zero widgets so it as if the UIPanel does not know that these widgets exist. 

Do I need to do another step for the UIPanel to be aware of and render these new widgets? 


simon129

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 20
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #1 on: July 11, 2012, 09:56:07 PM »
after moving in/out clipped panel, call UIWidget.CheckParent(), I fixed it in my project.

joreldraw

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #2 on: July 12, 2012, 04:15:21 AM »
Where is UIWidget.CheckParent() or how to do?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #3 on: July 12, 2012, 04:44:32 AM »
It's a function in UIWidget. It tells the widget that its parent may have been changed and that it should check to make sure, and re-add itself to the panel if that was the case.

It's mainly only needed if you move the widget from one panel to another. I've never had to use it after NGUITools.AddChild.

joreldraw

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #4 on: July 12, 2012, 05:23:24 AM »
This is not a public function. How i use this on a Table hierachy?
I like to test if this solve my table problem.

Thanks in advance

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #5 on: July 12, 2012, 08:38:21 AM »
It has been made public in 2.1.0, so just make it public on your end.

cfj

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #6 on: July 12, 2012, 10:20:45 AM »
I tried using UIWidget.CheckParent and it did not work. 

Also, I found out that the UIPanel showing zero widgets really does not mean anything since it is a refresh issue.  While running the game if I click on another object and then click back on the panel, the inspector shows the correct number of widgets (still does not render them though).  I saw this behavior with other panels that don't have this issue so it is unrelated.

I just cannot get the buttons to render unless I scroll the panel in game or adjust the clipping Center or Size on the panel in the inspector.

My Hierarchy is the following:

Tracker (script that adds the buttons)
|
-> Panel Contents (UIPanel / UIDraggable Panel)
    |
    -> UIGrid (UIGrid)
        |
        -> Button Objects (UIDrag Panel Contents / UIButton / etc)
|
-> Panel Frame (UIPanel)
    |
    -> Sprite (UISlicedSprite / UIDrag Panel Contents / Collider)

Can I provide anything else to try and figure this out?  Many thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #7 on: July 12, 2012, 10:31:40 AM »
Sounds like your clipped panel isn't positioned where it should be, or perhaps you're instantiating buttons using Object.Instantiate instead of NGUITools.AddChild. Also, why is the script that adds the buttons on the bottom-most object? Shouldn't it be on the UIGrid object so it adds them as children of that object?

cfj

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #8 on: July 12, 2012, 11:03:49 AM »
As far as I can tell, the Clipping setting for the panel holding the contents is exactly where I want it.  In addition, I still get the no render issue even with the Clipping turned off.

I am using Button prefabs and NGUITools.AddChild(myUIGrid, buttonPrefab).

I didn't think it mattered where the script was that added the buttons (where I have it I would call it the top most part of the hierarchy).  I did move it to my UIGrid gameobject but still got the buttons not rendering issue.

Anything else I can provide or check?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #9 on: July 12, 2012, 11:06:05 AM »
I can't think of anything. I add plenty of things dynamically (orc inventory example adds all items and creates the backpack dynamically, for example), so I know it works. You must be missing something obvious.

cfj

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #10 on: July 12, 2012, 11:28:03 AM »
OK, I tried breaking the problem down and got some info that may help.

I CAN get the buttons to render if I call NGUITools.AddChild from Awake, Start or in an Update.
I CANNOT get the buttons to render if I call NGUITools.AddChild from a custom public function that is called either through a button press seen in another script or using Unity's Invoke or InvokeRepeating function.

For example, the following code will not render my button until I scroll the panel:

Start()
{
  InvokeRepeating("Test", 1, 10);  // same with Invoke as well
}

Test()
{
  myButton = NGUITools.AddChild(myUIGrid, buttonPrefab);
}

Does this help?

cfj

  • Guest
Re: Draggable UIPanel not seeing added widgets
« Reply #11 on: July 12, 2012, 02:00:38 PM »
I was able to replicate the issue with the NGUI Example 7 Scroll View (Panel) Scene.

I removed all the item objects under UIGrid.  Created a prefab from one of them and then attached the following script to UIGrid:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
   public GameObject testPrefab;
   public GameObject testUIGrid;
   
   void Start () {
      Invoke("TestInvoke", 1);
   }

   
   void Update () {}
   
   void TestInvoke() {
      GameObject go = NGUITools.AddChild(testUIGrid, testPrefab);
   }
}

This causes the rendering issue I am talking about.  However, if you put the NGUITools.AddChild directly in Start, you do not get the issue.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #12 on: July 12, 2012, 04:44:28 PM »
Can't say I've ever used the Invoke function... ever.

But if it doesn't work with this approach then the most obvious solution is: don't use it. :)

Disastercake

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 87
  • www.disastercake.com
    • View Profile
    • Disastercake
Re: Draggable UIPanel not seeing added widgets
« Reply #13 on: July 12, 2012, 07:32:47 PM »
It's a function in UIWidget. It tells the widget that its parent may have been changed and that it should check to make sure, and re-add itself to the panel if that was the case.

It's mainly only needed if you move the widget from one panel to another. I've never had to use it after NGUITools.AddChild.

I need some example code to understand how to use this method.  I can't find it anywhere, even inside UIWidget.  Would like to try it to see if it fixes my issue.
Creator of Soul Saga.
http://www.disastercake.com

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Draggable UIPanel not seeing added widgets
« Reply #14 on: July 12, 2012, 07:38:13 PM »
I need some example code to understand how to use this method.  I can't find it anywhere, even inside UIWidget.  Would like to try it to see if it fixes my issue.
It's a private method in UIWidget. You call it by using Broadcast. Check DragDropItem.cs, line 87.