Unit 5 - User Interface - Unity Learn
In this Unit, you will program a game to test the player’s reflexes, where the goal is to click and destroy objects randomly tossed in the air before they can fall off the screen. In creating this prototype, you will learn how to implement a User Interfa
learn.unity.com
Destroy target with click and sensor
타깃을 클릭하거나 화면 아래로 떨어지면 gameObject가 사라지도록 구현한다.
Unity에서 구현한 메서드 OnMouseDown, OnTriggerEnter를 Override한다.
//OnMouseDown은 유저의 마우스가 오브젝트의 Collider에서 클릭하면 호출된다.
private void OnMouseDown()
{
Destroy(gameObject);
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
Add Score text position it on screen
Hierarchy에서 Create > UI > Text - TextMeshPro를 클릭하여 Import TMP Essentials를 한다.
inspector창에서 Anchors를 설정하면 하면 스케일을 변경해도 텍스트 위치를 일정하게 만들 수 있다.
Add a Particle explosion
ParticleSystem 클래스를 활용하여 클릭하여 타깃이 사라질 때 폭발하는 효과를 준다.
public ParticleSystem explosionParticle;
private void OnMouseDown()
{
Destroy(gameObject);
Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
gameManager.UpdateScore(pointValue);
}
Stop spawning and score on GameOver
Coroutine을 도는 쓰레드의 while 문 조건을 isGameAcive로 설정하였을 때,
이 변수를 원하는 값으로 먼저 변경하고 Coroutine을 실행해야한다.
Coroutine을 실행하고 변수를 변경해도 별도의 쓰레드에서 영향을 받지 않는다.
void Start()
{
//먼저 변경
isGameActive = true;
//Coroutine 실행
StartCoroutine(SpawnTarget());
score = 0;
UpdateScore(0);
}
IEnumerator SpawnTarget()
{
while(isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}
Make the restart button work
Scene을 다시 로드하여 재시작하는 Unity SceneManagement 기능 활용
using UnityEngine.SceneManagement;
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
버튼의 On Clicke event에 SceneManager의 LoadScene을 실행하는 RestartGame 메서드를 연결한다.
Call SetDifficulty on button click
버튼의 onClick 이벤트에 리스너를 더해서 SetDifficulty가 이벤트핸들러로 호출되도록 설정한다.
void Start()
{
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
}
https://github.com/Myoungmin/UnityLearn-Create_with_Code-Prototype_5
GitHub - Myoungmin/UnityLearn-Create_with_Code-Prototype_5: Unit 5 - User Interface
Unit 5 - User Interface. Contribute to Myoungmin/UnityLearn-Create_with_Code-Prototype_5 development by creating an account on GitHub.
github.com
'Unity > Unity Learn' 카테고리의 다른 글
Junior Programmer > Next Steps : Project Optimization, Research and Troubleshooting, Sharing your Projects (0) | 2022.06.19 |
---|---|
Junior Programmer > Unit 4 - Gameplay Mechanics (0) | 2022.06.15 |
Junior Programmer > Unit 3 - Sound and Effects (0) | 2022.06.12 |
Junior Programmer > Unit 2 - Basic Gameplay (0) | 2022.06.04 |
Junior Programmer > Unit 1 - Player Control (0) | 2022.06.01 |