
오늘은 미니 프로젝트에서 다른 팀원분들이 짠 코드들을 하나하나 들여다보며 공부했다
내가 쓴 코딩들도 다시 보며 이해하고 쓴게 맞는지 복습해보는 시간
애니메이션 쪽이랑... 스테이지 관련.. Ui Manager, GameManager 볼게 많다...!
[HideInInspector]
어제 공부했던 SerializeField 와 반대로 사용하는 친구
public으로 지정하고 Inspector 창에서는 안보이게끔 한다
에디터에서 조작할 일 없는 변수들에게 적용
void Awake
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject):
}
else
{
Destroy(gameObject);
}
}
GameManager 나 UiManager 같이 게임 구동에 필요한 로직들을 한곳에 모았는데 다른 씬으로 넘어가면서 똑같은 매니저가 2개가 있으면 저장한 값에 오류가 발생하니 '이 매니저를 대표로 지정한다!' 라는 느낌으로 하나의 싱글톤을 설정해주고 대표로 지정 안된 오브젝트들은 삭제시킨다
PlayerPrefs // 사용자 로컬에 값을 저장!
int(정수)나 float(실수), string(문자열)을 저장할 수 있다
PlayerPrefs.SetInt(key, value) //key에는 지정할 이름, value에는 정수값
PlayerPrefs.GetInt(key, default) //저장된 정수값을 불러올 때 사용
//Float와 String또한 마찬가지
PlayerPrefs.HasKey(Key) //조건문에서 사용 저장된 값이 있는지 여부 확인
PlayerPrefs.DeleteKey(Key) //특정 키의 저장된 값 삭제
PlayerPrefs.DeleteAll() //저장된 모든 데이터 값 삭제
사운드 값을 로컬에 저장해서 게임을 껐다켰다 할때마다 귀가 터지는 경험을 할 필요가 없어진다거나
스테이지 클리어를 한 상태인지 안한 상태인지 값을 지정해줘서 해금요소들을 추가시킬 수 있다
퍼즐게임이면 앞에있는 스테이지를 클리어 해야지 해금된다거나
로그라이크 게임이라면 플레이어가 어디까지 도달했고 무슨 아이템을 먹었는지에 따라 해금요소를 추가할 수 있겠다 싶다
void Start()
{
float savedBGM = PlayerPrefs.GetFloat("bgmSource", 1f);
float savedSFX = PlayerPrefs.GetFloat("sfxSource", 1f);
bgmSource.volume = savedBGM;
sfxSource.volume = savedSFX;
if (bgmSlider != null) bgmSlider.value = savedBGM;
if (sfxSlider != null) sfxSlider.value = savedSFX;
}
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
bgmSource = BGM.GetComponent<AudioSource>();
sfxSource = SFX.GetComponent<AudioSource>();
if (PlayerPrefs.HasKey("bgmSource"))
{
float savedbgmVolume = PlayerPrefs.GetFloat("bgmSource");
bgmSource.volume = savedbgmvolume;
}
if (PlayerPrefs.HasKey("sfxSource"))
{
float savedsfxVolume = PlayerPrefs.GetFloat("sfxSource");
sfxSource.volume = savedsfxvolume;
}
}
public void SetBGMVolume(float value)
{
bgmSource.volume = value;
PlayerPrefs.SetFloat("bgmSource", value);
}
public void SetSFXVolume(float value)
{
sfxSource.volume = value;
PlayerPrefs.SetFloat("sfxSource", value);
}
그으래서 요번 프로젝트 사운드의 값을 저장하고 다시 불러왔을 때 저장되게끔 만들었다
인터넷 정보를 찾아보며 아 이렇게 사용하는 거구나! 이해하려 노력하며 긁어왔는데 안되더라
으어어 이러면서 뭐가 문제인지 확인하려 했는데 도저히 모르겠어서 팀원분들한테 도움! 요청했더니 PlayerPrefs가 초기화 되는 명령어가 GameManager에 있더라~
그래서 게임 시작시 초기화 하는게 아닌 특정 키를 눌렀을 때 초기화 할 수 있도록 변경해주니 다행이 잘 작동 하더라
게임시간 5초 남았을 때 경고음 발생!
bool warningPlayed = false;
if (!warningPlayed && this.time <= 5.0f)
{
AudioManager.instance.PlayWarning();
warningPlayed = true;
}
if (!warningPlayed && this.time <= 5.0f)
경고음이 실행 안되었다 = 5초 이하다 가 둘다 참 일때 실행
불값을 참으로 변경 함으로 써 경고음이 실행 되었다 로 변경