빗물받는 르탄이 게임
public GameObject rain;
void Srart()
{
InvokeRepeating("MakeRain", 0f, 1f)
}
void MakeRain()
{
instantiate(rain)
}
풍선을 지켜라 게임
public GameObject endPanel;
public Text timeTxt;
public Text nowScore;
public Text bestScore;
public Animator anim;
bool isPlay = true;
float time = 0.0f;
public void GameOver()
{
isPlay = false;
anim.Setbool("isDie", true);
invoke("TimeStop", 0.5f);
nowScore.text = time.ToString("N2");
if (PlayerPrefs.HasKey("bestScore"))
{
float best = PlayerPrefs.GetFloat("bestScore");
if (best < time)
{
PlayerPrefs.SetFloat("bestScore", time);
bestScore.text = time.ToString("N2");
}
else
{
bestScore.text = best.ToString("N2");
}
}
else
{
PlayerPrefs.SetFloat("bestScore", time);
bestScore.text = time.ToString("N2");
}
endPanel.SetAcive(true);
}
void TimeStop()
{
Time.timeScale = 0.0f;
}
- 게임 종료 시 현재 점수와 최고 점수를 나타낼 때, 데이터를 보관하는 기능인 PlayerPrefs를 사용한다.
- SetFloat는 float 자료형을 세팅, SetString은 string 자료형을 세팅
- HasKey는 찾아올 키값이 있는지 없는지 확인하는 기능. 있다면 true 없다면 false가 됨
고양이 밥주기 게임
public GameObject food;
void Srart()
{
InvokeRepeating("MakeFood", 0f, 0.5f)
}
void MakeFood()
{
float x = transform.position.x;
float y = transform.position.y + 2.0f;
instantiate(food, new Vector(x, y), Quaternion.identity);
}
- 추가로 instantiate를 써줄 때 어떤 위치에서 생성할지까지 정해줄 수 있음.
- 그래서 x, y를 추가해 줌. Dog 스크립트니까 Dog x, y 기준으로 생성될 것
- Quaternion.identity는 별도의 회전값을 주지 않겠다는 뜻. 회전값 없이 생성한다는 뜻
카드 맞추기 게임
public GameObject card;
void Srart()
{
for(int i = 0; i < 16; i++)
{
instantiate(card, this.transform);
}
}
void Update()
{
}
for(int i = 0; i < 16; i++)
{
GameObject go = instantiate(card, this.transform);
go.transform.position = new Vector2()
}
}
void Update()
{
}
- 카드 16장을 보드에 4x4로 배치하기
- 배치를 위해 instantiate를 go 변수에 담음
- go의 Vector2 (x, y)에는 배치에 필요한 원하는 값을 넣어주면 됨
- for문을 통해서 증가하고 있는 i값을 4로 나눴을 때, 나머지 값을 x, 몫 값을 y에 넣어주면 된다.
for(int i = 0; i < 16; i++)
{
GameObject go = instantiate(card, this.transform);
float x = (i % 4) * 1.4f;
float y = (i / 4) * 1.4f;
go.transform.position = new Vector2(x, y)
}
}
- 1.4f는 카드의 거리. Scale 1.3에 간격 0.1이 추가된 값
int[] arr = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
arr = arr.OrderBy(x => Random.Range(0f, 7f)).ToArray();
- int arr은 정수 배열
- 배열을 OrderBy(정렬)할 건데 어떻게 정렬할 건지 소괄호 안에 조건이 들어감
=> 이 화살표는 해당 배열을 순서대로 한 번씩 순회한다는 뜻. 그걸 랜덤하게 0부터 7까지.
ToArray는 그냥 기존의 배열 Array 자료형으로 다시 맞춰준 것
- 이러면 16장의 카드들이 섞여서 재배열 된다.
int idx = 0;
public void Setting(int number)
{
idx = number;
}
- Setting 함수 안에 int number라는 매개변수를 써서 외부에서 이 함수를 호출할 때 어떤 값을 넣어줄 수 있게 됨
- 첫 번째 생성 카드가 i[7]이라고 하면, 첫 번째로 생성된 카드에다가 Setting 함수에 7을 넣어주고 그 7이 첫 번째 카드의 idx에 들어가게 됨
public GameObject card;
void Start()
{
int[] arr = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
arr = arr.OrderBy(x => Random.Range(0f, 7f)).ToArray();
for(int i = 0; i < 16; i++)
{
GameObject go = instantiate(card, this.transform);
float x = (i % 4) * 1.4f;
float y = (i / 4) * 1.4f;
go.transform.position = new Vector2(x, y)
go.GetComponent<Card>.Setting(arr[i]);
}
}
<Card> 스크립트의 Card 컴포넌트 안에 있는 Setting이라는 함수를 불러오게 하는 것
- 거기에다 배열의 i번째에 있는 데이터를 넘겨준다