ãUnityã2Dã¿ã¤ã«ããã17 2ãã¹ã§1ãªãã¸ã§ã¯ãã®Tileãæ´ãã§ã移åããã¦ãé ç½®ãã
Unity 2021.1.0f1 Personal(2021å¹´3æ)
ä»åã¯ä»¥åæ稿ãã【Unity】2Dタイルマップ12 Tileを掴んで、移動させて、配置するãã¢ãããã¼ããã¾ãï¼
é·ãã¼ãã«ãªã©2ãã¹ã§1ãªãã¸ã§ã¯ãã表ãTileãä¸è¨è¨äºã¨åæ§ã«ç§»åããã¦ããã¾ãã
2ãã¹ã§1ãªãã¸ã§ã¯ãã®Tileãæºåãã
ä»åã¯é·ãã¼ãã«ã®ã¹ãã©ã¤ããç¨æãã¾ããã
table_f.png
table_r.png
過去記事ãåèã«TilePaletteã¸ç»é²ãã¾ããï¼ååè¨äºã§ä½¿ã£ãã¿ã³ã¹ãæ®ãã¦ããã¾ããï¼
2ãã¹ã§1ãªãã¸ã§ã¯ãã®Tileæ å ±ãä¿åã§ããScriptableObject
Tileæ å ±ãä¿åããScriptableObjectãéå»è¨äºã§ä½æãã¾ããã
2ãã¹ã§1ãªãã¸ã§ã¯ãã®Tileæ å ±ãä¿åã§ããããã«ã¢ãããã¼ããã¦ããã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.Tilemaps; [CreateAssetMenu] public class TileScriptableObject : ScriptableObject { public List<TileStore> tileDataList = new List<TileStore>(); } [Serializable] public class TileStore { public string name; public Tile[] tiles; }
TileScriptableObject.cs
ãã¡ãã®ScriptableObjectãçæããtansuã¨tableã®Tileãç»é²ãã¾ãã
ã«ã¼ã«ã¨ãã¦ã¯å¥¥ã«ããTileããé ã«ç»é²ãã¾ããï¼tableã®å ´åã¯table_fãtable_rã®é çªï¼
ScriptableObjectã«Tileãç»é²
以ä¸ã§ScriptableObjectã®æºåã¯å®äºã§ãã
Tilemapãä½æãã
ç¶ãã¦Tilemapãä½æãã¾ãã
家å
·é
ç½®ç¨ã®Tilemap(FurnitureTIlemap)ã¨ç§»åç¨ã®Tilemap(MoveTilemap)ãä½æãã¾ãã
移åç¨ã®ã¿position.yã0.1ã«ãã¾ãã
MoveTilemapã®position.yãå¤æ´
Tileã®ç§»åãã³ã³ããã¼ã«ããScriptä½æ
æå¾ã«Tileã®ç§»åãã³ã³ããã¼ã«ããScriptï¼MoveTileController.csï¼ãç¨æããControllerã¨ããååã®ãªãã¸ã§ã¯ãä½æãã¦åãä»ãã¾ãã
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Tilemaps; public class MoveTileController : MonoBehaviour { [SerializeField] TileScriptableObject tileSO; [SerializeField] Tilemap defaultTilemap; [SerializeField] Tilemap moveTilemap; private Vector3Int selectPos; private PlacedTiles selectTile; private List<PlacedTiles> placedTiles = new List<PlacedTiles>(); private void Start() { SetFurniture("tansu", 0, 0); SetFurniture("table", 2, 3); } private void SetFurniture(string name, int x, int y) { var tileStore = tileSO.tileDataList.Single(t => t.name == name); for (int i = 0; i < tileStore.tiles.Length; i++) { defaultTilemap.SetTile(new Vector3Int(x, y-i, 0), tileStore.tiles[i]); } placedTiles.Add(new PlacedTiles(tileStore, x, y)); } private void Update() { if (Input.GetMouseButtonDown(0)) { SelectTile(); } else if (Input.GetMouseButton(0) && selectTile != null) { MoveTile(); } else if (Input.GetMouseButtonUp(0) && selectTile != null) { DeployTile(); } } private void SelectTile() { var mousePos = Input.mousePosition; mousePos.z = 10f; selectPos = defaultTilemap.WorldToCell(Camera.main.ScreenToWorldPoint(mousePos)); if (!defaultTilemap.HasTile(selectPos)) return; for (int i = 0; i < placedTiles.Count; i++) { for (int j = 0; j < placedTiles[i].tileBundle.Count;j++) { if (placedTiles[i].tileBundle[j].x == selectPos.x && placedTiles[i].tileBundle[j].y == selectPos.y) { selectTile = placedTiles[i]; break; } } if (selectTile != null) break; } for (int i = 0; i < selectTile.tileBundle.Count; i ++) { var pos = new Vector3Int(selectTile.tileBundle[i].x, selectTile.tileBundle[i].y, 0); moveTilemap.SetTile(pos, selectTile.tileBundle[i].tile); defaultTilemap.SetTile(pos, null); } } private void MoveTile() { var mousePos = Input.mousePosition; mousePos.z = 10f; Vector3Int nextPos = defaultTilemap.WorldToCell(Camera.main.ScreenToWorldPoint(mousePos)); if (selectPos == nextPos) return; selectTile.tileBundle.ForEach(tb => { moveTilemap.SetTile(new Vector3Int(tb.x, tb.y, 0), null); }); selectTile.tileBundle.ForEach(tb => { var pos = new Vector3Int(tb.x - (selectPos.x - nextPos.x), tb.y - (selectPos.y - nextPos.y), 0); moveTilemap.SetTile(pos, tb.tile); tb.x = pos.x; tb.y = pos.y; }); selectPos = nextPos; } private void DeployTile() { selectTile.tileBundle.ForEach(tb => { var pos = new Vector3Int(tb.x, tb.y, 0); moveTilemap.SetTile(pos, null); defaultTilemap.SetTile(pos, tb.tile); }); selectTile = null; } public class PlacedTiles { public string name; public List<TileBundle> tileBundle = new List<TileBundle>(); public PlacedTiles(TileStore tileStore, int x, int y) { this.name = tileStore.name; for (int i = 0; i < tileStore.tiles.Length; i++) { this.tileBundle.Add(new TileBundle(tileStore.tiles[i], x, y-i)); } } public class TileBundle { public Tile tile; public int x; public int y; public TileBundle(Tile tile, int x, int y) { this.tile = tile; this.x = x; this.y = y; } } } }
MoveTileController.cs
tileSOã«ä½æããScriptableObjectããdefaultTilemapã«å®¶å ·é ç½®ç¨TilemapããmoveTilemapã«ç§»åç¨Tilemapãç»é²ãã¾ãã
åºæ¬çã«ã¯【Unity】2Dタイルマップ12 Tileを掴んで、移動させて、配置するã§ä½æããScriptã¨åãã§ãUpdateã¡ã½ããã«ã¦Tileãæ´ã(SelectTile)ãTileã移åããã(MoveTile)ãTileãé ç½®ãã(DeployTile)ã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
Startã¡ã½ããã«ã¦SetFurnitureã¡ã½ãããå¼ã³åºããScriptableObjectã«ç»é²ãã家å
·ãSceneã«ã»ãããã¾ãã
家å
·ãã»ããããã¨åæã«PlacedTileã¯ã©ã¹ã«Tileæ
å ±ãç»é²ãã¾ãã
PlacedTileã¯ã©ã¹ã«ã¯TileBundleã¨ããã¯ã©ã¹ã®ListããããTileBundleã¯ã©ã¹ã«ã¯Tileã¨Tileã®ä½ç½®æ
å ±ãä¿åããã¾ãã
2ãã¹ã§1Tileã®ãªãã¸ã§ã¯ããPlacedTileã¯ã©ã¹ã«ä¿åããå ´åãTileBundleã¯ã©ã¹ã«ã¯(x, y)ã¨(x, y-1)ã®2ã¤ã®Tileä½ç½®æ
å ±ãä¿åããã¾ãã
ä»åã®tableã¿ã¤ã«ã®å ´åã¯table_fã¨(x:2, y:3)ãtable_rã¨(x:2, y:2)ã®æ
å ±ãä¿åããã¾ãã
çµæ
ããã§ã¯å®éã«Tileãæ´ãã§åããã¦ã¿ã¾ãã
2ãã¹ã§1ãªãã¸ã§ã¯ãã®ãã¼ãã«Tileã2ãã¹ã¨ãåãã¦ãã¾ãã
ãã¡ãã1ãã¹ã®ã¿ã³ã¹Tileãæ£å¸¸ã«åä½ãã¾ãã
ä»åã¯ä»¥ä¸ã¨ãªãã¾ãã
ãããã¨ããããã¾ãããã