Search Unity

Moving a UI button across a canvas

Discussion in 'Getting Started' started by Deleted User, Dec 28, 2016.

  1. Deleted User

    Deleted User

    Guest

    (writes like 250-300 words being silly, realizes what question to ask, deletes them)

    What I need to do is move a button's position to the position of another button's anchor, so one button can do a cute slide to the position of a previously present one when it's pushed; while accommodating for any hypothetical windowed screen dimensions.

    https://docs.unity3d.com/ScriptReference/RectTransform.html

    Is there a variable I'm missing in here? Uh, the moving stuff around I can handle I just need to find the right data to act on. If I can't figure it out I guess I just sprinkle some empties in there and use their world coordinates.

    I figure I'll move it there, then set its anchor and position variables in 1 frame. So you can bork it up only for a moment if you're shagging the window while it's moving and then it's good (which is an acceptable fringe case). Moving it to a position that doesn't adjust as the anchor is designed to allow isn't an acceptable fringe case because the whole point of the anchors is adjustable window size.

    (oops wrote like 150 again)
     
  2. Deleted User

    Deleted User

    Guest

    face -> palm

    1.) Move the anchor instantly to the desired value

    2.) lerp the now non-0 local position of the thingy to 0

    3.) profit????

    Edit:

    I'm... I'm batman.

    Code (CSharp):
    1.        
    2.  
    3. RectTransform rt = selectedSlot.newGameField.GetComponent<RectTransform>();
    4. Vector2 newAnchor = new Vector2(0.5f, 0.8f);
    5. rt.anchorMin = newAnchor;
    6. rt.anchorMax = newAnchor;
    7.  
    8.  
    Stop programming when you hit Batman stage, kids. Never go full Marvel.
     
    Last edited by a moderator: Dec 28, 2016
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I tend to just use Transform.position. Unless you need to mess with the anchors at runtime, you don't actually need to touch RectTransform.

    Transform.position works mostly as you'd expect it too.