Skip to content

Instantly share code, notes, and snippets.

@Amitkapadi
Created January 31, 2023 07:04
Show Gist options
  • Save Amitkapadi/42606483205f1492ccd7211e08faeac3 to your computer and use it in GitHub Desktop.
Save Amitkapadi/42606483205f1492ccd7211e08faeac3 to your computer and use it in GitHub Desktop.
LayoutUtility for Unity World Space.
namespace MyUtility
{
public static class LayoutUtility
{
public static float[] GetXPosLayout(int totalObj, float maxWidth, float objXSize, float space, float xPedding)
{
float maxSize = maxWidth - xPedding - xPedding; //Left and Right
float occupiedArea = (totalObj * objXSize);
float halfObjSize = objXSize * 0.5f;
if (totalObj >= 2)
occupiedArea += (totalObj - 1) * space;
float difference = (maxSize - occupiedArea) * 0.5f;
float[] xPos = new float[totalObj];
float starting_x_value = maxWidth * -0.5f;// localSpace
starting_x_value += xPedding;
starting_x_value += difference;
starting_x_value += halfObjSize;
for (int i = 0; i < totalObj; i++)
{
xPos[i] = starting_x_value;
starting_x_value += (objXSize + space);
}
return xPos;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment