Вопрос

I am trying to save block positions in any kind of collection. For example, if I have a grid of 16*16 blocks, I need to save 256 block positions. I could just use an array or a list right? No problem here.

But what when I have a grid of grids, like a 10x10 grid of grids. So suddenly I need to save 10x10x256 blocks. I currently save the blocks in every grid, so every grid keeps track of it's own blocks. Also no problem here (yet).

The problem actually is that I need to iterate over all those grids to check if a grid has an adjunctive grid. This results in enormous looping times.

I was wondering if there is some kind of way to reduce this, or do this better.
I was thinking of using the position of these blocks as the index in the collection, but for example using a multidimensional array gives a lot out of bounds errors, so somehow I can't use that.

Это было полезно?

Решение

If I understand correctly you may need this I stored each GameObject in Array of Gameobject YOu may not get an Exception here

    GameObject[,] ArrayOfGameObjects = new GameObject[10, 10];       
    public void spawnGrid ()
                {
                    for (int y = 0; y < 10; y++) {
                        for (int x = 0; x <10; x++) {

                        ArrayOfGameObjects[x,y] = Instantiate (YourPrefabGameObject, new Vector3 (x, y, 0),Quaternion.identity)as GameObject;   

                        }
                    }
                }

    public CheckAdjOf (GameObject gameObj)
            {

                if (gameObj == null)
                {
                     //Do your stuff if its null
                     //for example break, return 
                    }

                int x = (int)gameObj.transform.position.x;
                int y = (int)gameObj.transform.position.y;

                GameObject Left,Right,Up,Down;      

                if (x < mArray.GetUpperBound (0))       
                Right = ArrayOfGameObjects [x + 1, y] == 0);

                if (x > 0)  
                Left = ArrayOfGameObjects [x - 1, y] == 0);


                if (y < mArray.GetUpperBound (0))
                Up = ArrayOfGameObjects [x, y + 1] == 0);

                if (y > 0)
                Down = ArrayOfGameObjects [x, y - 1] == 0);

                // you can also find diagonal GameObjects 
                // do your stuff after you getting all adjunctive grid/block 

            }

Другие советы

I am assuming that you want to check if part of a grid is within another? If not, disregard ;)

If so, you could optimize things by creating bounds for each of the grids. Each grid could have a collider and you could perform checks for the individual blocks only when a collision happens between two grids.

This should eliminate unnecessary loop through distant grids. You should be able to grow the bounds of a grid by using Bounds.Encapsulate when you add a new block.

hth.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top