示例#1
0
    private void CalculateView()
    {
        Camera          camera          = FindObjectOfType <Camera>();
        CameraControler cameraControler = camera.GetComponent <CameraControler>();
        float           cameraSize      = cameraControler.GetSize();

        cameraSize = Mathf.Ceil(cameraSize);

        viewWidth  = (int)cameraSize;
        viewHeight = (int)cameraSize * 2;

        int fullViewWidth  = viewWidth + 2;
        int fullViewHeight = viewHeight + 2;

        int numberOfViewTiles = fullViewWidth * fullViewHeight;

        viewTiles = new GameObject[numberOfViewTiles];

        float startX = camera.transform.position.x - (cameraSize / 2 + 1);
        float startY = camera.transform.position.y - (cameraSize + 1);

        // Limit view tiles to map limits
        startX = Mathf.Clamp(startX, 0, mapWidth - cameraControler.GetSize());
        startY = Mathf.Clamp(startY, 0, mapHeight - cameraControler.GetSize());

        startX = Mathf.Floor(startX);
        startY = Mathf.Floor(startY);

        float unitSize = 1.00f;

        // wi - width index
        // hi - height index
        for (int wi = 0; wi < fullViewWidth; wi++)
        {
            for (int hi = 0; hi < fullViewHeight; hi++)
            {
                Vector2 newPosition = new Vector2(startX + wi * unitSize, startY + hi * unitSize);

                GameObject newViewTile = Instantiate(viewTilePrefab, newPosition, Quaternion.identity) as GameObject;
                newViewTile.transform.parent = this.transform;
                newViewTile.name             = "viewTile [" + wi + "][" + hi + "]";

                ViewTile vt = newViewTile.GetComponent <ViewTile>();
                vt.x = (int)startX + wi;
                vt.y = (int)startY + hi;

                viewTiles[wi * fullViewHeight + hi] = newViewTile;
            }
        }
    }