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 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; } }
private TiledMap LoadNextMapPart(float baseX) { string partFile; if (partsLoaded < partsStart.Count) { partFile = partsStart[partsLoaded]; } else if (partsLoaded < 6) { partFile = partsEasy[Random.Range(0, partsEasy.Count)]; } else if (partsLoaded < 12) { // 50/50 chance of getting an easy or medium part if (Random.Range(0, 1) == 1) { partFile = partsEasy[Random.Range(0, partsEasy.Count)]; } else { partFile = partsMed[Random.Range(0, partsMed.Count)]; } } else { partFile = partsMed[Random.Range(0, partsMed.Count)]; } string partPath = partsDirectory + "/" + partFile; GameObject partPrefabGO = Resources.Load(partPath) as GameObject; Assert.IsTrue(partPrefabGO != null, "The part prefab does not exist! Path:" + partPath); TiledMap partPrefab = partPrefabGO.GetComponent <TiledMap>(); Assert.IsTrue(partPrefab != null, "The part prefab does not have a TiledMap component."); TiledMap part = Instantiate(partPrefab, new Vector3(baseX, partPrefab.GetMapHeightInPixelsScaled() / 2), Quaternion.identity, transform); Rigidbody2D physicsBody = part.GetComponent <Rigidbody2D>(); Assert.IsTrue(physicsBody != null, "The map '" + partPath + "' needs to have a Rigidbody2D."); physicsBody.velocity = speed * Vector2.left; partsLoaded += 1; return(part); }
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)); }