Space buttons evenly

I want my automatically created buttons to be spaced evenly across the game space.

So now the part of the tutorial that I kinda ignored is going to be helpful (the RogueLike Board Manager).  Hmmm, actually, that’s not that helpful, because I’m not creating a grid first – I just have a single-color background right now.  To the google!

This one’s cool because it spaces things evenly in a circle.  I want it in a grid (so maybe I have to act like I have a grid anyhow … hmmm.).

This person has the same problem as me, but no one chimed in.

This thread talks about making things along a path.

Looks like there’s a $20 solution in the asset store.   Unclear if it would interact well with my instantiating. Okay so it looks like I do need to pretend I have a grid.

  1. First question – can the script figure out how big the gamespace is?

Ooh – this looks useful.  Oh gosh.  GUI coordinates start at (0,0) in the upper left (go to (Screen.width, Screen.height) in the lower right), vs. screen coordinates start at (0,0) in the lower left.

So I’m using GUI coordinates.  I want to start at (0,0), go all the way across to (0, Screenheight), etc.  Maybe I use the BoardManager from RogueLike, but just replace the coordinates.

I was lazy and am trying to just do it myself.

Vector3 buttonPosition1 = new Vector3((float)Screen.width/2f, (float)Screen.height/2f, 10f);

This is NOT centered along any axis.

For when I want to know about more specific things: http://forum.unity3d.com/threads/get-screen-dimensions.110777/

Huh.  When I put it at (0,0,0), it’s dead center.  This doesn’t make much sense.  Apparently this is what I should’ve expected?  From this guy’s pictures …  http://answers.unity3d.com/questions/994599/gameobject-with-000-position-not-being-in-the-cent.html

Maybe this has something to do with how my camera is viewing things.  When I change it to world space, it can’t see any buttons.   Or maybe something to do with being childed to a canvas?  Or maybe it’s working exactly how it should.

This is top left:  Vector3(400,250,0)

Ah HA!  I needed to tell it what space I was defining vectors in.  Turns out “Camera.main.ScreenToWorldPoint” was actually kinda important.  Now the below is showing dead center. 

Vector3 buttonPosition1 = Camera.main.ScreenToWorldPoint(new Vector3((float)Screen.width/2f, (float)Screen.height/2f, 10f)); //(float)Screen.height

But … the /2f on both of them doesn’t matter.  If I take it out, it’s still dead center.  If I change it to /4f, it’s still dead center.  I need to quarter my x axis, so …

This must have something to do with World Space vs other types of space?  Or orthographic vs. perspective? (My camera’s currently an orthographic one.)

This does the same thing :

ScreenToViewportPoint

This is for calculating camera bounds.  What I did below still puts it dead center.

        float horzExtent = Camera.main.orthographicSize * Screen.width / Screen.height;
        Debug.Log(horzExtent);
        float vertExtent = Camera.main.orthographicSize * Screen.height / Screen.width;
        Vector3 buttonPosition1 = (new Vector3(horzExtent, vertExtent, 0f));

Sigh.

And more.   This uses a linecast.

#todo – add this to my thing about preventing player from leaving bounds

 

This works to get the lower left and I have no idea why.

Vector3 buttonPosition1 = new Vector3(((float)Screen.width / 2f)*-1.5f, ((float)Screen.height / 2f)*-1.5f, 10f);

This works to get top right and I have no idea why.

Vector3 buttonPosition2 = new Vector3(((float)Screen.width / 2f)*1.5f, ((float)Screen.height / 2f)*1.5f, 10f);

Also it looks like I don’t need those pesky (float)s in there.

OHHH this makes more sense maybe?

Vector3 buttonPosition1 = new Vector3((Screen.width/4f)*-4f, (Screen.height/4f)*4f, 10f); // top left

This divides the width by 4, then goes to the 4th position to the left.

If I give it a little buffer on each side, it fits better:

        float bufferX = Screen.width  150f;
        float bufferY = Screen.height  50f;

Not sure how to automate that so I don’t have to do it by eye, but that’ll be a later #todo.

YESSSS: this one does a cute little stacking thing.  Not at all what I wanted, but it’s getting closer!

    void CreateButtons()
    {
        GameObject canvas = GameObject.Find(Canvas);

        // pull in info 
        TextAsset csvFile = (TextAsset)Resources.Load(PuzzleBotGoogle, typeof(TextAsset)); //PuzzleBotData2.txt
        string[] levels = csvFile.text.Split(\n); // #todo why does \r not work? 

        float bufferX = Screen.width  150f;
        float bufferY = Screen.height  50f;
        float baseX = bufferX / 4f; 
        float baseY = bufferY / 4f; 

        float xMult = 4; 
        float yMult = 4; 
       
        int buttonCounter = 0;
        foreach (string level in levels)
        {
            Vector3 buttonPosition = new Vector3(baseX * xMult, baseY * yMult, 10f) ;
            GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
            button.transform.SetParent(canvas.transform, false) ;    
            buttonCounter += 1;
            xMult += 1;
            yMult += 1;
        } 

    }

Ah.  That’s because it’s just counting up for both xMult & yMult at the same time.  I need it to go through all of xMult first, then to iterate on yMult.

Now I need to account for the fact that buttons are overlapping.  Add in how wide buttons are. My buttons are 160 wide, so this fixed that (#todo – would be nice for script to know how wide button is without me checking):

        foreach (string level in levels)
        {
            Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f) ;
            GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
            button.transform.SetParent(canvas.transform, false) ;    
            buttonCounter += 1;
            Debug.Log(xMult);
            xMult += 1;
            xSpace += 160; 

        } 

Made yBuffer a little bigger so you can still see the score in the top right.

Now to iterate across yMult … This does *not* work.  It just marches on across the x axis forever.

        foreach (string level in levels)
        {
            Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
            GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
            button.transform.SetParent(canvas.transform, false);    
            buttonCounter += 1;
            Debug.Log(xMult);
            xMult += 1;
            xSpace += 160; 
            if (xMult == 4)
            {
                yMult -= 1;
                xMult = 4;
            }

        } 

Moving the if loop up above everything else also does not work.

 

        foreach (string level in levels)
        {
            if (xMult == 4)
            {
                yMult -= 1;
                xMult = 4;
            }

            Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
            GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
            button.transform.SetParent(canvas.transform, false);    
            buttonCounter += 1;
            Debug.Log(xMult);
            xMult += 1;
            xSpace += 160; 

        }


    }

 

Not even an if else loop works.

 

        foreach (string level in levels)
        {
            if (xMult == 4)
            {
                yMult -= 1;
                xMult = 4;

                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                Debug.Log(xMult);
                xMult += 1;
                xSpace += 160; 
            }
            else
            {
                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                Debug.Log(xMult);
                xMult += 1;
                xSpace += 160; 
            }

        } 

 

Becaaaaause it turns out xMult is only going up till 0.  Also I need to reset xSpace to 0.  Now it is beautiful and perfect.

grid!

        foreach (string level in levels)
        {
            if (xMult == 0)
            {
                yMult = yMult1;
                xMult = 4;
                xSpace = 0; 

                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                xSpace += 160; 
            }
            else
            {
                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonTemplate, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
//                Debug.Log(xMult);
                xMult += 1;
                xSpace += 160; 
            }

        } 

Leave a Reply

Your email address will not be published. Required fields are marked *