Search Unity

Need help with UI scaling.

Discussion in 'UGUI & TextMesh Pro' started by Jejkobb, Sep 14, 2017.

  1. Jejkobb

    Jejkobb

    Joined:
    Aug 10, 2017
    Posts:
    19
    I'm having a bit of trouble with the UI system. Never quite understood it. Can someone explain how I stop this from happening:

    This is how it looks in the editor and it's also how I want it to look:



    Now this is what it looks like in full screen 1920x1080:



    This is what the button object looks like in the editor (while the game is not running):



    This is the code for positioning the buttons:

    Code (CSharp):
    1. void createButton(string id, string t, int i)
    2.     {
    3.         // Create a Button object
    4.         if (buttonGameObject != null)
    5.         {
    6.             scrollPanel.GetComponent<Image>().enabled = true;
    7.  
    8.             scrollObject.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollObject.GetComponent<RectTransform>().rect.width, d.GetNode(DialogueID.ToString()).options.Length * 85);
    9.             scrollPanel.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0, 1);
    10.  
    11.             GameObject b = Instantiate(buttonGameObject, new Vector3(10, 160 - (60 * buttonID)), Quaternion.identity);
    12.             b.transform.SetParent(scrollObject.GetComponent<RectTransform>().transform);
    13.             b.GetComponentInChildren<Text>().text = "" + t;
    14.             UnityEngine.Events.UnityAction action1 = () => { this.onButtonClicked(id, i); };
    15.             b.GetComponent<Button>().onClick.AddListener(action1);
    16.  
    17.             buttonID++;
    18.         }
    19.     }
    How do I get it to look the same no matter the resolution?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I believe this option will work for you, unless the window can be resized to anything.

    I tried to mimic your setup.
    I created a panel which was setup to be on bottom and stretch width-wise.
    then added 3 buttons
    added vertical layout group to the panel (all 4 options selected).

    Does that help ?
     
  3. Jejkobb

    Jejkobb

    Joined:
    Aug 10, 2017
    Posts:
    19
    I did it by adding all the possible buttons ready onto the scene. Then adding them to an array. And just making them visible when they're used and it workes great. But thanks for the help anyway.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got it working..:)