using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using UnityEngine; namespace DDD { public abstract class SimpleViewModel : MonoBehaviour, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void Awake() { } protected virtual void OnEnable() { } protected virtual void Start() { } protected virtual void OnDisable() { } protected virtual void OnDestroy() { } public virtual void Initialize() { } public virtual void Cleanup() { } /// /// PropertyChanged 이벤트 발생 /// /// 변경된 속성 이름 (자동으로 설정됨) protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// /// 필드 값 변경 및 PropertyChanged 이벤트 발생 /// /// 필드 타입 /// 변경할 필드 참조 /// 새로운 값 /// 속성 이름 (자동으로 설정됨) /// 값이 실제로 변경되었는지 여부 protected bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } /// /// 배치 업데이트를 위한 플래그 /// private bool _isUpdating; /// /// 배치 업데이트 중 보류된 알림들 /// private readonly HashSet _pendingNotifications = new(); /// /// 배치 업데이트 시작 - 여러 속성 변경을 한 번에 처리 /// protected void BeginUpdate() => _isUpdating = true; /// /// 배치 업데이트 종료 - 보류된 모든 알림을 처리 /// protected void EndUpdate() { _isUpdating = false; if (_pendingNotifications.Count > 0) { foreach (var prop in _pendingNotifications) { OnPropertyChanged(prop); } _pendingNotifications.Clear(); } } } }