싱글톤 ScoreManager
public class ScoreManager : MonoBehaviour
{
//싱글톤
//어디에서나 접근 할 수 있도록 static(정적)으로 자기자신을 저장해서 싱글톤이라는 디자인패턴을 사용
public static ScoreManager instance;
bool die = true;
void Awake()
{
//정적으로 자신을 체크합니다. nul
//방법1
if(instance == null)
{
//자기자신을 저장
instance = this;
}
/*//방법2
if(!instance)
{
instance = this;
}*/
}
//점수를 증가하고 싶다
//Text Scoretext;
[SerializeField] Text scoreText;
int score = 0;
void Start()
{
}
/* public void AddScore()
{
score++;
text = GameObject.Find("Score").GetComponent<Text>();
text.text = score.ToString();
}*/
public void AddScore(int num)
{
if(die)
{
score += num; //점수를 더해줍니다
scoreText.text = "Score : " + score.ToString();
}
}
public void Die()
{
die = false;
scoreText.text = "게임 종료";
}
}
https://tecoble.techcourse.co.kr/post/2020-11-07-singleton/
싱글톤(Singleton) 패턴이란?
이번 글에서는 디자인 패턴의 종류 중 하나인 싱글톤 패턴에 대해 알아보자. 싱글톤 패턴이 무엇인지, 패턴 구현 시 주의할 점은 무엇인지에 대해 알아보는 것만으로도 많은 도움이 될 것이라
tecoble.techcourse.co.kr
https://glikmakesworld.tistory.com/2
유니티 디자인패턴 - 싱글톤 (Unity Design Patterns - Singleton)
싱글톤 패턴은 초보 개발자들이 가장 많이 쓰는 디자인 패턴이 아닐까 싶다. 클래스 구조를 짜다보면 다른 클래스의 함수를 사용해야 할 수도 있고, 전체 클래스들이 공유하는 전역변수가 필요
glikmakesworld.tistory.com
코루틴
시간제어
invoke~
Time.deltatime
public class CoroutineTest : MonoBehaviour
{
void Start()
{
StartCoroutine("CoName");
}
//코루틴
IEnumerator CoName()
{
/*Debug.Log("안녕하세요");
yield return new WaitForSeconds(1); //1초지연
Debug.Log("1초 지났다");
yield return new WaitForSeconds(1); //1초지연
Debug.Log("나의 멘탈이 부셔진다");
yield return new WaitForSeconds(2); //2초지연
Debug.Log("그래도 열심히 할것이다.");*/
int counter = 0;
while(true)
{
Debug.Log(counter++);
counter++;
yield return new WaitForSeconds(1); //1초 지연
if(counter == 10)
{
// while문일 때 계속 진행되기 때문에 종료시점에 지워줘야한다.
StopCoroutine("CoName");
}
}
}
}
728x90
'유니티' 카테고리의 다른 글
유니티 [Texture , Materiall] (0) | 2023.06.30 |
---|---|
유니티 UI SetActive / enabled (0) | 2023.06.30 |
6/30 C# 클래스 상속 (0) | 2023.06.30 |
6/29 [Invoke, OnBeacmeInvisible(), SoundManger(싱글톤)] (0) | 2023.06.29 |
6/28 특정오브젝트 찾기, 인스턴스화 (0) | 2023.06.28 |