Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: cfj on July 11, 2012, 07:11:45 PM

Title: Draggable UIPanel not seeing added widgets
Post by: cfj 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? 

Title: Re: Draggable UIPanel not seeing added widgets
Post by: simon129 on July 11, 2012, 09:56:07 PM
after moving in/out clipped panel, call UIWidget.CheckParent(), I fixed it in my project.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: joreldraw on July 12, 2012, 04:15:21 AM
Where is UIWidget.CheckParent() or how to do?
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: joreldraw 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
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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?
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj 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?
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj 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?
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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. :)
Title: Re: Draggable UIPanel not seeing added widgets
Post by: Disastercake 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook 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.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: Disastercake on July 12, 2012, 07:50:08 PM
I see this line:

  1. mTrans.BroadcastMessage("CheckParent", SendMessageOptions.DontRequireReceiver);
  2.  

What is the difference between Broadcast and mTrans.Broadcast in that script?  Does Broadcast only broadcast to a specified object that the method instance belongs to?
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook on July 12, 2012, 08:11:17 PM
I was just referring to BroadcastMessage.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj on July 12, 2012, 09:47:23 PM
It is not just the Invoke method.  I did some more research. 

When using a Draggable UIPanel, the ONLY time I was able to get the button to render when using NGUITools.AddChild was using it in Awake, Start or the FIRST CALL of Update (first frame).  I thought calling it in Update was fine but I only tried it on the first frame.  If you call it in Update at any other time either by using a counter, using Invoke, or using a GetKey command to execute the command after the first frame, it will not render the button.  It creates it, but will not render it until you scroll the UIPanel or adjust it in some way.

Knowing this, is there a way to create widgets dynamically on draggable UIPanels and have the widgets render without adjusting the panel?


Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj on July 12, 2012, 11:14:53 PM
Finally thought to look at the code of UIDraggablePanel (head ... bang ... on ... desk).  I got it working by using ResetPosition().  Although it is not ideal since it is moving the clipping box around where I don't want it to be at the top left of the widgets.  I could also get it working by using Scroll(0.001f) but that seems very hackish to me.

It seems to me this works because it replicates the behavior that causes it to finally render. 
1) ResetPosition adjusts the clipping box (which is the same as me doing it in the inspector) which causes it to render.
2) Scroll(0.001f) literally scrolls the panel just a bit (which is the same as me scrolling it in game) which causes it to render.

Out of curiosity, is there a way to get new widgets in a draggable panel to render without scrolling it or adjusting the clipping box?

Title: Re: Draggable UIPanel not seeing added widgets
Post by: joreldraw on July 13, 2012, 03:29:23 AM
Is this your problem?

I have same issue and i not add in execution time, my hierachy is predefined in editor.

Video: http://www.tasharen.com/forum/index.php?action=dlattach;topic=641.0;attach=213 (http://www.tasharen.com/forum/index.php?action=dlattach;topic=641.0;attach=213)
Post: http://www.tasharen.com/forum/index.php?topic=641.msg3292#msg3292 (http://www.tasharen.com/forum/index.php?topic=641.msg3292#msg3292)

I try refreshing table, repositioning and a lot of options with the clipping panel and nothing fix. But at this time on Android work fine from first time i open, but not on Desktop.

Pdta: Only appear this fail when the dragable panel not have objects to fill and be dragable.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook on July 13, 2012, 08:45:16 AM
Is your panel marked as "static" perchance? "Static" flag toggled on the UIPanel (on the UIPanel, not the game object) tells the panel that the geometry will not change, and it will ignore your widget changes.
Title: Re: Draggable UIPanel not seeing added widgets
Post by: cfj on July 13, 2012, 01:22:31 PM
Ah Ha!  That worked for me.  I thought I had tried adjusting that before and didn't see a difference.  Oh well, it works now.  Since the hint for Static is "Check if widgets don't move", I just left it on like Example 7 since the widgets don't move, the panel does.  But I understand now that it will prevent dynamic widgets showing.

Thanks!
Title: Re: Draggable UIPanel not seeing added widgets
Post by: ArenMook on July 13, 2012, 02:18:13 PM
Disastercake had the same problem with his project. Because of you two I've added an extra check to the panel that will ensure that this works in the future.