/// <summary> /// 一个实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parent">父对象</param> /// <param name="args">参数</param> public T GenerateEntity <T>(Transform parent, string path, params object[] args) where T : MonoBehaviour, Entity { if (parent == null) { return(default(T)); } string typeName = typeof(T).ToString(); //string path = ConstantVal.GetPanelPath(typeName);//mao 获取panel路径 // GameObject plobj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(path), parent); ObjectPoolSingle singleType = (ObjectPoolSingle)Enum.Parse(typeof(ObjectPoolSingle), typeName); GameObject plobj = ObjectPoolManager.Instance.GetObjcectFromPool(singleType, path, true); plobj.transform.SetParent(parent, false); plobj.name = typeName; T t = plobj.GetComponent <T>(); if (null == t) { t = plobj.AddComponent <T>(); } t.objType = singleType; t.isTmpObj = true; t.obj = plobj; t.Init(args); return(t); }
/// <summary> /// 直接获取面板 最好是用事件系统 /// </summary> /// <param name="entity"></param> /// <returns></returns> public PanelBase GetPanel(ObjectPoolSingle singleType) { if (panelDic.ContainsKey(singleType)) { return(panelDic[singleType]); } return(null); }
public Entity GenerateEntity(ObjectPoolSingle single) { GameObject obj = ObjectPoolManager.Instance.GetObjcectFromPool(single, single.ToString(), true); Entity entity = obj.GetComponent <Entity>(); entity.obj = obj; return(obj.GetComponent <Entity>()); }
/// <summary> /// 这里加参数 /// </summary> void CheckPool(ObjectPoolSingle type) { if (EnumIndexObjectDic.ContainsKey(type)) { if (EnumIndexObjectDic[type].Count > objCount) { var obj = EnumIndexObjectDic[type][0]; EnumIndexObjectDic[type].RemoveAt(0); des_Queue[type].Add(obj); } } }
/// <summary> /// 关闭对象,返还给对象池默认可以存5个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ctrl"></param> /// <param name="objCount"></param> public void DisappearObjectToPool(ObjectPoolSingle type, GameObject obj, bool isTempObj) { long storeTime = CGameTime.Instance.GetTimeStamp(); if (!isTempObj) { if (!EnumIndexObjectDic.ContainsKey(type)) { EnumIndexObjectDic.Add(type, new List <GameObject>()); des_Queue.Add(type, new List <GameObject>()); } if (obj == null) { Debug.LogError("exist null pool obj!"); } //对象返回池子 obj.SetActive(false); obj.transform.SetParent(this.transform, false); //防止点快了 bool canAddToPool = true; foreach (GameObject existObj in EnumIndexObjectDic[type]) { if (existObj.GetInstanceID() == obj.GetInstanceID()) { canAddToPool = false; break; } } if (canAddToPool) { EnumIndexObjectDic[type].Add(obj); } } else { if (!tempEnumIndexObjectDic.ContainsKey(type)) { tempEnumIndexObjectDic.Add(type, new List <TempItem>()); tempDes_Queue.Add(type, new List <TempItem>()); } if (obj == null) { Debug.LogError("exist null pool obj!"); } //对象返回池子 obj.SetActive(false); obj.transform.SetParent(this.transform, false); //防止点快了 bool canAddToPool = true; foreach (TempItem existTemp in tempEnumIndexObjectDic[type]) { if (existTemp.obj.GetInstanceID() == obj.GetInstanceID()) { canAddToPool = false; break; } } if (canAddToPool) { TempItem tempItem = new TempItem(); tempItem.obj = obj; tempItem.storeTime = storeTime; tempEnumIndexObjectDic[type].Add(tempItem); } } }
/// <summary> /// 从对象池里面拿东西 枚举 物体名(路径) 是否临时对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objectName">物体的全路径</param> /// <returns></returns> public GameObject GetObjcectFromPool(ObjectPoolSingle objType, string objectName, bool isTemp) { object obj = null; GameObject gameObj = null; if (!isTemp) { if (EnumIndexObjectDic.ContainsKey(objType)) { if (EnumIndexObjectDic[objType].Count == 0) { if (Resources.Load(objectName) == null) { Debug.LogError(string.Format("找不到名字为{0}的物体", objectName)); return(null); } obj = GameObject.Instantiate(Resources.Load(objectName)); } if (EnumIndexObjectDic[objType].Count > 0) { //拿走该对象 obj = EnumIndexObjectDic[objType][0]; EnumIndexObjectDic[objType].RemoveAt(0); } } else { //先从回收站取 if (des_Queue.ContainsKey(objType) && des_Queue[objType].Count > 0) { int index = des_Queue[objType].Count; obj = des_Queue[objType][index - 1]; } else { if (Resources.Load(objectName) == null) { Debug.LogError(string.Format("找不到名字为{0}的物体", objectName)); return(null); } obj = GameObject.Instantiate(Resources.Load(objectName)); } } } else { if (tempEnumIndexObjectDic.ContainsKey(objType)) { if (tempEnumIndexObjectDic[objType].Count == 0) { obj = GameObject.Instantiate(Resources.Load(objectName)); } if (tempEnumIndexObjectDic[objType].Count > 0) { //拿走该对象 obj = tempEnumIndexObjectDic[objType][0].obj; tempEnumIndexObjectDic[objType].RemoveAt(0); } } else { //先从回收站取 if (tempDes_Queue.ContainsKey(objType) && tempDes_Queue[objType].Count > 0) { int index = tempDes_Queue[objType].Count; obj = tempDes_Queue[objType][index - 1].obj; } else { obj = GameObject.Instantiate(Resources.Load(objectName)); } } } gameObj = obj as GameObject; //GameObject gameObj = obj as GameObject; if (gameObj == null) { gameObj = GameObject.Instantiate(Resources.Load(objectName)) as GameObject; } gameObj.SetActive(true); return(gameObj); }