forked from zxl/LaboratoryProtection
66 lines
1.6 KiB
C#
66 lines
1.6 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Cysharp.Threading.Tasks.Triggers;
|
|
|
|
using MessagePipe;
|
|
|
|
using PMaker.DependencyInjection;
|
|
using PMaker.MessagePipe;
|
|
using PMaker.UI;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UniRx;
|
|
|
|
using UnityEngine;
|
|
|
|
public class PageController : SingletonMonobehaviour
|
|
{
|
|
[SerializeField]
|
|
private Page[] _pages;
|
|
|
|
[SerializeField]
|
|
private Transform _uiRoot;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private Dictionary<int, GameObject> _pool;
|
|
|
|
private void Reset()
|
|
{
|
|
_pages = GameObject.FindObjectsOfType<Page>();
|
|
_uiRoot = GameObject.Find("Pages").transform;
|
|
}
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
foreach (var item in _pages)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
_pool = new Dictionary<int, GameObject>();
|
|
MessageKit.GetAsyncSubscriber<string, int>()
|
|
.Subscribe("Reload", async (_, token) => {
|
|
var prefab = _pages[_];
|
|
var page = Instantiate(prefab.gameObject, _uiRoot);
|
|
page.SetActive(true);
|
|
if (_pool.TryGetValue(_, out var last) == true)
|
|
{
|
|
DestroyImmediate(last);
|
|
_pool.Remove(_);
|
|
}
|
|
_pool.Add(_, page);
|
|
await page.StartAsync();
|
|
Debug.Log(GetPage(0));
|
|
})
|
|
.AddTo(this);
|
|
}
|
|
|
|
public Page GetPage(int index)
|
|
{
|
|
return _pool[index].GetComponent<Page>();
|
|
}
|
|
}
|