Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scrolling UI Dynamic Menu

Discussion in 'UGUI & TextMesh Pro' started by shraa1, Jun 16, 2016.

  1. shraa1

    shraa1

    Joined:
    Sep 2, 2013
    Posts:
    6
    Hey guys, I am trying to create a scrolling menu, which will have buttons added to it dynamically. I don't really want to hardcode anything, but I have ended up hard coding too many values. And on top of that, I still can't get to work it the way I want it to.

    I am attaching a unitypackage file, just 1 scene and 1 script and 2 image files-

    What problems I am facing-
    1. I am trying to have a horizontal scrolling menu, which will have either a small or a tall button. The button is using a "preserve aspect" image, so the button even outside the image (since the button is big, and the image is only covering a small portion of the button) is getting clicked.
    2. Too many hardcoded values, especially the -39, 130, -6 in (gridSize - 6), etc.
    3. Not able to get an even gap between the buttons.

    Some of the methods I have used may be unorthodox, but they were the only way I could get this far. If there is a better way to do any of this, it will be much appreciated.
     

    Attached Files:

  2. shraa1

    shraa1

    Joined:
    Sep 2, 2013
    Posts:
    6
    Oh, I just remembered. Also the text should be placed in the center, below the image. Setting Center Align moves the text to the right side if the gap is larger
     
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    You can build UI elements programmatically, just takes a little care and finesse (and a whole lot of luck)

    You can look at the editor script in the UI Extensions project (link in sig) for dynamically creating controls to see how things are laid out. And you can also take a look at some of the scroll snap controls for how they build things.

    Hope that helps.
     
  4. shraa1

    shraa1

    Joined:
    Sep 2, 2013
    Posts:
    6
    Thanks for the reply, I'll take a look. :cool:
     
  5. zge

    zge

    Joined:
    Sep 18, 2014
    Posts:
    35
    Also look at Horizontal Layout Group and Layout Element components which work together to give your buttons the right sizes and spacing
     
  6. shraa1

    shraa1

    Joined:
    Sep 2, 2013
    Posts:
    6
    Thanks for the reply. I had already looked into it. Horizontal layout group stretched or squished the image too, and there was still blank space left which would accept the clicks. I am trying something else though, and it might work. I'll post an update if that works :) :)