Author Topic: Set To Current Position not working correctly in latest NGUI  (Read 4653 times)

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Set To Current Position not working correctly in latest NGUI
« on: March 05, 2015, 07:24:01 PM »
This may only be for anchors over 1.0 but for example here are some values I have and what the code below provided as answers:

UIRectEditor
float final = Mathf.Floor(0.5f + Mathf.Lerp(0f, f1 - f0, rel.floatValue) + abs.intValue);

Top anchor:Custom
Relative=1.204429 Absolute = 19

After pressing Set To Current Position
Relative = 1.09379 Absolute = 0;

Height of anchored To object = 177

So you can tell that its exactly backwards, it should have increased to 1.31177


Further notes. Its not just anchors over 1. Normal anchors are also moving when you do set current position. I debugged into this code line above and f0,f1(which I understand to be the sides of who you are anchored too) have values which seem incorrect
Position =190.985,89.81001
Dimensions
284x177
Bottom side should then be 89.81 - (177/2) but instead of coming out with ~1 the bottom is shown as -194
Top side should then be 89.81 + (177/2) but instead of coming out with ~177 the topis shown as 4
« Last Edit: March 05, 2015, 07:38:40 PM by greyhoundgames »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Set To Current Position not working correctly in latest NGUI
« Reply #1 on: March 05, 2015, 10:25:10 PM »
It's a side effect of how Unity's Mathf.Lerp works. It always clamps to 0-1 range, so everything using it is also affected. To fix this, Mathf.Lerp usage should be replaced with a non-clamped lerp function.

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: Set To Current Position not working correctly in latest NGUI
« Reply #2 on: March 06, 2015, 11:01:51 AM »
I don't think this is the problem. I see this also on smaller anchors not > 1 and putting in the unclamped lerp function as such
  1. public static float LerpUnclamped(float from, float to, float value)
  2. {
  3.    return (1.0f - value) * from + value * to;
  4. }
  5.  

and

  1. public void SetVertical (Transform parent, float localPos)
  2. {
  3.   if (rect)
  4.   {
  5.     Vector3[] sides = rect.GetSides(parent);
  6.     float targetPos = LerpUnclamped(sides[3].y, sides[1].y, relative);
  7.     absolute = Mathf.FloorToInt(localPos - targetPos + 0.5f);
  8.   }
  9.   else
  10.   {
  11.     Vector3 targetPos = target.position;
  12.     if (parent != null) targetPos = parent.InverseTransformPoint(targetPos);
  13.     absolute = Mathf.FloorToInt(localPos - targetPos.y + 0.5f);
  14.   }
  15. }
  16.  

Did not solve the problem

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Set To Current Position not working correctly in latest NGUI
« Reply #3 on: March 10, 2015, 07:43:11 PM »
Now that I'm back from PAX I was able to give it a shot.

1. New scene.
2. ALT+SHIFT+S, moved it to top left corner.
3. Advanced anchoring, went through each anchor and used "Set to current position".
4. All anchor points update automatically. Even worked fine when it's outside 0 to 1 range.

What are you doing differently on your end?