0
\$\begingroup\$

I'm trying to place one in our field right below another, all ui belonging to an overlay canvas. I tried doing a few different ways subtracting height from different position types including rect types but the result of position I get seems to be inconsistent and a bit off. Thanks in advance.

enter image description here enter image description hereenter image description here enter image description here

enter image description here

enter image description here Note the anchor pivot point for my body fields on Y is 1, because I need it that way for the field to expand downwards as the field text grows in length. enter image description here

So as you can see as the lines get longer and the height increases, the placement of the empty body fields is no longer accurate and gets thrown off. What's going on?

enter image description here

So here's the code I'm running to create my new Input fields, the current one turns white and "cements" itself as a new one empty non white one gets instantiated right underneath it, when you double click anywhere on the screen. Hence the times click on the code. Anyways the commented out code are all the different ways I've tried doing this but with no luck. The moment by input field height starts changing when there is more than one line, the new instantiated field doesn't get place properly, right underneath it like it does with just one line as you'll see in the images, below the code.

  public void CementSegment_And_AddNewEmpty()
{
    TimesClicked++;
    StartTimesClickedTimer = true;


    if (TimesClicked >= 2)
    {
        //Turns current segment to white (cements it)

        NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<Image>().color = new Vector4(1, 1, 1, 1);

        //Adds new unsegmented/non white body to bottom of the (newly cemented white segment from code line above)

        //float height = NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<RectTransform>().rect.height;
        float height = NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<RectTransform>().sizeDelta.y;


        //Vector3 pos = NoteBodyObjects[NoteBodyObjects.Count - 1].transform.position;
        //Vector3 pos = NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<RectTransform>().anchoredPosition;
        //Vector3 pos = NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<RectTransform>().localPosition;
        Vector3 pos = NoteBodyObjects[NoteBodyObjects.Count - 1].GetComponent<RectTransform>().position;

        Vector3 BodyFieldLocation = pos - new Vector3(0, (height * 0.5f), 0);

        GameObject NewBodyField = Instantiate(DefaultBodyField, ContentParent);

        //NewBodyField.transform.position = BodyFieldLocation;
        //NewBodyField.GetComponent<RectTransform>().anchoredPosition = BodyFieldLocation;
        //NewBodyField.GetComponent<RectTransform>().localPosition = BodyFieldLocation;
        NewBodyField.GetComponent<RectTransform>().position = BodyFieldLocation;
        

        NoteBodyObjects.Add(NewBodyField);
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ This can be affected by how you've configured the RectTransform..Can you show us a screenshot of the RectTransform inspector for the parent element, the child element you want to place, and the already placed child element you want to find the bottom of? If you have any layout components in use, you should tell us about those too. \$\endgroup\$
    – DMGregory
    Oct 9 at 11:52
  • \$\begingroup\$ I've looked at your screenshots and code but I'm having a hard time understanding what exactly you are trying to do here. Help me understand the use case. What is a "NoteBodyObject" is this context? Also , you have a ContentSizeFitter to the added elements themselves -- why? \$\endgroup\$ Oct 9 at 19:27
  • \$\begingroup\$ thanks for your time anyways guys, i posted the answer \$\endgroup\$ Oct 9 at 19:56
0
\$\begingroup\$

I figured out what it was. I'm an idiot. I happened to scale my prefab input field before making it a prefab, using the transform scale tool instead of using the rect tool like I suppose you're suppose to do. Meaning the inconsistencies were due to scale values that went unaccounted for from the transform scaling. I don't deal too much with UI but lesson learned. So if you want accurate manipulation of rect positions, height etc, please make sure you do all your UI size position changes using the rect transform in the inspector or the appropriate rect tools. Hope this helps someone!

\$\endgroup\$
1
  • \$\begingroup\$ Ah yes -- that is a gotcha. I always reset a UI prefab's localScale after instantiating and parenting it to a UI element as the UI prefab scale can get whack without context. \$\endgroup\$ Oct 9 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.