Isometric, 클릭시 좌표 출력하기
Unity/과제 2019. 5. 13. 02:351 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Text; public class TestIsometric : MonoBehaviour { public Button btn; public Camera camera; public GameObject tileParty; public GameObject isoTileParty; private float tileWidth = 0.64f; private float tileHeight = 0.64f; private float isoTileWidth = 0.64f; private float isoTileHeight = 0.32f; private int cntX = 0; private int cntY = 0; // Start is called before the first frame update void Start() { this.btn.onClick.AddListener(() => { this.CreateIsoTile(); }); } private void Update() { if(Input.GetMouseButtonDown(0)) { Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero); if(hit.collider!=null) { var script = hit.collider.GetComponent<TestisoTile>(); Debug.LogFormat("This tile's coordinate : ({0},{1})", script.x, script.y); } } } public void CreateIsoTile() { var tileGo = GameObject.Instantiate(Resources.Load<GameObject>("isoTile")); tileGo.transform.SetParent(this.isoTileParty.transform); var txt = tileGo.GetComponentInChildren<TextMesh>(); txt.text = string.Format("({0},{1})", this.cntX, this.cntY); var script = tileGo.GetComponent<TestisoTile>(); script.x = this.cntX; script.y = this.cntY; tileGo.transform.position = this.MakeIsoTile(new Vector2(this.cntX, this.cntY)); cntX++; if (this.cntX == 7) { this.cntX = 0; this.cntY++; } if (this.cntY == 4 ) { this.btn.gameObject.SetActive(false); } } public Vector2 MakeIsoTile(Vector2 mapPos) { if(this.cntY >= 1) { var screenX = this.isoTileParty.transform.position.x + ((0.64f * -mapPos.y) + (0.64f * mapPos.x)); var screenY = this.isoTileParty.transform.position.y + -(0.32f * (mapPos.y + mapPos.x)); return new Vector2(screenX, screenY); } else { var screenX = this.isoTileParty.transform.position.x + mapPos.x * this.isoTileWidth; var screenY = this.isoTileParty.transform.position.y - mapPos.x * this.isoTileHeight; return new Vector2(screenX, screenY); } } public void CreateTile() { var tileGo = GameObject.Instantiate(Resources.Load<GameObject>("Tile")); tileGo.transform.SetParent(this.tileParty.transform); var txt = tileGo.GetComponentInChildren<TextMesh>(); txt.text = string.Format("({0},{1})", this.cntX, this.cntY); tileGo.transform.position = this.MapToScreen(new Vector2(this.cntX, this.cntY)); cntX++; if (this.cntX == 4) { this.cntX = 0; this.cntY++; } if (this.cntY == 4) { this.btn.gameObject.SetActive(false); } } public Vector2 MapToScreen(Vector2 mapPos) { //var screenX = mapPos.x * this.tileWidth + -10f * this.tileWidth; //var screenY = -(mapPos.y * this.tileHeight) + 3.6f; var screenX = this.tileParty.transform.position.x + mapPos.x * this.tileWidth; var screenY = this.tileParty.transform.position.y - mapPos.y * this.tileHeight; return new Vector2(screenX, screenY); } public Vector2 ScreenToMap(Vector2 screenPos) { //128,64 -> 2,1 var mapX = (int)screenPos.x / this.tileWidth; var mapY = (int)screenPos.y / this.tileHeight; return new Vector2(mapX, mapY); } } | cs |
'Unity > 과제' 카테고리의 다른 글
2D Running Game 기본 시스템 만들기 (0) | 2019.05.09 |
---|---|
씬 전환 및 가서 공격하기 (0) | 2019.04.16 |
왔다갔다 때리기 (0) | 2019.04.16 |
0 (0) | 2019.04.11 |