Author Topic: Anchor sprite to screen corner at runtime?  (Read 3888 times)

delzhand

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 6
    • View Profile
Anchor sprite to screen corner at runtime?
« on: February 08, 2014, 05:50:41 PM »
I'm having trouble figuring out what the best practice is for this case.  I came up with this based on another forum post:

  1.      
  2.       UIRect uiRect = sprite.GetComponent<UIRect>();
  3.  
  4.       uiRect.topAnchor.target = parent;
  5.       uiRect.bottomAnchor.target = parent;
  6.       uiRect.rightAnchor.target = parent;
  7.       uiRect.leftAnchor.target = parent;
  8.  
  9.       uiRect.leftAnchor.absolute = 10;
  10.       uiRect.rightAnchor.absolute = 210;
  11.       uiRect.topAnchor.absolute = -10;
  12.       uiRect.bottomAnchor.absolute = 35;
  13.  

But that anchors left to left side, right to right side, etc.  My I want my sprite to be 200 wide, starting 10 from the left edge.  This is easy as pie in the UI - I just set the sprite anchor's "left" to "target left" with a value of 10, and "right" to "target left" with a value of 210.

I thought maybe SetToNearest was a possible option, but the description and variable names make that function completely inscrutable to me.  I'm sure things will be easier once I can figure out the abstraction from the data storage to the ui, but as it stands I have no clue what variables the Anchor panel represents.

delzhand

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 6
    • View Profile
Re: Anchor sprite to screen corner at runtime?
« Reply #1 on: February 08, 2014, 07:12:36 PM »
And as a followup question, how come the top image here is left 0 top 0, and not the second one?  Where on earth is that 0 pixels from the left or top of the containing panel?


delzhand

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 6
    • View Profile
Re: Anchor sprite to screen corner at runtime?
« Reply #2 on: February 09, 2014, 10:42:03 AM »
Okay, I think maybe I've got it figured out - someone can correct me if I'm wrong.

The anchor panel of the UI is an abstraction and only makes sense in the context of the ui.  If you want to set the position of something to the corner of the screen, you can use Screen.width or whatever, because you're not going to be using the UI to change things in the actual game.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Anchor sprite to screen corner at runtime?
« Reply #3 on: February 09, 2014, 04:43:13 PM »
Quote
But that anchors left to left side, right to right side, etc.  My I want my sprite to be 200 wide, starting 10 from the left edge.  This is easy as pie in the UI - I just set the sprite anchor's "left" to "target left" with a value of 10, and "right" to "target left" with a value of 210.
If you look at UIRect, it has 3 SetAnchor functions, one of which is:
  1.         public void SetAnchor (GameObject go, int left, int bottom, int right, int top)
  2.         {
  3.                 Transform t = (go != null) ? go.transform : null;
  4.  
  5.                 leftAnchor.target = t;
  6.                 rightAnchor.target = t;
  7.                 topAnchor.target = t;
  8.                 bottomAnchor.target = t;
  9.                
  10.                 leftAnchor.relative = 0f;
  11.                 rightAnchor.relative = 1f;
  12.                 bottomAnchor.relative = 0f;
  13.                 topAnchor.relative = 1f;
  14.  
  15.                 leftAnchor.absolute = left;
  16.                 rightAnchor.absolute = right;
  17.                 bottomAnchor.absolute = bottom;
  18.                 topAnchor.absolute = top;
  19.  
  20.                 ResetAnchors();
  21.                 UpdateAnchors();
  22.         }
In your case the code would be:
  1.                 leftAnchor.target = t;
  2.                 rightAnchor.target = t;
  3.                 topAnchor.target = t;
  4.                 bottomAnchor.target = t;
  5.                
  6.                 leftAnchor.relative = 0f;
  7.                 rightAnchor.relative = 0f; // <-- left
  8.                 bottomAnchor.relative = 1f; // <-- bottom set to 100% of the target's height (= top)
  9.                 topAnchor.relative = 1f;
  10.  
  11.                 leftAnchor.absolute = 10;
  12.                 rightAnchor.absolute = 210;
  13.                 bottomAnchor.absolute = -50; // we subtract 50 from 100% of the target's height
  14.                 topAnchor.absolute = 0;
  15.  
  16.                 ResetAnchors();
  17.                 UpdateAnchors();
You can also shorten it like so:
  1. widget.leftAnchor.Set(0f, 10f);
  2. widget.rightAnchor.Set(0f, 210f);
  3. widget.topAnchor.Set(1f, 0f);
  4. widget.bottomAnchor.Set(1f, -50f);
  5. widget.ResetAnchors();
  6. widget.UpdateAnchors();