Yang.공부방

메모장

Memo2019. 6. 5. 11:37

[NGUI ]

콜라이더를 따로 붙여야 한다.

Depth로 우선순위 설정을 할 수 있다


UICamera의 존재여부

SceneManager


[Atlas] 

(주의할 점 : 2017이하버전과 2018버전은 완전히 다르다) 

DrawFall 을 줄이기 위함



https://github.com/yang930729/Unity-study-ugui-ngui.git



[안드로이드 빌드할때 필요한 폴더주소]

C:\Users\user\AppData\Local\Android\Sdk

C:\Users\user\AppData\Local\Android\Sdk\platform-tools

Astar알고리즘 이용하여 타일이동

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Tile : MonoBehaviour
{
    public TextMesh G;
    public TextMesh H;
    public TextMesh F;
    public GameObject arrow;
 
    public float g;
    public float h;
    public float f;
    
    public Vector2 vec2;
    public bool closeNode;
    public bool isBlock;
 
    public Tile(Vector2 vec2)
    {
        this.vec2 = vec2;
    }
}
cs


1
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
 
public class TestAstar : MonoBehaviour
{
    public int col;
    public int row;
    public Vector2[] impossiblePos;
    public Vector2 startPos;
    public Vector2 endPos;
 
    public Button findNextNodeBtn;
    public Button displayOpenList;
    public Button closeListBtn;
    public Button clearColorBtn;
    public Button findNodeBtn;
 
    private int tileHeight = 100;
    private int tileWidth = 100;
    private Vector2 tilesPos;
 
    private GameObject selectedTile;
    private GameObject tile;
    private GameObject targetNode;
    private GameObject motherNode;
    private List<GameObject> openList = new List<GameObject>();
    private List<GameObject> closeList = new List<GameObject>();
    private Dictionary<Vector2, GameObject> dicFindTile = new Dictionary<Vector2, GameObject>();
 
    private void Awake()
    {
 
    }
 
    void Start()
    {
        this.CreateMap();
        this.ButtonManager();
    }
 
    #region 버튼
    private void ButtonManager()
    {
        this.findNextNodeBtn.onClick.AddListener(() =>
        {
            this.SortingOpenList();
            this.NextNode();
        });
 
        this.displayOpenList.onClick.AddListener(() =>
        {
            foreach(var tile in this.openList)
            {
                tile.GetComponentInChildren<SpriteRenderer>().color = Color.yellow;
                this.SetActiveArrowAndFGH(tile);
            }
        });
 
        this.closeListBtn.onClick.AddListener(() =>
        {
            this.CloseListColoring();
        });
 
        this.findNodeBtn.onClick.AddListener(() =>
        {
            this.FindNode();
        });
 
        this.clearColorBtn.onClick.AddListener(() =>
        {
            this.ClearColor();
        });
    }
    #endregion
 
    private void SortingOpenList()
    {
        IEnumerable<GameObject> sortNode = this.openList.OrderBy(x => x.GetComponent<Tile>().f);
 
        //foreach (var node in sortNode)
        //{
        //    Debug.Log(node.GetComponent<Tile>().f);
        //}
 
        this.selectedTile = sortNode.FirstOrDefault<GameObject>();
    }
 
    private void NextNode()
    {
        this.motherNode = this.selectedTile;
        this.selectedTile.GetComponentInChildren<SpriteRenderer>().color = Color.magenta;
    }
 
    private void SetFGH()
    {
        foreach (var tile in this.openList)
        {
            var tileScript = tile.GetComponent<Tile>();
            
            this.targetNode.GetComponent<Tile>();
            var getG = tile.GetComponent<Tile>().g;
            var getH = tile.GetComponent<Tile>().h;
            getG = Mathf.Round(Vector2.Distance(tile.GetComponent<Tile>().vec2, this.motherNode.GetComponent<Tile>().vec2) * 10);
            var dx = Mathf.Abs(tile.GetComponent<Tile>().vec2.x - this.endPos.x);
            var dy = Mathf.Abs(tile.GetComponent<Tile>().vec2.y - this.endPos.y);
 
            getH = (dx + dy) * 10;
 
            tile.GetComponent<Tile>().f = getG + getH;
 
            tileScript.G.text = getG.ToString();
            tileScript.H.text = getH.ToString();
            tileScript.F.text = tile.GetComponent<Tile>().f.ToString();
        }
    }
 
    private void TurnArrow()
    {
 
    }
 
    private void CloseListColoring()
    {
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; j++)
            {
                var test = new Vector2(j, i);
 
                var tiles = dicFindTile[test];
 
                if (this.closeList.Contains(tiles))
                {
                    tiles.GetComponent<SpriteRenderer>().color = Color.cyan;
                }
            }
        }
    }
 
    #region 타일초기화
    private void ClearColor()
    {
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; j++)
            {
                var tiles = new Vector2(j, i);
 
                var tile = dicFindTile[tiles];
 
                tile.GetComponent<SpriteRenderer>().color = Color.black;
                tile.GetComponent<Tile>().arrow.SetActive(false);
 
                var tilePos = tile.GetComponent<Tile>().vec2;
                
                if (tilePos == startPos)
                {
                    tile.GetComponent<SpriteRenderer>().color = Color.green;
                }
                if (tilePos == endPos)
                {
                    tile.GetComponent<SpriteRenderer>().color = Color.red;
                }
                for (int a = 0; a < this.impossiblePos.Length; a++)
                {
                    if (tilePos == this.impossiblePos[a])
                    {
                        tile.GetComponent<SpriteRenderer>().color = Color.blue;
                    }
                }
 
                
            }
        }
    }
    #endregion
 
    #region 타일생성
    private void CreateMap()
    {
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; j++)
            {
                this.tilesPos = new Vector2(j, i);
                this.tile = Instantiate<GameObject>(Resources.Load("tile01"as GameObject);
                var tileLine = Instantiate<GameObject>(Resources.Load("tileLine2"as GameObject);
 
                tile.transform.SetParent(GameObject.Find("tiles").transform);
                tileLine.transform.SetParent(tile.transform);
 
                var screenPos = new Vector2(j * this.tileWidth + 512 - 300, i * -this.tileHeight + this.tileHeight * 6);
                var worldPos = Camera.main.ScreenToWorldPoint(screenPos);
                worldPos.z = 0;
 
                tile.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", j, i);
                tile.transform.position = worldPos;
                var tileJ = (tile.GetComponent<Tile>().vec2.x = j);
                var tileI = (tile.GetComponent<Tile>().vec2.y = i);
 
                this.TileColoring(j, i, tile);
                this.dicFindTile.Add(new Vector2(j, i), tile);
 
            }
        }
    }
 
    private void TileColoring(int j, int i, GameObject tile)
    {
        if (this.tilesPos == this.startPos)
        {
            tile.name = "start";
            tile.GetComponent<SpriteRenderer>().color = Color.green;
            this.motherNode = tile;
        }
        if (this.tilesPos == this.endPos)
        {
            tile.name = "end";
            tile.GetComponent<SpriteRenderer>().color = Color.red;
            this.targetNode = tile;
        }
        for (int a = 0; a < this.impossiblePos.Length; a++)
        {
            if (this.tilesPos == this.impossiblePos[a])
            {
                tile.name = "block";
                tile.GetComponent<SpriteRenderer>().color = Color.blue;
                tile.GetComponent<Tile>().isBlock = true;
            }
        }
        this.SetActiveArrowAndFGH(tile);
    }
    #endregion
    
    private void SetActiveArrowAndFGH(GameObject tile)
    {
        if (!this.openList.Contains(tile))
        {
            tile.GetComponent<Tile>().G.gameObject.SetActive(false);
            tile.GetComponent<Tile>().F.gameObject.SetActive(false);
            tile.GetComponent<Tile>().H.gameObject.SetActive(false);
            tile.GetComponent<Tile>().arrow.SetActive(false);
        }
        else
        {
            tile.GetComponent<Tile>().G.gameObject.SetActive(true);
            tile.GetComponent<Tile>().F.gameObject.SetActive(true);
            tile.GetComponent<Tile>().H.gameObject.SetActive(true);
            tile.GetComponent<Tile>().arrow.SetActive(true);
        }
    }
 
    private void FindNode()
    {
        var mother = this.motherNode.GetComponent<Tile>();
 
        var motherX = mother.vec2.x - 1;
        var motherY = mother.vec2.y - 1;
        var maxX = mother.vec2.x + 1;
        var maxY = mother.vec2.y + 1;
 
        if (motherX < 0)
        {
            motherX = 0;
        }
        if (motherY < 0)
        {
            motherY = 0;
        }
 
        for (int x = (int)motherX; x <= maxX; x++)
        {
            for (int y = (int)motherY; y <= maxY; y++)
            {
                var data = new Vector2(x, y);
                var tile = this.dicFindTile[data];
 
                if (tile.GetComponent<Tile>().isBlock != true && !this.closeList.Contains(tile))
                {
                    this.openList.Add(tile);
                    this.openList.Remove(motherNode);
                    this.SetActiveArrowAndFGH(tile);
                    this.SetFGH();
                    this.closeList.Add(motherNode);
                    if (tile != this.motherNode)
                    {
                        tile.GetComponent<SpriteRenderer>().color = Color.yellow;
                        this.TurnArrow(tile);
                    }
                }
            }
        }
    }
 
    private void TurnArrow(GameObject tile)
    {
        var script = tile.GetComponent<Tile>();
        var xPos = this.motherNode.transform.position.x - script.arrow.transform.position.x;
        var yPos = this.motherNode.transform.position.y - script.arrow.transform.position.y;
        var rad = Mathf.Atan2(yPos, xPos) * Mathf.Rad2Deg;
        script.arrow.transform.rotation = Quaternion.Euler(new Vector3(00, rad + 90));
    }
}
 
cs


'Project' 카테고리의 다른 글

isometric 참고 사이트  (0) 2019.06.06

isometric

Unity/수업내용2019. 5. 12. 22:33

참고 : http://clintbellanger.net/articles/isometric_math/




1
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
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
 
public class Helloworld : MonoBehaviour
{
    public Button btn;
 
    private int tileWidth = 64;
    private int tileHeight = 64;
    private int col = 0;
    private int row = 0;
 
    void Start()
    {
        StringBuilder sb = new StringBuilder();
 
        btn.onClick.AddListener(() =>
        {
            this.CreateTile(col, row);
            col++;
            if(col >= 3)
            {
                col = 0;
                row++;
            }
 
            if (col == 0 && row ==3)
            {
                var textPos = this.btn.transform.position;
                this.btn.gameObject.SetActive(false);
            }
        });
    }
 
    private void CreateTile(float x, float y)
    {
        var tile = Instantiate(Resources.Load<GameObject>("64x64"));
        
        var map = new Vector2(x, y);
        
        tile.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y);
 
        var screen = this.MapToScreen(map);
 
        var worldPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512 - 512, screen.y + 384 + 384));
 
        tile.transform.position = new Vector3(worldPos.x, worldPos.y, 0);
    }
 
    public Vector2 MapToScreen(Vector2 mapPos)
    {
        var screenX = mapPos.x * tileWidth;
        var screenY = -mapPos.y * tileHeight;
 
        return new Vector2(screenX, screenY);
    }
}
 
cs



1
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestIsoTile : MonoBehaviour
{
    public Button btn;
 
    private int tileWidth = 64;
    private int tileHeight = 32;
    private int col = 0;
    private int row = 0;
 
    void Start()
    {
        this.btn.onClick.AddListener(()=>
        {
            CreateTile(col, row);
            col++;
 
            if(col >=3)
            {
                col = 0;
                row++;
            }
 
            if (col == 0 && row == 3)
            {
                var textPos = this.btn.transform.position;
                this.btn.gameObject.SetActive(false);
            }
        });
    }
 
    private void CreateTile(float x, float y)
    {
        var tile = Instantiate(Resources.Load<GameObject>("isoTile"));
 
        var map = new Vector2(x, y);
 
        tile.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y);
 
        var screen = this.MapToScreen(map);
 
        var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512, screen.y + 384));
        
        tile.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);
    }
 
    public Vector2 ScreenToMap(Vector2 screenPos)
    {
        //128,64 ->(2,1)
        var screenX = (int)screenPos.x / tileWidth;
        var screenY = (int)screenPos.y / tileHeight;
 
        return new Vector2(screenX, screenY);
    }
}
 
cs


수정 후

1
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class TestIsoTile : MonoBehaviour
{
    public Button btn;
 
    public int colCount;
    public int rowCount;
 
    private GameObject tile;
    private int tileWidth = 64;
    private int tileHeight = 32;
    private int col = 0;
    private int row = 0;
    private Coroutine routine;
    
    void Start()
    {
        this.btn.onClick.AddListener(() =>
        {
            while (true)
            {
                CreateTile(this.col, row);
                this.col++;
 
                if (this.col >= this.colCount)
                {
                    this.col = 0;
                    this.row++;
                }
                if (this.row >= this.rowCount)
                {
                    //this.btn.gameObject.SetActive(false);
                    row = 0;
                    break;
                }
            }
        });
 
        this.TileCheck();
    }
 
    private void Update()
    {
        
    }
 
    void TileCheck()
    {
        if(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)
                {
                    Debug.Log(hit.collider.GetComponentInChildren<TextMesh>().text);
                }
            }
            yield return null;
        }
    }
 
    private void CreateTile(float x, float y)
    {
        this.tile = Instantiate(Resources.Load<GameObject>("isoTile"));
 
        this.tile.tag = "Tile";
 
        var isometric = GameObject.Find("isometricList");
 
        this.tile.transform.SetParent(isometric.transform);
 
        var map = new Vector2(x, y);
 
        this.tile.GetComponentInChildren<TextMesh>().text = string.Format("({0},{1})", x, y);
 
        var screen = this.MapToScreen(map);
 
        var screenPos = Camera.main.ScreenToWorldPoint(new Vector2(screen.x + 512, screen.y + 384));
 
        this.tile.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);
    }
}
cs