"게임 로직은 건들지 않고 UI만 갈아끼우는 설계"
using TMPro;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int hp = 100;
public TextMeshProUGUI hpText; // UI와 강한 결합
public void OnDamge(int damage)
{
hp -= damage;
// 로직 스크립트 안에서 UI를 건든다..? -> UI와 강한 결합
hpText.text = "HP:" + hp.ToString();
if(hp <= 0)
{
Debug.Log("Enemy 죽음..");
}
}
}

[Model - 데이터 관리]
public class PlayerModel_MVC
{
public int hp = 100;
}
[View - 화면에 출력 - Model 직접 참조중]
using UnityEngine;
using UnityEngine.UI;
public class PlayerView_MVC : MonoBehaviour
{
public PlayerModel_MVC playerModel;
public Slider hpSlider;
public void UpdateView()
{
hpSlider.value = playerModel.hp / 100f;
}
}
[Controller - 입력 처리 및 Model(데이터) 수정]
using UnityEngine;
public class PlayerController_MVC : MonoBehaviour
{
PlayerModel_MVC playerModel = new PlayerModel_MVC();
public PlayerView_MVC playerView;
public void OnDamage(int damage)
{
playerModel.hp -= damage; // Model(데이터) 수정
playerView.UpdateView(); // View(UI - Slider) 갱신
}
}
[Model 파트 MVC와 동일]
[View - 오로지 그려주기만 한다]
using UnityEngine;
using UnityEngine.UI;
public class PlayerView_MVP : MonoBehaviour
{
[SerializeField] Slider slider;
public void SetFillAmount(float ratio)
{
slider.value = ratio;
}
}
// Model을 직접 참조하고 있지 않다
// 데이터 가공도 하지 않는다
// 오로지 그려주기만 한다
[Presenter - Model(데이터) 와 View(화면) 중재]
using UnityEngine;
public class PlayerPresenter_MVP : MonoBehaviour
{
PlayerModel_MVC model = new PlayerModel_MVC();
[SerializeField] PlayerView_MVP playerView; // 조립할 녀석
public void TakeDamge(int damage)
{
model.hp -= damage;
float ratio = model.hp / 100f;
playerView.SetFillAmount(ratio);
}
}
Model
- 데이터 관리 (값만 가짐, 순수 C#)
- MonoBehaviour 상속받지 않음
View
- 그리기만 하는 녀석 -> UI 컴포넌트 수정/갱신
- MonoBehaviour 상속받음
Presenter
- Model(데이터)의 변화를 감지해서 View(화면)에 전달/명령내리는 녀석
Model : 어떤 데이터를 관리하지? (선언된 데이터)
View : 화면에 뭘 그릴건데?
Presenter : Model(데이터) 업데이트와 그려줄 View(화면)를 누가 중재/관리하니?
[model]
public class StatModel
{
public float hp = 100f;
public float maxHP = 100f;
public float GetHpRatio() => hp / maxHP;
}
[View]
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class SliderBarView : MonoBehaviour
{
public Slider slider;
public void SetValue(float ratio) => slider.value = ratio;
}
public class TextPercentView : MonoBehaviour
{
public TMP_Text hpText;
public void SetProgress(float ratio)
{
hpText.text = $"HP; {ratio * 100}%";
}
}
[Presenter]
using UnityEngine;
public class StastPresenter : MonoBehaviour
{
StatModel model = new StatModel();
[SerializeField] SliderBarView viewA;
[SerializeField] TextPercentView viewB;
public void OnDamage(float damage)
{
model.hp -= damage;
viewA.SetValue(model.GetHpRatio());
viewB.SetProgress(model.GetHpRatio());
}
}
[Model]
public class ScoreModel
{
int score;
public event System.Action<int> OnScoreChanged; // 전광판
public void AddScore(int amount)
{
score += amount;
OnScoreChanged?.Invoke(score);
}
}
[Presenter]
using UnityEngine;
public class ScorePresenter : MonoBehaviour
{
[SerializeField] ScoreView scoreView;
[SerializeField] TargetView targetView;
[SerializeField] RankingView rankingView;
[SerializeField] AchievementPopupView popupView;
ScoreModel model = new ScoreModel();
void OnEnable()
{
model.OnScoreChanged += scoreView.UpdateScoreText;
model.OnScoreChanged += targetView.UpdateTargetText;
model.OnScoreChanged += CheckAchievement; // Q. 왜 View쪽이 아닌 여기에 구현할까?
}
void OnDisable()
{
// 구독 해지 안해준다면??
}
void CheckAchievement(int score)
{
if(score >= 100) popupView.Show("팝업 떳습니다");
}
}
OnEnable (구독 시작)
OnDisable (구독 해제)
👉🏻 메모리 누수/죽은 참조 호출 방지 핵심,
밥먹듯이 습관적으로 해주기
MVC: 프로젝트가 커지면, View와 Model이 꼬이는(참조가 많이 일어나는) 스파게티 위험이 있다.
MVP: Presenter가 Model과 View를 중재해서 깔끔하게 관리
👉🏻 대규모 프로젝트로 가도 문제가 발생할 확률이 적다.