Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unity UI How do I change a UI element's Y position in code?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by AcademyOfFetishes, Apr 22, 2019.

  1. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    212
    if someone can help me with this, I feel like I'll understand the UI system a lot better.

    I've got a scene like this:


    I want to move that dialog to the top

    in play mode, I discovered that that's a Y pos of 840:



    but this code moves it way above where it should be:

    Code (CSharp):
    1.     public void MoveSayDialogToTop()
    2.     {
    3.         var sayDialog = GameObject.Find("SayDialog").transform.Find("Panel").GetComponent<RectTransform>();
    4.         var pos = sayDialog.localPosition;
    5.         sayDialog.localPosition = new Vector3(pos.x, 840, pos.z);
    6.     }
    When this is run, it looks like this in the scene:



    It even has a different Y from what I'm setting, it has a 1440

    What am I doing wrong?
     
  2. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,761
    Change localPosition too anchorPosition.
     
  3. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    212
  4. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    782
    localPosition is where the pivot of the element is in relation to the parents pivot.

    anchorPosition is where the pivot of your element is in relation to your elements anchor.


    Those can result in totally different things because every element (yours, and the parent) can have totally arbitrary values for pivot and anchor.
    Your anchor can even be set to a "scaling anchor" so to say (which is when min and max are not equal, and your anchor describes a range instead of a point).

    Take a look:

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

    The second sentence is what you want to know:
    In the end it totally depends on what you want to achieve.
    Sometimes you want to deal with pivot, other times you want a delta to the anchor,
    and yet other times you must use a non-point-anchor...
     
    AcademyOfFetishes likes this.
  5. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    212
    I'm sure this is in your answer and I can't understand it: What does this have to do with positioning? I can take a Panel's pivot and drag it around in the editor and the position doesn't change at all. Why does changing anchorPosition change the position?
     
  6. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    782
    The editor works in a completely different way to make it "easier".
    When you are in the editor, and drag the pivot point around with your mouse, the unity editor basically counter-calculates how the "anchorPosition" would have to change so the rectangle visually doesn't move. (same for anchor values etc).

    That's why there are the two buttons (raw mode, and blueprint mode).

    Press raw mode to get no helper calculations or anything:

     
    AcademyOfFetishes likes this.
  7. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    212
    Thank you! That makes everything so much more clear! :)