- Popup Service là hệ thống quản lý Popup trong MS 2.0
- Popup Service sẽ sử dụng queue để quản lý thứ tự hiển thị các popup. Chỉ có 1 popup xuất hiện trên màn hình tại 1 thời điểm.
- Cấu trúc interface IPopupService
- Cấu trúc Popup Data
Cách sử dụng
- Setup một Popup
- Tạo Popup Controller
- Class của popup controller phải kế thừa lại PopupBase
- Có thể override lại method DoIntro và DoOutro để thực hiện Animation khi mở và đóng popup nếu cần
- Tạo Popup Prefab
- Kéo Scripts Popup Controller vào ngang hàng với Canvas của Popup
- Gọi Hàm Close để đóng popup
- Kéo Prefab vào Group Popup trong Addressable. Tên của popup trong addressable phải trùng với tên của class Popup Controller
- Tạo Popup Controller
- Push Popup lên màn hình
var popupService = SupportSystem.Instance[SupportSystem.ServiceIDs.PopupService] as IPopupService;
PopupData popupData = new PopupData()
{
//Type của Popup Controller
popupType = typeof(DemoPopup),
// Data truyền vào trong Popup. Data này sẽ lấy được khi Popup chạy vào hàm Start.
data = new Dictionary<string, object>()
{
{"Demo", "Value" }
},
//Callback khi Popup hiện lên trên màn hình
OnPopupPushed = (obj) =>
{
DebugMonkey.Log(() => $"Popup Pushed: {obj.GetType().Name}");
},
//Callback khi đóng Popup
OnPopupClosed = () =>
{
DebugMonkey.Log(() => "Popup Closed");
},
//Override lại sorting của canvas. nếu ko set giá trị thì canvas sẽ giữ nguyên sorting của prefab
order = 100
}
popupService.PushPopup(popupData);
- Editor
trong Inspector của PopupService, có thể xem được Popup hiện tại trên màn hình vào danh sách Popup trong Queue.Popup sẽ hiển thị lần lượt theo danh sách trong Popups In Queue
- Các lỗi có thể gặp phải
- PopupData cant be null => PopupData không thể null
- PopupDemo is not a Popup => Class PopupDemo không được kế thừa PopupBase
- Load prefab popup from Addressable failed => Không load được Popup từ Addressables
- [ERROR] Get Data with Key: {key}. the data type is wrong. cant cast {value.GetType().Name} to {typeof(T).Name} => Key tồn tại trong data nhưng sai value sai định dạng
- [ERROR] Get Data with Key: {key}. Key not found => Key trong data không tồn tại