• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
白染
博客园    首页    新随笔    联系   管理    订阅  订阅

对象池

我对 ---对象池的理解为:

在大型游戏中,往往有些物体需要不断的销毁,不断的生成,这样就很消耗资源。所以对象池技术就应运而生了。

比喻成一个池子,当不在需要时,本该销毁的物体,放回对象池中,使其失活,当再次需要使用时,再次从对象池中取出,让其激活。

当对象池中的物体全部取出,外界还需要时,在外界重新生成物体,使用完之后放入对象池。

对象池很有效的节省了电脑资源!

(个人观点,不喜勿喷)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pool {//对象池
    private static Pool instance;//实例化自己
    private Pool() { }
    public static Pool Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Pool();
            }
            return instance;
        }
    }
    Dictionary<string, List<GameObject>> dic = new Dictionary<string, List<GameObject>>();////创建一个字典dic 键为子弹或敌人种类 值为数组 存放多个同样的子弹或敌人
    public GameObject Getobj(string name)//可以加生成位置和角度--(string name,Vector3 position,Quaternion rotation)
    {
        GameObject go = null;//创建返回值对象
        if (dic.ContainsKey(name))//如果字典里有name这个key
        {
            List<GameObject> list = dic[name];//从这个key位置取出
            if (list.Count > 0)//key对应的数组不为空
            {
                go = list[0];//取出一号位的子弹
                go.SetActive(true);//使用时激活
                list.RemoveAt(0);//从列表中去除这个子弹(拿出来用)
                // go.transform.position = position;
                //go.transform.rotation = rotation;
                
            }
        }
        if (go == null)//没有name名字的,或者list为0时
        {
            go = GameObject.Instantiate(Resources.Load(name)) as GameObject;//在指定地方生成给定key的预设体的gameobject

            int index = go.name.IndexOf("(Clone)");
            go.name = go.name.Substring(0, index);//重新给名字
        }
        return go;//返回创建的东西
    }
    public void Saveobj(GameObject obj)// 参数是需要取消激活的对象g 
    //将需要取消激活的对象取消激活
    {
        obj.SetActive(false);//失活
        if (dic.ContainsKey(obj.name))//如果这个字典里有这个key
        {
            dic[obj.name].Add(obj); //就在这个key所对应的列表中加入这个g  (这个g就是已经用完的子弹,放到这个列表里的gameobjet都是不销毁只是取消激活等待再次利用的gameobject)
        }
        else//如果没有这个key
        {
            List<GameObject> list = new List<GameObject>();//建立一个这个key的列表并把g放进去
            list.Add(obj);
            dic[obj.name] = list;
        }
        obj.transform.parent = GameObject.Find("Pool").transform;//给他们找一个父类存放
    }
}

比如:
生成cube和sphere

生成cube和sphere:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetGameobj : MonoBehaviour {
    string[] s = { "Cube", "Sphere" };
    // Use this for initialization
    void Start () {
        
    }
    public void Get()
    {
        GameObject go = Pool.Instance.Getobj(s[Random.Range(0, 2)]);//从对象池中实例化出来
        go.transform.position = new Vector3(0, 5, 0);
    }
    // Update is called once per frame
    void Update () {
        
    }
}

 

object的失活:

1.cube和sphere:调用了pool中的方法 (其实并不是销毁 是取消激活)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Saveobj : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (transform.position.y < -5)//这个脚本挂在cube和sphere上,当y小于-5时自动“销毁”,进入不激活状态
        {
            Pool.Instance.Saveobj(this.gameObject);
        }
    }
}
莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
posted @ 2018-12-04 10:55  白染  阅读(270)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3