UI MyPC
UI MyPC의 역할
- Player의 중요한 능력치를 UI에 표시해 준다.
필요한 변수들 선언
using UnityEngine;
using UnityEngine.UI;
public class UI_1_MyPC : MonoBehaviour
{
private static UI_1_MyPC instance = null;
// UI Window
public GameObject UI_W_MyPC = null;
// Detail
public Text AttackText;
public Text AttackSpeedText;
public Text BulletVelocityText;
public Text RangeText;
public Text MoveSpeedText;
public Text Storage;
// Manager
private StatusManager statusManager = null;
private PoolingManager poolingManager = null;
public static UI_1_MyPC Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<UI_1_MyPC>();
if (instance == null)
{
GameObject singletonObject = new GameObject(typeof(UI_1_MyPC).Name);
instance = singletonObject.AddComponent<UI_1_MyPC>();
DontDestroyOnLoad(singletonObject);
}
}
return instance;
}
}
...
}
중요한 역할을 하는 변수들은 다음과 같다.
- instance
- UI_1_MyPC 클래스의 싱글톤 인스턴스를 저장하고 관리하며, 전역적으로 접근 가능한 정적 변수
- Detail List
- UI에 표시되는 텍스트들(~~ Text)
기본 함수(Awake, Start, Update)
using UnityEngine;
using UnityEngine.UI;
public class UI_1_MyPC : MonoBehaviour
{
...
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
// Start is called before the first frame update
void Start()
{
statusManager = StatusManager.Instance;
poolingManager = PoolingManager.Instance;
}
// Update is called once per frame
void Update()
{
}
...
}
Awake()
싱글톤 인스턴스 설정과 씬 전환 시 객체를 유지하게 만들고, 현재 오브젝트가 기존 인스턴스와 다른 경우 파괴하도록 만들어줬다.
Start()
UI에서 보여줄 능력치들을 StatusManager에서 가지고 오고, 공격 속도는 PoolingManager에서 관리하므로 각각의 인스턴스를 가지고 오도록 해줬다.
UI Active 관련 함수
using UnityEngine;
using UnityEngine.UI;
public class UI_1_MyPC : MonoBehaviour
{
...
public void OpenUI()
{
if(UI_W_MyPC != null)
{
UpdateStats();
UI_W_MyPC.SetActive(true);
}
}
public void CloseUI()
{
if (UI_W_MyPC != null)
{
UI_W_MyPC.SetActive(false);
}
}
...
}
UI 매니저에서 각각의 UI 요소들의 Active 상태를 제어하는 OpenUI, CloseUI 함수를 정의해 유지보수성을 높였다.
상태 변화를 제어하는 것을 이 함수들을 사용하는 것으로 한정해 불필요한 활성화/비활성화로 부작용을 방지하도록 만들었다.
Status Update Function
using UnityEngine;
using UnityEngine.UI;
public class UI_1_MyPC : MonoBehaviour
{
...
public void UpdateStats()
{
if (statusManager != null && poolingManager != null)
{
Debug.Log("UpdateStats");
AttackText.text = statusManager.AttackPower.ToString();
AttackSpeedText.text = statusManager.AttackSpeed.ToString();
BulletVelocityText.text = poolingManager.bulletPool.Peek().speed.ToString();
RangeText.text = statusManager.AngleRange.ToString();
MoveSpeedText.text = statusManager.MoveSpeed.ToString();
Storage.text = statusManager.CurrentStorage.ToString();
}
else
{
Debug.LogError("UpdateStats");
}
}
}
이 함수는 UI의 Text를 업데이트해 주는 부분이다.
UI를 Open 하는 경우 이 함수를 통해 값을 최신화해 준다.
참고 사항
UI Manager
[Unity] UI Manager 구현
UI ManagerUI Manager 기본정보큰 UI의 틀이 있고 버튼을 통해 총 7개의 화면 전환이 있어야 한다.기본 정보를 보여주는 UI게임 기능인 프로그램의 정보를 띄워주는 UI인벤토리처럼 획득한 아이템 리스
gdoo.tistory.com
마무리하며..
간단하게 수치 값을 연동하고 텍스트로 업데이트해 주는 기능이 있는 UI라 크게 어려운 점은 없었다.
'Unity > Hackers Window' 카테고리의 다른 글
[Unity] UI 3 : MyDocument(Inventory UI) (0) | 2024.11.22 |
---|---|
[Unity] UI 3 : DownLoad (0) | 2024.11.21 |
[Unity] UI 1 : HUD (0) | 2024.11.19 |
[Unity] UI Manager 구현 (0) | 2024.11.18 |
[Unity] Monster 구현 : M_VE - 2 (1) | 2024.11.17 |