👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:Unity基础实战
⭐🅰️⭐
文章目录
- ⭐🅰️⭐
- ⭐前言⭐
- 🎶(==1==) 使用简单的List显隐功能
- 🎶(==2==) 升级——使用缓存池小框架
- ⭐🅰️⭐
⭐前言⭐
🎶(1) 使用简单的List显隐功能
1.先全部把资源资源存储到List中,并让其失活
2.然后添加一个显隐方法,需要的时候显示,不需要的时候隐藏
/// <summary>/// 动态加载左侧Item/// </summary>public void AotuLoadLeftItem(){int num = ServerLists.Count / 5 + 1; //分成多少个区服集按钮Itemfor (int i = 1; i <= num; i++){//加载预制体GameObject leftItem = Instantiate(Resources.Load<GameObject>("UI/UIItem/leftItem1"));leftItem.transform.SetParent(LeftServe.content); //固定其父对象LeftItemData serverData = leftItem.GetComponent<LeftItemData>();//设置每个Item显示的区间范围int star, end;star = (i-1) * 5 + 1;end = i * 5; if (i * 5 >= ServerLists.Count ){end = ServerLists.Count;} serverData.ChangeTextShow(star, end ); //更新显示的区间范围文本Button leftItemButton = leftItem.GetComponent<Button>(); //给按钮添加监听,点击后更新右边的区服// AotuLoadRightItem(star, end); //更新对应的区间服务器集AotuLoadRightItem(star, end);//按下才可以激活leftItemButton.onClick.AddListener(() =>{ActiveTrue(star, end);});}}/// <summary>/// 动态加载右侧Item/// </summary>/// <param name="start"></param>/// <param name="end"></param>public void AotuLoadRightItem(int start ,int end){for (int i = start; i <= end; i++){GameObject rightItem = Instantiate(Resources.Load<GameObject>("UI/UIItem/ChooseItem1")); rightItem.transform.SetParent(RightServe.content,false); //固定其父对象// rightItem.transform.localScale = Vector3.one;ChooseItemData serverData = rightItem.GetComponent<ChooseItemData>();serverData.UpdataItemInfo(ServerLists[i-1]); //更新服务器数据serverData.gameObject.SetActive(false);//先全部失活rightItemList.Add(rightItem);//然后存储到List列表中}}/// <summary>/// 激活,(好处减少了Destory的性能消耗)/// </summary>/// <param name="start"></param>/// <param name="end"></param>public void ActiveTrue(int start, int end){for (int i = start; i <= end; i++){rightItemList[i - 1].SetActive(true);}}
🎶(2) 升级——使用缓存池小框架
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;// 抽屉数据 public class PoolData
{public GameObject fatherObj;public List<GameObject> poolList;public PoolData(GameObject obj, GameObject poolObj){fatherObj = new GameObject(obj.name);fatherObj.transform.parent = poolObj.transform;poolList = new List<GameObject>() {};PushObj(obj);}//存public void PushObj(GameObject obj){obj.SetActive(false);poolList.Add(obj);obj.transform.parent = fatherObj.transform;}//取public GameObject GetObj(){GameObject obj = null; obj = poolList[0]; //取第一个poolList.RemoveAt(0); //然后容器中移除obj.SetActive(true);obj.transform.parent = null; //断开了父子关系return obj;}
}//缓存池管理器
public class PoolManager : SingleManager<PoolManager>
{//存储衣柜public Dictionary<string, PoolData> poolDic = new Dictionary<string, PoolData>();private GameObject poolObj;//衣柜里面拿东西public void GetObj(string name, UnityAction<GameObject> callBack){//有抽屉 并且抽屉里有东西if (poolDic.ContainsKey(name) && poolDic[name].poolList.Count > 0){callBack(poolDic[name].GetObj());}else{//通过异步加载资源 创建对象给外部用ResourceManager .GetInstance().LoadAsync<GameObject>(name, (obj2) =>{obj2.name = name;callBack(obj2); //异步加载出的资源不能直接给外部使用所以需要委托});}}//放东西进衣柜public void PushObj(string name, GameObject obj){if (poolObj == null) //防止报错poolObj = new GameObject("Pool");//里面有抽屉if (poolDic.ContainsKey(name)){poolDic[name].PushObj(obj);}else{poolDic.Add(name, new PoolData(obj, poolObj));}}//清空缓存池——在场景切换时public void Clear(){poolDic.Clear();poolObj = null;}
}
⭐🅰️⭐
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、