Author Topic: instantiated prefabs with UIButtonOffset freak out when under a UIGrid  (Read 4517 times)

theprojectabot

  • Guest
I instantiate a prefab that has a position of 0,0,0 I then parent that prefab to a container prefab.  This container prefab has a UIGrid attached to it.  At the end of instantiating all my prefabs I: uiGrid.repositionNow = true;

This works fine unitl I hover over a button.  It then send the prefab child off into a totally random position.  I found by turning off the UIButtonOffset script I no longer had these issues under UIGrid.

Steps:
Instantiate UIButtonprefab that has UIButtonOffset attached. 
Child it to a UIGrid Gameobject
call repositionNow
hover mouse over button and watch it disapear.

theprojectabot

  • Guest
perhaps I am exposing a problem in my own UI setup....
If I call ResetPosition on my draggable panel it gets set to:  x= -1084590, instead of 0. I wonder where this random number is coming from that the draggable panel thinks is its top right...  all parents are 0,0,0 in for their positions except for the UI root which I set to -1426 on x so that it wont be in my 3d camera.

Any ideas folks?

btw that random number was the same x I got when I had button offset on..
Also I am finding that unless I set the localScale of my buttons in code right after instantiation they come out as 735,735,735 scale instead of 1,1,1.

Ideas?

theprojectabot

  • Guest
Ok after looking into this I think I see where there are weird values being populated... but I dont know why they are there:

in SetDragAmount I see that left and right = 1096121  these numbers are calculated by the b.min + hx  which comes from the clipRange of the mPanel.

This all comes from a calcualted Bounds  from the mBounds = NGUIMath.CalculateRelativeWidgetBounds(mTrans, mTrans); on Line 145 in UIDraggablePanel...
Is this because I have large wigets pixel dimensions of x wide:1154 and 10 of them? plus grid spacing? 

yeah it just seems my bounds are way way off.  And I dont know why...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Use NGUITools.AddChild to instantiate objects.

theprojectabot

  • Guest
I tried that route and I still have the same problem.

bjennings76

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: instantiated prefabs with UIButtonOffset freak out when under a UIGrid
« Reply #5 on: September 22, 2013, 06:59:37 PM »
I've had this same problem when removing buttons from a UIGrid and then repositioning the remaining buttons.

Each button will slide back to its original position on mouse over, leaving a hole in the list and acting just plain weird. This is because UIButtonOffset's 'mPos' variable used to tween doesn't get reset when the UIGrid repositions it's children. That position only gets set once at in UIButtonOffset's Start() function.

I see two solutions to the problem:
  • The saved position is local, so you could nest the button into an empty game object that is in turn inside the UIGrid. The grid will reposition that game object pulling the button along with it which will never be the wiser and it's saved position is still valid.

  • You could modify NGUI directly to expose a 'ResetStartPosition()' function in UIButtonOffset that UIGrid would use during it's 'Reposition()' function when a transform it repositions has that component.

For me, I didn't need the UIButtonOffset effect, so I've just removed the UIButtonOffset component and my problem is solved.