// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : 2020-05-31 // Description : // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** namespace G { using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class BoardManager : MonoBehaviour { private class BoardInfo where T : MonoBehaviour { private readonly Dictionary _templates = new Dictionary(); private readonly Dictionary> _boardQueues = new Dictionary>(); public async void LoadAsync(string key) { var goList = await AssetProxy.Instance.TryGetAssetsAsync(key, POOL_NAME).Task; if (goList != null) { foreach (var go in goList) { _templates.Add(go.name, go); _boardQueues.Add(go.name, new Queue()); } } } public void Load(GameObject[] templates) { if (templates != null) { foreach (var template in templates) { _templates.Add(template.name, template); _boardQueues.Add(template.name, new Queue()); } } } /// /// /// /// /// /// public T Create(string name, Transform parent) { T result = default; if (_boardQueues.TryGetValue(name, out Queue queue)) { if (queue.Count > 0) { result = queue.Dequeue(); } else { var go = Object.Instantiate(_templates[name], parent); go.name = name; result = go.GetComponent(); if (!result) { result = go.AddComponent(); } } } return result; } public void Recycle(T board) { if (_boardQueues.TryGetValue(board.name, out Queue queue)) { queue.Enqueue(board); } } } const string POOL_NAME = "boards"; /// /// /// private readonly BoardInfo _bloodBoardInfo = new BoardInfo(); /// /// /// private readonly BoardInfo _damageBoardInfo = new BoardInfo(); /// /// /// private readonly BoardInfo _bubbleBoardInfo = new BoardInfo(); public GameObject[] bloodBoardTemplates; public GameObject[] damageBoardTemplates; public GameObject[] bubbleBoardTemplates; private Transform _damageLayer; private Transform _bloodLayer; private Transform _bubbleLayer; static BoardManager _Instance; public static BoardManager Instance { get { return _Instance; } } public static Vector2 ScreenPointToLocalPointInRectangle(Vector2 screenPoint) { return KUIRoot.ScreenPointToLocalPointInRectangle(screenPoint); } private void Awake() { _Instance = this; _damageLayer = CreateSubRoot("DamageLayer", 20); _bloodLayer = CreateSubRoot("BloodLayer", 40); _bubbleLayer = CreateSubRoot("BubbleLayer", 50); _bloodBoardInfo.Load(bloodBoardTemplates); _damageBoardInfo.Load(damageBoardTemplates); _bubbleBoardInfo.Load(bubbleBoardTemplates); } private void Start() { //var canvas = GetComponent(); //canvas.worldCamera = KUIRoot.RootCamera; //AssetProxy.Instance.CreateReleasePool(POOL_NAME); //_bloodBoardInfo.LoadAsync("board_blood"); //_damageBoardInfo.LoadAsync("board_damage"); //_bubbleBoardInfo.LoadAsync("board_bubble"); } private void OnDestroy() { //AssetProxy.Instance.RemoveReleasePool(POOL_NAME); } private Transform CreateSubRoot(string name, int sort) { var go = new GameObject(name) { layer = this.gameObject.layer }; var rectT = go.AddComponent(); rectT.SetParent(this.transform, false); rectT.localPosition = new Vector3(0, 0, -sort); rectT.anchorMin = Vector2.zero; rectT.anchorMax = Vector2.one; rectT.offsetMax = Vector2.zero; rectT.offsetMin = Vector2.zero; return go.transform; } /// /// /// /// template name /// public DamageBoard CreateDamageBoard(string name) { return _damageBoardInfo.Create(name, _damageLayer.transform); } /// /// 血条 /// /// public BloodBoard CreateBloodBoard(string name) { var result = _bloodBoardInfo.Create(name, _bloodLayer); result.Init(); return result; } /// /// /// /// public BubbleBoard CreateBubbleBoard(string name) { return _bubbleBoardInfo.Create(name, _bubbleLayer); } public void ReturnBoard(DamageBoard board) { board.Reset(); _damageBoardInfo.Recycle(board); } public void ReturnBoard(BubbleBoard board) { board.Reset(); _bubbleBoardInfo.Recycle(board); } public void ReturnBoard(BloodBoard board) { board.Reset(); _bloodBoardInfo.Recycle(board); } } }