I’m basically trying to make some functionality to emulate the below image. When a player looks at an object (say in this case, a door) a little option bar will pop up in the lower center area of the screen. The bar will have a horizontal width scaled by how many options (icons) the player has.
Layout Groups seem to be a good option, but I’m having a hard time finding something that will anchor the Group/panel as well as the icons inside of it in the center. At least messing around a bit with the Layout Groups sample scene anyways.
What would I go about doing to accomplish this? Also, when I procedurally add the icons through code, would I just instantiate a game object and position it as a child of the “Grid” game object?
I don’t see any tutorials on this yet, but feel free to direct me if I missed one. Cheers
Coming in as a user of DF-GUI for reference, it was very easy to snap panels to horizontal center etc.
I’m currently doing something kind-of similar, though without Layout Groups. There may be nicer ways to do this, but just in case this might help someone, here’s what my code (adapted for this example) looks roughly like:
public Canvas canvas;
public GameObject buttonPrefab;
int btnSize = 120; //fetch or calculate somehow
int numIcons = 3;
float relativeWidthButton = btnSize / Camera.main.pixelWidth;
float relativeHeightButton = btnSize / Camera.main.pixelHeight;
float relativeWidthTotal = (Size * numIcons) / Camera.main.pixelWidth;
float startX = 0.5f - relativeWidthTotal / 2; //First icon's X coordinate (relative)
float yPos = 0.9f;
public void repositionIcons(){
for(int i=0; i<numIcons; i++){ //just position them manually
RectTransform btn = ((GameObject)Object.Instantiate(buttonPrefab)).GetComponent<RectTransform>();
btn.SetParent(canvas.transform, false);
btn.anchorMin = newVector2(startX + i*btnWidth, yPos); //using anchors in the buttons' corners
btn.anchorMax = newVector2(startX + i*btnWidth, yPos - relativeHeightButton);
btn.offsetMin = btn.offsetMax = new Vector2(0, 0); //make sure the button is stretched by the anchors.
btn.GetComponent<Image>().overrideSprite = ...;//set image here
}
}
It would be nice if this could be done more automagically, but I haven’t figured out a better way yet. I’ve tried setting a panel as the buttons’ parents and resizing (and anchoring) that instead, but this led to some weird scaling/ stretching issues that I haven’t bothered to fix yet.
Thanks for the input! I’ll try to replicate something like that into my own system. That was kind of how I imagined I would do it via code, I just wasn’t sure if Unity had an automagic system like you said. Will try to post my results if I get success, and curious to see if others have different methods.
Cheers!
The Layout Group positions the child objects and the Size Fitter adjusts the width and height of your background based child objects.
If you dont add the Layout Element Component to a child object, it will resize to fit.
Awesome! Yeah I used MinSize instead of preferred because preferred was causing some inconsistency issues. – actually scratch that now it seems to be working with preferred… I’m reading up in the docs about the difference
Where did you learn this - have you been using the beta for a while or is it on a video/doc somewhere or just experimenting like crazy?
Looks like this is my issue with Perferred - when I try to set the child object to be 100x100 preferred, and delete all but one of them, it looks like the image doesn’t want to size down quite that small, maybe due to the background sprite?
But MinSize just seems to work to do what I want (though in this case it seems like the term “FixedSize” is more appropriate, and minsize implies that it could get bigger?
You can see here that the background is totally shrunk to fit the button size, and it’s exactly 100x100.
Not sure the logic behind Preferred size and why it’s scaling the button like that, maybe because of the pixel size or aspect ratio of the background image sprite…
When you set PreferredWidth or Height Values, you have to disable Flexible Width or Height
Enable those 2 options also and set their value to 0
Edit: Above the Preview section of your gameobject (“Window” in the last picture, right below the content size fitter) there’s a button.
Change that to Layout Properties to get more information about whats happening
Ah ok thanks that Layout Properties window is helpful!
Have to say though, that current workflow seems like a bug on Unity’s end. In order to disable it, I need to enable the checkbox and set the value to 0? Shouldn’t having it unchecked make it disabled by default?