타일맵에서 my 타일을 지정한 위치로 이동시키기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class TestIsometric : MonoBehaviour
{
public Button createTileBtn;
public Button createMyTilebtn;
public int colCount, rowCount;
public int myColPosition, myRowPosition;
private GameObject mapTile;
private Coroutine routine;
private int tileWidth = 64;
private int tileHeight = 32;
private int col = 0; //행
private int row = 0; //열
private GameObject my;
private Vector2 vec2;
private Vector2 tileVec2;
private Dictionary<Vector2, GameObject> dicFIndTile = new Dictionary<Vector2, GameObject>();
void Start()
{
this.CreateMap();
this.createTileBtn.onClick.AddListener(() =>
{
});
createMyTilebtn.onClick.AddListener(() =>
{
this.CreateTile();
});
this.TileCheck();
}
void TileCheck()
{
if (this.routine != null)
{
StopCoroutine(this.routine);
}
this.routine = StartCoroutine(this.TileCheckImpl());
}
IEnumerator TileCheckImpl()
{
while (true)
{
if (Input.GetMouseButtonDown(0))
{
Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPos, Vector2.zero);
if (hit.collider != null)
{
var data = this.dicFIndTile.TryGetValue(this.vec2, out this.mapTile);
if (hit.collider == data)
{
var target = hit.collider.gameObject;
this.MoveTransform(target);
break;
}
}
}
yield return null;
}
}
private void MoveTransform(GameObject target)
{
if (this.routine != null)
{
StopCoroutine(this.routine);
}
this.routine = StartCoroutine(this.MoveTransformImpl(target));
}
private IEnumerator MoveTransformImpl(GameObject target)
{
while (true)
{
var dir = (target.transform.position - this.my.transform.position).normalized;
var distance = Vector3.Distance(target.transform.position, this.my.transform.position);
this.my.transform.position += dir * 2f * Time.deltaTime;
if (distance <= 0.02f)
{
this.my.transform.position = target.transform.position;
Debug.Log("이동완료");
this.TileCheck();
break;
}
yield return null;
}
}
private void CreateTile()
{
if (this.my == null)
{
this.my = Instantiate(Resources.Load<GameObject>("myIsoTile"));
//this.my.tag = "Tile";
//this.my.transform.SetParent(GameObject.Find("TestIsometricBuilding").transform);
this.CreatedTileMap(my, this.myColPosition, myRowPosition);
}
}
private void CreateMap()
{
while (true)
{
this.mapTile = Instantiate(Resources.Load<GameObject>("isoTile"));
this.mapTile.tag = "Tile";
var isometric = GameObject.Find("TestIsometricBuilding");
this.mapTile.transform.SetParent(isometric.transform);
this.CreatedTileMap(this.mapTile, this.col, row);
this.col++;
if (this.col >= this.colCount)
{
this.col = 0;
this.row++;
}
if (this.row >= this.rowCount)
{
this.createTileBtn.gameObject.SetActive(false);
row = 0;
break;
}
this.dicFIndTile.Add(new Vector2(col, row), mapTile);
}
}
private void CreatedTileMap(GameObject tileMap, float x, float y)
{
this.vec2 = new Vector2(x, y);
if (tileMap.GetComponentInChildren<TextMesh>())
{
tileMap.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y);
}
var screen = this.MapToScreen(vec2);
var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512, screen.y + 384 + 100));
tileMap.transform.position = new Vector3(screenPos.x, screenPos.y, 0);
}
public Vector2 MapToScreen(Vector2 mapPos)
{
var screenX = mapPos.x * this.tileWidth - (mapPos.y * this.tileWidth);
var screenY = mapPos.y * -tileHeight - (mapPos.x * tileHeight);
return new Vector2(screenX, screenY);
}
}