void Start() { // first of all, lets find the size of the background image // we are scrolling SpriteRenderer background = GetComponent <SpriteRenderer> (); float bgx = background.bounds.size.x; float bgy = background.bounds.size.y; // and the size of the camera box float camerax = cam.orthographicSize * Screen.width / Screen.height * 2; float cameray = cam.orthographicSize * 2; // and the size of the level we are locking our scrolling to float levelx = level.GetMapWidthInPixelsScaled(); float levely = level.GetMapHeightInPixelsScaled(); // so the levels have their origin at the top left corner, // we need the center for our calculations Vector2 corner = level.transform.position; center = new Vector2(corner.x + levelx / 2, corner.y - levely / 2); // finally, what's the scaling factor between camera size // and background image size? we need this to calculate where // we should place the background image based on the camera's // position in the main level scalex = (float)((levelx - bgx) / (levelx - camerax)); scaley = (float)((levely - bgy) / (levely - cameray)); }
private void FixedUpdate() { if (front.transform.position.x + front.GetMapWidthInPixelsScaled() <= mainCamera.transform.position.x - CameraHalfWidth) { Destroy(front.gameObject); front = back; back = LoadNextMapPart(front.transform.position.x + front.GetMapWidthInPixelsScaled()); } }
private void Start() { GameObject mainCameraGO = GameObject.FindGameObjectWithTag("MainCamera"); Assert.IsTrue(mainCameraGO != null, "Could not find the main camera."); mainCamera = mainCameraGO.GetComponent <Camera>(); Assert.IsTrue(mainCamera != null, "Main camera does not have a Camera component."); front = LoadNextMapPart(-CameraHalfWidth); back = LoadNextMapPart(front.transform.position.x + front.GetMapWidthInPixelsScaled()); }
private void mergeMap() { if (lastRow == this.row) { return; } if (root != null) { GameObject.Destroy(root); } root = new GameObject("root"); root.transform.localScale = Vector3.one; root.transform.position = Vector3.zero; int mapCount = 10; lastRow = row; // // //新的坐标系M,N // int halfWidth = (int)MapCreater.TileRange.x / 2; // M点在像素坐标系X轴的偏移 // int halfHeight = (int)MapCreater.TileRange.y / 2; // 像素坐标系Y轴的偏移 for (int i = 0; i < mapCount; i++) { GameObject gObj = GameObject.Instantiate(MapElements) as GameObject; gObj.transform.parent = root.transform; gObj.name += i; TiledMap tiled = gObj.GetComponent <TiledMap>(); float mapWidth = tiled.GetMapWidthInPixelsScaled(); float mapHeight = tiled.GetMapHeightInPixelsScaled(); float halfWidth = mapWidth / 2; float halfHeight = mapHeight / 2; // int indexX = i%row; // int indexY = i/row + i % row; // i % 2 == 0 ? i / 2 + 0.5 : i - 1 // gObj.transform.position = new Vector3(-offsetWidth * (indexX / 2 + indexX % 2 * 0.5f) , // -offsetHeight * (i / row + (i % row) * 0.5f)); int column = i / row; float tiledX = halfWidth * column - halfWidth * (i % row); float tiledY = -halfHeight * column - halfHeight * (i % row); gObj.transform.position = new Vector3(tiledX, tiledY); gObj.transform.localScale = Vector3.one; } }
void Start() { // first of all, lets find the size of the level we are scrolling TiledMap scrollingLevel = GetComponent <TiledMap> (); float scrollx = scrollingLevel.GetMapWidthInPixelsScaled(); float scrolly = scrollingLevel.GetMapHeightInPixelsScaled(); // and the size of the camera box float camerax = cam.orthographicSize * Screen.width / Screen.height * 2; float cameray = cam.orthographicSize * 2; // and the size of the level we are locking our scrolling to float levelx = level.GetMapWidthInPixelsScaled(); float levely = level.GetMapHeightInPixelsScaled(); // so the levels have their origin at the top left corner, // we need the center for our calculations offset = new Vector2(-scrollx / 2, scrolly / 2); // we also need the main level's center Vector2 corner = level.transform.position; center = new Vector2(corner.x + levelx / 2, corner.y - levely / 2); // finally, what's the scaling factor between camera size // and scroll-level size? we need this to calculate where // we should place the scroll level based on the camera's // position in the main level scalex = (float)((levelx - scrollx) / (levelx - camerax)); scaley = (float)((levely - scrolly) / (levely - cameray)); }