美文网首页
自己实现一个对象池

自己实现一个对象池

作者: HMY轩园 | 来源:发表于2017-05-17 10:12 被阅读0次
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameObjectPool : MonoBehaviour {

    List<GameObject> pools = new List<GameObject>();//创建一个放object的List工具
    private GameObjectPool() { }
    private static GameObjectPool instance;

    public static GameObjectPool Instance()
    {
        if (instance==null)
        {
            instance = new GameObject("GameObjectPool").AddComponent<GameObjectPool>();
        }

        return instance;
    }

    /// <summary>
    /// 需要object在场景里显示时
    /// </summary>
    /// <param obj="obj"></param>
    /// <returns></returns>
    public GameObject MyInstantiate(GameObject  obj) {
        //如果对象池为空,则实例化一个并返回
        if (pools.Count == 0)
        {
            return Instantiate(obj, Vector3.zero, Quaternion.identity) as GameObject;
        }
        //对象池不为空 拿出索引为0的那个 并让其显示且移除对象池
        else
        {
            GameObject obj2 = pools[0];//拿出索引为0的那个
            obj2.SetActive(true);//让其显示
            pools.Remove(obj2);//移除对象池
            return obj2;//返回
        }
    }
    /// <summary>
    /// 对象不需要时 隐藏且放到对象池里
    /// </summary>
    /// <param obj="obj"></param>
    public void MyDisable(GameObject obj) {
        obj.SetActive(false);
        pools.Add(obj);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    public GameObject prefab;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            GameObjectPool.Instance().MyInstantiate(prefab);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {
    void OnEnable()
    {

        transform.position = Vector3.zero;

        StartCoroutine(DelayDisable(3));
    }
        // Use this for initialization
        void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.forward * Time.deltaTime * 20);
    }

    IEnumerator DelayDisable(float time) {
        yield return new WaitForSeconds(time);
        GameObjectPool.Instance().MyDisable(gameObject);
    }
    void OnDisable()
    {
        Debug.Log("自己消失掉");
    }

}

相关文章

  • 自己实现一个对象池

  • netty-Buffer轻量级对象池实现分析

    netty是使用threadlocal变量来实现轻量级的对象池的,每个线程都拥有自己的对象池。netty自己实...

  • Netty对象池

    在平时工作中,听说和使用过连接池,线程池等.还有一种就是对象池,可以实现对象复用的功能. 当然实现对象池的方式手段...

  • Unity 学习笔记

    Unity 对象池实现方案:

  • 游戏框架与组件(1):对象池

    本文介绍了对象池以及对象池的代码实现。 本文链接游戏框架与组件(1):对象池[https://www.jiansh...

  • kotlin版对象复用池

    项目中经常用到对象池复用,但是不一样的对象对应的复用池都不统一。想着能否有一个对象池,可以不用关心对象是啥,实现对...

  • common pool分析

    项目地址 项目介绍: Apache的Commons Pool库提供了一个对象池化API和大量的对象池实现。 与1....

  • commons-pool2 池化技术探究

    一、前言 我们经常会接触各种池化的技术或者概念,包括对象池、连接池、线程池等,池化技术最大的好处就是实现对象的重复...

  • 源码|从串行线程封闭到对象池、线程池

    今天讲一个牛逼而实用的概念,串行线程封闭。对象池是串行线程封闭的典型应用场景;线程池糅合了对象池技术,但核心实现不...

  • Netty之Recycler

    Recycler用来实现对象池,其中对应堆内存和直接内存的池化实现分别是PooledHeapByteBuf和Poo...

网友评论

      本文标题:自己实现一个对象池

      本文链接:https://www.haomeiwen.com/subject/mkxdxxtx.html