1. Contains 키워드
해당 요소가 배열 혹은 리스트에 포함되어 있는지 여부를 체크해 bool 값을 반환한다.
docs.unity3d.com/kr/530/ScriptReference/ArrayUtility.Contains.html
Unity - 스크립팅 API: ArrayUtility.Contains
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기
docs.unity3d.com
2. Add()
리스트나 배열의 마지막에 요소를 추가한다.
docs.unity3d.com/kr/530/ScriptReference/Array.Add.html
Unity - 스크립팅 API: Array.Add()
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기
docs.unity3d.com
3. RemoveAt()
리스트나 배열의 인덱스 요소를 제거한다. 여기서 인덱스 요소란, 괄호 안의 숫자 위치에 해당하는 요소를 뜻한다.
docs.unity3d.com/kr/530/ScriptReference/Array.RemoveAt.html
Unity - 스크립팅 API: Array.RemoveAt()
Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기
docs.unity3d.com
4. 숫자 콤보!
간단한 합, 차 계산 결과를 맞춰 콤보를 쌓는 게임을 만들어 보자.
우선 캔버스에 버튼 두개(Dir+, Dir-)와 이미지 안에 텍스트를 넣은 오브젝트(Block(n))를 원하는 만큼 만든다.
맞췄을 경우 동그라미, 틀렸을 경우 엑스를 화면에 표시할 것이다. True와 False라는 빈 게임 오브젝트를 만들고 그 안에 UI 이미지를 만들었다.
유니티에서는 저 정도는 드로잉할수 있는 기본 도구를 제시한다.
Image 컴포넌트 부분의 Source Image에서 Knob을 선택하면 동그라미가 그려진다. 그리고 아래 Color를 수정하면 해당 색상의 동그라미가 그려진다. 필자의 경우 배경 색상으로 동그라미를 하나 더 그려 안이 뚫려있는 느낌을 내 주었다.
X의 경우는 대충 기본 이미지가 사각형이니 색상만 바꾸어 길쭉하게 늘리고 조금 회전시킨 후, 복사해서 뒤집으면 된다.
콤보를 출력하는 텍스트는 새로운 캔버스에 만들 것이다. 이유는 콤보 텍스트가 계산 식(Block)에 가려질 수 있기 때문이다.
새로 만든 캔버스의 목적은 블럭들에 의해 가려지지 않도록 하기 위함이다. 따라서 캔버스 오브젝트의 캔버스 컴포넌트에서 Sort Order를 1로 수정한다. 간단하게 깊이라고 생각하면 편하다. 높을수록 상단에 배치된다.
마지막으로 게임을 종료할 X 버튼을 새로 만든 캔버스에 생성하면 인터페이스는 완성이다.
Manager라는 빈 게임 오브젝트 하나를 만들고 Manager C# 스크립트 파일을 하나 만든 후, 컴포넌트 추가를 해 주자.
아래는 Manager C# 스크립트 전문이다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Manager : MonoBehaviour
{
public List<GameObject> list_Block = new List<GameObject>();
List<int> list_Number1 = new List<int>();
List<int> list_Number2 = new List<int>();
List<float> list_PosY = new List<float>();
int nSum = 0;
private int nCombo = 0;
public Text txt_combo;
public GameObject goTrue;
public GameObject goFalse;
public GameObject goBlock;
public Transform trans_parent;
public Text txt_right;
public Text txt_left;
IEnumerator DealyActiveTF(GameObject go) {
go.SetActive(true);
yield return new WaitForSeconds(0.5f);
go.SetActive(false);
}
void ResetPosition() {
nCombo++;
txt_combo.text = nCombo + " Combo!!";
StartCoroutine(DealyActiveTF(txt_combo.gameObject));
GameObject go = Instantiate(goBlock, trans_parent);
//Instantiate(a,b); -> a 오브젝트를 b 위치에 복제
//go 오브젝트는 포지션을 리셋할때마다 계속 갱신되는 goBlock 오브젝트 복제본을 갖는다.
list_Block.Add(go);
list_Number1.Add(Random.Range(0, 10));
list_Number2.Add(Random.Range(0, 10));
int nRand = Random.Range(0, 2);
string str = "";
switch (nRand)
{
case 0:
str = "+";
break;
case 1:
str = "-";
break;
}
list_Block[list_Block.Count - 1].transform.GetChild(0).GetComponent<Text>().text =
list_Number1[list_Number1.Count - 1] + str + list_Number2[list_Number2.Count - 1];
for (int i = 0; i < list_Block.Count; i++) {
list_Block[i].transform.localPosition = new Vector3(0, list_PosY[i], 0);
}
int nRandom = Random.Range(0, 2);
if (nRandom == 0)
{
txt_right.text = nSum.ToString();
txt_left.text = (nSum + Random.Range(1, 3)).ToString();
}
else
{
txt_left.text = nSum.ToString();
txt_right.text = (nSum + Random.Range(1, 3)).ToString();
}
}
void Init() {
for (int i = 0; i < list_Block.Count; i++) {
list_PosY.Add(list_Block[i].transform.localPosition.y); //리스트 블록의 y좌표를 가져온다.
list_Number1.Add(Random.Range(0, 10)); //리스트에 인자를 추가할 때는 Add함수를 사용한다.
list_Number2.Add(Random.Range(0, 10));
int nRand = Random.Range(0, 2);
string str = "";
switch (nRand) {
case 0:
str = "+";
break;
case 1:
str = "-";
break;
}
list_Block[i].transform.GetChild(0).GetComponent<Text>().text = list_Number1[i] + str + list_Number2[i];
}
if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("+"))
//Contains 키워드: 배열에 해당 요소가 포함되어 있으면 true를 반환합니다.
{
nSum = list_Number1[0] + list_Number2[0];
}
else if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("-")) {
nSum = list_Number1[0] - list_Number2[0];
}
int nRandom = Random.Range(0, 2);
if (nRandom == 0)
{
txt_right.text = nSum.ToString();
txt_left.text = (nSum + Random.Range(1, 3)).ToString();
}
else
{
txt_left.text = nSum.ToString();
txt_right.text = (nSum + Random.Range(1, 3)).ToString();
}
}
void Start()
{
Init();
}
void Update()
{
}
public void OnButtonClick(int n) {
if (n == 0)
{
if (int.Parse(txt_right.text)==nSum)
{
Destroy(list_Block[0].gameObject); //게임 오브젝트를 제거합니다.
list_Block.RemoveAt(0); //배열의 인덱스 요소를 제거합니다. 여기서 인덱스 요소는 괄호 안의 숫자 위치에 해당하는 요소를 뜻합니다.
list_Number1.RemoveAt(0);
list_Number2.RemoveAt(0);
StartCoroutine("DealyActiveTF", goTrue); //인자를 받는 코루틴 함수 호출법, 함수 이름을 적고 콤마 뒤에 인자를 적는다.
//goTrue.SetActive(true);
//goFalse.SetActive(false);
if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("+"))
{
nSum = list_Number1[0] + list_Number2[0];
}
else if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("-"))
{
nSum = list_Number1[0] - list_Number2[0];
}
ResetPosition();
}
else {
StartCoroutine("DealyActiveTF", goFalse);
nCombo = 0;
//goTrue.SetActive(false);
//goFalse.SetActive(true);
print("Fail");
}
}
else if(n==1) {
if (int.Parse(txt_left.text)==nSum)
{
Destroy(list_Block[0].gameObject);
list_Block.RemoveAt(0);
list_Number1.RemoveAt(0);
list_Number2.RemoveAt(0);
StartCoroutine("DealyActiveTF", goTrue);
//goTrue.SetActive(true);
//goFalse.SetActive(false);
if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("+"))
{
nSum = list_Number1[0] + list_Number2[0];
}
else if (list_Block[0].transform.GetChild(0).GetComponent<Text>().text.Contains("-"))
{
nSum = list_Number1[0] - list_Number2[0];
}
ResetPosition();
}
else
{
StartCoroutine("DealyActiveTF", goFalse);
nCombo = 0;
//goTrue.SetActive(false);
//goFalse.SetActive(true);
print("Fail");
}
}
}
public void Quit() {
Application.Quit(); //종료
}
}
|
cs |
그리고 Manager 오브젝트에 Manager 스크립트 컴포넌트 부분에 해당 오브젝트를 넣어주면 된다. 리스트의 경우 사이즈를 아까 만든 블럭의 갯수대로 하면 Element가 숫자대로 나온다. 거기에 만든 모든 블럭을 넣어주면 된다. 또 Block(1) 프리팹은 그냥 아무 블럭이나 하나 프로젝트로 끌고와서 프리팹으로 만든 다음 넣어주면 된다. Trans_parent의 캔버스는 블럭들이 들어 있는 캔버스이다. 블럭들이 내려올 때 y좌표를 맞추기 위해 필요한 녀석이다. Txt_right에는 Div+ 산하의 텍스트를, Txt_left에는 Div- 산하의 텍스트를 넣어주면 된다.
마지막으로, 버튼 온클릭 부분에 Manager 오브젝트를 넣고 OnButtonClick 함수를 선택한 후 각각 Div+에는 인자 0을, Div-에는 인자 1을 입력한다.
또, 종료 버튼엔 Quit 함수를 선택해준다.
여기까지 하면 원하는 대로 게임이 실행될 것이다.
'수업 복습 > 유니티 복습 노트' 카테고리의 다른 글
[유니티] 유니티 수업 10차시 (0) | 2021.05.20 |
---|---|
[유니티] 유니티 수업 5, 6차시 (0) | 2021.04.15 |
[유니티] 유니티 수업 4차시 (0) | 2021.04.02 |
[유니티] 유니티 수업 3차시 (0) | 2021.03.25 |
[유니티] 유니티 수업 2차시 (0) | 2021.03.18 |
댓글