using System.Collections; using System.Collections.Generic; using UnityEngine; using Assets.Script.nn; /// <summary> /// 卡牌池 /// </summary> public class CardPool:MonoBehaviour { static Stack<GameObject> objectPool = new Stack<GameObject>(); /// <summary> /// 清空池子 /// </summary> public static void Clear() { objectPool.Clear(); foreach(GameObject go in objectPool) { Destroy(go); } } /// <summary> /// 取 /// </summary> /// <returns></returns> public static GameObject Pop() { if (objectPool.Count == 0) { GameObject newcard = Instantiate(Resources.Load<GameObject>("nn/3DCard/Card")); objectPool.Push(newcard); } GameObject card = objectPool.Pop(); card.SetActive(true); card.transform.position = Vector3.zero; return card; } /// <summary> /// 存 /// </summary> /// <param name="obj"></param> public static void Release(GameObject obj) { if (objectPool.Contains(obj)) throw new System.Exception("对象已经放入池内!"); objectPool.Push(obj); obj.GetComponent<Assets.Script.nn.Card>().ResetCardState(); obj.SetActive(false); obj.transform.localScale = new Vector3(0.7f,0.7f,0.7f); } }