public override void UpdateTileAt(int x, int y) { int index = x + y * tileMap.Width; SpriteRenderer current = spriteMap[index]; if (current == null) { current = new GameObject(string.Format("[{0}, {1}]", x, y)).AddComponent <SpriteRenderer>(); current.transform.SetParent(parent); spriteMap[index] = current; } ScriptableTile tile = tileMap.GetTileAt(x, y); current.sprite = tile ? tile.GetSprite(tileMap, new Point(x, y)) : null; current.transform.localPosition = new Vector2(x, y) + (tile ? new Vector2(current.sprite.pivot.x / current.sprite.rect.width, current.sprite.pivot.y / current.sprite.rect.height) : Vector2.zero); current.transform.localScale = Vector2.one; current.sharedMaterial = material; current.color = color; current.sortingLayerID = sortingLayer; current.sortingOrder = orderInLayer; current.gameObject.SetActive(tile != null); }
public void UpdateTile(int x, int y) { int index = x + y * tileMap.Width; SpriteRenderer current = spriteMap [index]; if (current == null) { current = new GameObject(string.Format("[{0}, {1}]", x, y), typeof(PolygonCollider2D)).AddComponent <SpriteRenderer>(); current.transform.position = new Vector2(x, y); current.transform.localScale = Vector2.one; current.transform.SetParent(transform); current.gameObject.hideFlags = HideFlags.HideInHierarchy; current.sharedMaterial = material; current.color = color; spriteMap [index] = current; } ScriptableTile tile = tileMap.GetTileAt(x, y); if (current.GetComponent <PolygonCollider2D> ()) { DestroyImmediate(current.GetComponent <PolygonCollider2D> ()); } current.sprite = tile ? tile.GetSprite(tileMap, new Point(x, y)) : null; if (current.sprite) { current.gameObject.AddComponent <PolygonCollider2D> (); } }
public override void UpdateTileAt(int x, int y) { //var currentTexture = textureAtlas; ScriptableTile tile = tileMap.GetTileAt(x, y); int quadIndex = x + y * tileMap.Width; quadIndex *= 4; var uv = meshFilter.sharedMesh.uv; if (tile == null) { uv[quadIndex] = uv[quadIndex + 1] = uv[quadIndex + 2] = uv[quadIndex + 3] = Vector2.zero; meshFilter.sharedMesh.uv = uv; return; } Vector2[] prevUVs = new Vector2[] { uv[quadIndex], uv[quadIndex + 1], uv[quadIndex + 2], uv[quadIndex + 3] }; Sprite sprite = tile.GetSprite(tileMap, new Point(x, y)); bool atlasUpdated = AddSpriteToAtlas(sprite); // assign four uv coordinates to change the texture of one tile (one quad, two triangels) Rect spriteRect = rects[spriteList.IndexOf(sprite)]; Vector2[] newUVs = new Vector2[] { new Vector2(spriteRect.xMin, spriteRect.yMin), new Vector2(spriteRect.xMax, spriteRect.yMin), new Vector2(spriteRect.xMin, spriteRect.yMax), new Vector2(spriteRect.xMax, spriteRect.yMax) }; if (!newUVs.SequenceEqual(prevUVs)) { uv[quadIndex] = newUVs[0]; uv[quadIndex + 1] = newUVs[1]; uv[quadIndex + 2] = newUVs[2]; uv[quadIndex + 3] = newUVs[3]; meshFilter.sharedMesh.uv = uv; if (atlasUpdated) { //FIXME so that it only updates tiles who have changed uvs tileMap.UpdateType(tile); } } }