Author Topic: Runtime widget changes not appearing  (Read 5730 times)

beck

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Runtime widget changes not appearing
« on: February 05, 2014, 05:24:58 PM »
Hi, I've been having an issue using NGUI that has been really frustrating. When I make changes to a sprite, occasionally they do not appear immediately. When run in background is disabled, I can trigger an update by switching to a different application and then switching back. However, if run in background is enabled, I must move the sprite to cause changes to appear. Some examples of changes that I am making are enabling/disabling a widget, and changing alpha. I'm using Unity 3.5.7 and NGUI 3.0.9.

The objects where this problem occurs tend to be prefabs which I have spawned. Here is the code I'm using to spawn a prefab into my NGUI environment:

  1. public static UIWidget SpawnPrefab (UIWidget prefab, Transform parent, UIPanel panel)
  2. {
  3.         var instance = GameObject.Instantiate(prefab, parent.position, parent.rotation) as UIWidget;
  4.         instance.transform.parent = parent;
  5.         instance.transform.localScale = Vector3.one;
  6.        
  7.         panel.AddWidget(instance);
  8.        
  9.         return instance;
  10. }
  11.  

Can you discern from this info why I might be having this issue?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Runtime widget changes not appearing
« Reply #1 on: February 05, 2014, 09:18:37 PM »
Please update to the latest version.

And never use GameObject.Instantiate. You need to use NGUITools.AddChild(parent, prefab) instead. Not only does it reset the position and scale for you, but more importantly -- it also sets the child's layer properly to that of the parent.

Also... why are you adding this widget to your panel? You should not be doing that. It's all done for you, and doing it yourself will only lead to issues. Your function should be simply:
  1. return NGUITools.AddChild(parent.gameObject, prefab);
In other words, don't even need a separate function for it.

beck

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Runtime widget changes not appearing
« Reply #2 on: February 06, 2014, 12:41:52 PM »
Thanks for the response. Indeed, the AddWidget code was an attempt to fix the problems I was seeing.

I've changed my spawn code to use NGUITools.AddChild instead as you've suggested. However, the problem still occurs. Do you have any other ideas as to why this might be happening?

beck

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Runtime widget changes not appearing
« Reply #3 on: February 06, 2014, 12:51:13 PM »
Just wanted to mention, I just upgraded this part of the project to 4.2 and the problem still occurs. One of the main instances of the problem is when I set the alpha value of a parent widget - some of the children do not update until the object is nudged or until I change window focus.

Edit:
After some probing it appears that the mIsVisible variable on the sprites that are supposed to render is getting set to false. This only seems to be changed from UIPanel.UpdateWidgets() when it calls UpdateVisibility on every UIWidget. For some reason, the sprites are being sent "false" to that function, which disables their rendering.
I tested the conditions that generate that false value and it appears that it's caused by UIWidget.CalculateCumulativeAlpha is returning a value less than 0.001 (presumably 0). I'm not sure why this would be the case. I've looked through that sprite's finalAlpha value and all of it's parents up to the UIPanel and they are all set to 1. Could this be an ordering problem? Maybe the sprite isn't receiving its final alpha soon enough?

Edit2:
Although I previously thought you had suggested that I upgrade my Unity version, it appears I am behind on my NGUI version and the changelog indicates that this problem may be solved. I'm downloading the latest now and I'll edit this post with my results.

Edit3:
Upgrading to the latest NGUI version solved the problem. Thanks!
« Last Edit: February 06, 2014, 02:36:42 PM by beck »