Skip to content

6 ‐ UI Manager

문현우 edited this page Sep 13, 2023 · 5 revisions

📌 UIManager

🎇 Managed by 문현우

  • 게임의 UI를 관리하는 기능을 담당합니다.

📜 Code Summary

  • Awake()

    • instance 필드가 null이면, 이 클래스의 인스턴스를 생성하고, gameObject를 씬에서 제거되지 않도록 설정합니다.
  • LoadScene(string sceneName)

    • 지정된 이름의 씬을 로드하는 메서드입니다.
  • SetGameObjectActive(GameObject gameObject, bool active)

    • 지정된 GameObject의 활성화 여부를 설정하는 메서드입니다.

💻 Code

UIManager
using UnityEngine;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour
{
    public static UIManager instance;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    public void LoadScene(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }

    public void SetGameObjectActive(GameObject gameObject, bool active)
    {
        gameObject.SetActive(active);
    }


}

✔️ CharacterInfoBtn

🎇 Managed by 문현우

  • 캐릭터 정보 버튼을 관리하는 기능을 담당합니다.

📜 Code Summary

  • OnClickedCharacterInfoBtn(int characterIndex)

    • 캐릭터 정보 버튼이 클릭되었을 때 호출되는 메서드입니다.
    • 다른 모든 캐릭터 정보 버튼을 비활성화하고, 선택한 캐릭터 정보 버튼을 활성화합니다.
    • StartGameBtn 클래스의 ReceiveCharacterIndex() 메서드를 호출하여 선택한 캐릭터의 인덱스를 전달합니다.
  • DeactivateAllCharacterInfos()

    • characterInfos 배열에 있는 모든 GameObject를 비활성화합니다.

💻 Code

CharacterInfoBtn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterInfoBtn : MonoBehaviour
{
    public GameObject[] characterInfos;

    public void OnClickedCharacterInfoBtn(int characterIndex)
    {
        DeactivateAllCharacterInfos();
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
        StartGameBtn startGameBtn = FindObjectOfType<StartGameBtn>();
        if (startGameBtn != null)
        {
            startGameBtn.ReceiveCharacterIndex(characterIndex);
        }
        UIManager.instance.SetGameObjectActive(characterInfos[characterIndex], true);
    }

    void DeactivateAllCharacterInfos()
    {
        foreach (GameObject characterInfo in characterInfos)
        {
            UIManager.instance.SetGameObjectActive(characterInfo, false);
        }
    }
}

✔️ StartGameBtn

🎇 Managed by 문현우

  • 게임을 시작하는 버튼을 관리하는 기능을 담당합니다.

📜 Code Summary

  • ReceiveCharacterIndex(int characterIndex)

    • characterIndex 인자로 전달받은 값을 selectedCharacterIndex 필드에 저장합니다.
  • StartGame()

    • 먼저 selectedCharacterIndex 필드의 값이 -1이 아닌지 확인합니다.

    • 만약 -1이 아니라면, PlayerPrefs에 selectedCharacterIndex 필드의 값을 저장합니다.

    • MainScene 씬을 로드합니다.

💻 Code

StartGameBtn.cs
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartGameBtn : MonoBehaviour
{
    private int selectedCharacterIndex = -1;
    public void ReceiveCharacterIndex(int characterIndex)
    {
        selectedCharacterIndex = characterIndex;
    }

    public void StartGame()
    {
        if (selectedCharacterIndex != -1)
        {
            PlayerPrefs.SetInt("SelectedCharacterIndex", selectedCharacterIndex);
            PlayerPrefs.Save();
            SceneManager.LoadScene("MainScene");
        }
    }
}

✔️ RetryBtn

🎇 Managed by 문현우

  • 게임을 다시 시작하는 버튼을 관리하는 기능을 담당합니다.

📜 Code Summary

  • RetryGame()
    • 먼저 AudioManager 클래스의 PlaySfx() 메서드를 호출하여 "Select" 사운드 효과를 재생합니다

    • GameManager 클래스의 StartGame() 메서드를 호출하여 게임을 다시 시작합니다.

    • Time.timeScale 변수를 1로 설정하고, UIManager 클래스의 LoadScene() 메서드를 호출하여 MainScene 씬을 로드합니다.

💻 Code

RetryBtn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RetryBtn : MonoBehaviour
{
    public GameObject retryBtn;
    public void RetryGame()
    {
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
        GameManager.Instance.StartGame();
        Time.timeScale = 1f;
        UIManager.instance.LoadScene("MainScene");
    }
}

Clone this wiki locally