Author Topic: Widget.worldcorners not updating same frame  (Read 12960 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Widget.worldcorners not updating same frame
« on: September 07, 2016, 06:00:15 AM »
I'm drawing around a widget's borders after changing its anchor. If I delay the drawing 1 frame after updating the anchor, it works, but if I draw around the anchors n the same frame, it draws around the wrong position.

  1.                 TargetThing.SetAnchor (tuteTargets[maskCount].gameObject, -18, -18, 18, 18);
  2.                 TargetThing.ResetAndUpdateAnchors ();
  3.                
  4.                 maskTopLeft = (Vector2) UICamera.mainCamera.WorldToScreenPoint (TargetThing.worldCorners[1]);
  5.                 maskBottomRight = (Vector2) UICamera.mainCamera.WorldToScreenPoint (TargetThing.worldCorners[3]);
  6.  
How do I force the widget to immediately update its worldCorners to the new anchor positions?

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: Widget.worldcorners not updating same frame
« Reply #1 on: September 07, 2016, 03:33:25 PM »
SetAnchor already calls both Reset and Update.

What type of object is "TargetThing"? worldCorners has a different implementation for different UI components.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Widget.worldcorners not updating same frame
« Reply #2 on: September 08, 2016, 03:17:14 AM »
TargetThing is an NGUI widget that just captures input (widget and box collider, nothing else). I draw around the TargetThing position into  a mask texture during tutorial events. The mask texture shows the user what to press (blacking out everything else) and the TargetThing widget captures the press and progresses the tutorial.

As I say, it works so long as I defer drawing one frame. Maybe all the final NGUI positioning happens in LateUpdate and you can't reposition immediately this way?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Widget.worldcorners not updating same frame
« Reply #3 on: September 09, 2016, 02:47:15 PM »
Have you examined the EnvelopContent script? If you're just trying to draw around a widget, that script can resize any sprite to do just that.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Widget.worldcorners not updating same frame
« Reply #4 on: September 10, 2016, 08:04:24 AM »
I'm drawing into a texture to create a mask, not placing a widget. I can work around by offsetting the drawing by one frame. But is what I ask possible or are all widget bounds updated at the end of the frame?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Widget.worldcorners not updating same frame
« Reply #5 on: September 13, 2016, 10:26:00 AM »
SetAnchor should be immediate. Going through the order of calls, calling SetAnchor calls UpdateAnchors, which in turn calls OnAnchor, which is what calculates the widget's position, width and height.

UIWidget.worldCorners then uses those values, adjusting them by the widget's transform.

The only issue I see with your call is you're passing -18, -18, 18, 18 -- which I'm guessing are supposed to be absolute values... yet the function expects relative values instead (ie -1 to 1 range!)

I just tried this simple test:

1. New scene, ALT+SHIFT+S to make a sprite.
2. Game Object -> 3D Object -> Cube.
3. Attached this script to the cube and set the sprite reference on it:
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class Test2 : MonoBehaviour
  5. {
  6.         public UIWidget widget;
  7.  
  8.         void Update ()
  9.         {
  10.                 if (Input.GetKeyDown(KeyCode.S))
  11.                 {
  12.                         widget.SetAnchor(gameObject, 0f, -18, 0f, -18, 0f, 18, 0f, 18);
  13.  
  14.                         var corners = widget.worldCorners;
  15.                         var cam = UICamera.FindCameraForLayer(widget.gameObject.layer);
  16.                         var v0 = cam.cachedCamera.WorldToScreenPoint(corners[0]);
  17.                         var v1 = cam.cachedCamera.WorldToScreenPoint(corners[2]);
  18.                         Debug.Log(v0 + " " + v1);
  19.                 }
  20.         }
  21. }
  22.  
4. Play, hit "S", see values show up correctly immediately, and the sprite gets positioned right in the middle of the cube.