219 lines
5.2 KiB
C#
219 lines
5.2 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created : 2020-05-31
|
|
// Description :
|
|
// Last Modified By : Kimch
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "BoardManager" company="kunpo"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
namespace G
|
|
{
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class BoardManager : MonoBehaviour
|
|
{
|
|
private class BoardInfo<T> where T : MonoBehaviour
|
|
{
|
|
private readonly Dictionary<string, GameObject> _templates = new Dictionary<string, GameObject>();
|
|
private readonly Dictionary<string, Queue<T>> _boardQueues = new Dictionary<string, Queue<T>>();
|
|
|
|
public async void LoadAsync(string key)
|
|
{
|
|
var goList = await AssetProxy.Instance.TryGetAssetsAsync<GameObject>(key, POOL_NAME).Task;
|
|
if (goList != null)
|
|
{
|
|
foreach (var go in goList)
|
|
{
|
|
_templates.Add(go.name, go);
|
|
_boardQueues.Add(go.name, new Queue<T>());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Load(GameObject[] templates)
|
|
{
|
|
if (templates != null)
|
|
{
|
|
foreach (var template in templates)
|
|
{
|
|
_templates.Add(template.name, template);
|
|
_boardQueues.Add(template.name, new Queue<T>());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="parent"></param>
|
|
/// <returns></returns>
|
|
public T Create(string name, Transform parent)
|
|
{
|
|
T result = default;
|
|
if (_boardQueues.TryGetValue(name, out Queue<T> queue))
|
|
{
|
|
if (queue.Count > 0)
|
|
{
|
|
result = queue.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
var go = Object.Instantiate(_templates[name], parent);
|
|
go.name = name;
|
|
result = go.GetComponent<T>();
|
|
if (!result)
|
|
{
|
|
result = go.AddComponent<T>();
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void Recycle(T board)
|
|
{
|
|
if (_boardQueues.TryGetValue(board.name, out Queue<T> queue))
|
|
{
|
|
queue.Enqueue(board);
|
|
}
|
|
}
|
|
}
|
|
|
|
const string POOL_NAME = "boards";
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly BoardInfo<BloodBoard> _bloodBoardInfo = new BoardInfo<BloodBoard>();
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly BoardInfo<DamageBoard> _damageBoardInfo = new BoardInfo<DamageBoard>();
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly BoardInfo<BubbleBoard> _bubbleBoardInfo = new BoardInfo<BubbleBoard>();
|
|
|
|
|
|
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>();
|
|
//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<RectTransform>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="name">template name</param>
|
|
/// <returns></returns>
|
|
public DamageBoard CreateDamageBoard(string name)
|
|
{
|
|
return _damageBoardInfo.Create(name, _damageLayer.transform);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 血条
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public BloodBoard CreateBloodBoard(string name)
|
|
{
|
|
var result = _bloodBoardInfo.Create(name, _bloodLayer);
|
|
result.Init();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
}
|