Author Topic: NGUITools.AddChild and still anchor to screen edges? (Multiple UIRoots?)  (Read 5984 times)

travisschau

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Hi,
I'm using the NGUITools.AddChild() method to dynamically create dialogs from my resources folder within NGUI. The problem is that some of those dialogs need to anchor to the edges of the screen, which I only know how to do by setting the anchors to UIRoot. I can't do that when I'm creating a prefab of a single dialog, since the prefab can't store a reference to my UIRoot instance.

My workaround right now is to intsantiate an entirely new UIRoot prefab with my anchor-requiring dialog embedded in it. This seems hacky to me though, and I'm pretty sure it's causing some touch-event problems I'm having. There must be a better way.

What's the right way to deal with this?

thanks so much,

-Travis

scapegoat57

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUITools.AddChild and still anchor to screen edges? (Multiple UIRoots?)
« Reply #1 on: January 03, 2014, 03:09:37 AM »
The way I get around instantiating prefabs with anchors is by having an inactive instance of the prefab in the scene and calling Instantiate with that. The only draw back is that the copy is inactive, so you have to add SetActive(true) after your Instantiate call.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.AddChild and still anchor to screen edges? (Multiple UIRoots?)
« Reply #2 on: January 03, 2014, 03:51:26 AM »
Why can't you just do
  1. UIRoot root = NGUITools.FindInParents<UIRoot>(gameObject);
  2. widget.SetAnchor(root.transform);
?

travisschau

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: NGUITools.AddChild and still anchor to screen edges? (Multiple UIRoots?)
« Reply #3 on: January 04, 2014, 12:29:00 PM »
Thanks for the replies.

@ArenMook, that's what I wanted to do originally, but I can't seem to find a method called "SetAnchor" anywhere in NGUI. Searching only showed ResetAnchors() in UIRect.

As you say, my original thought was to do something like...

  1. newDialog.getComponent<UIWidget>().SetAnchor(root.transform);
  2.  
...but that method doesn't exist in UI widget! Where should I be looking?

Thanks again!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.AddChild and still anchor to screen edges? (Multiple UIRoots?)
« Reply #4 on: January 04, 2014, 12:38:52 PM »
You need to update your NGUI. Always update before posting.

http://www.tasharen.com/ngui/docs/class_u_i_rect.html#a9fe98a0b73b4921354b24ac7ba7e0d52