首页 > 其他 > 详细

Unity3d 实用的PoolManager教程

时间:2014-07-13 22:34:07      阅读:812      评论:0      收藏:0      [点我收藏+]

先看Example Scene

bubuko.com,布布扣

 

CreationExample 脚本

/// <description>
///    An example that shows the creation of a pool.
/// </description>
public class CreationExample : MonoBehaviour 
{
    /// <summary>
    /// The prefab to spawn from.
    /// </summary>
    public Transform testPrefab;
    public string poolName = "Creator";
    public int spawnAmount = 50;
    public float spawnInterval = 0.25f;

    private SpawnPool pool;

    /// <summary>
    /// 设置池的基本参数.
    /// </summary>
    private void Start()
    {
        this.pool = PoolManager.Pools.Create(this.poolName);

        // 将这个池设为当前物体的子物体
        this.pool.group.parent = this.transform;

        // 设置位置角度
        this.pool.group.localPosition = new Vector3(1.5f, 0, 0);
        this.pool.group.localRotation = Quaternion.identity;

        
        // 创建一个预制池,不需要预先装载
        // 如果有没特别的需求不需要设置,池会采用默认设置 只调用Spawn()方法即可
        // 下面的设置可以跳过
        PrefabPool prefabPool= new PrefabPool(testPrefab);
        prefabPool.preloadAmount = 5;      // 默认参数 预先加载的预制数量 下面的参数 我会放在下文详细解释
        prefabPool.cullDespawned = true;
        prefabPool.cullAbove = 10;
        prefabPool.cullDelay = 1;
        prefabPool.limitInstances = true;
        prefabPool.limitAmount = 5;
        prefabPool.limitFIFO = true;

        this.pool.CreatePrefabPool(prefabPool);//在这spwan池中 开辟一个预制池

        this.StartCoroutine(Spawner());


        // 键值对的方式存放预制
        // 我们知道在这个 Shapes 池中 ,咱们使用了 Cude 预制
        // 使用相应的Key获取 池中的预制实例  
        Transform cubePrefab   = PoolManager.Pools["Shapes"].prefabs["Cube"];
        Transform cubeinstance = PoolManager.Pools["Shapes"].Spawn(cubePrefab);
        cubeinstance.name = "Cube (Spawned By CreationExample.cs)";      }


    /// <summary>
    /// 每隔spawnInterval秒 就生产一个 预制实例
    /// </summary>
    private IEnumerator Spawner()
    {
        int count = this.spawnAmount;
        Transform inst;
        while (count > 0)
        {
            // 排一条线
            inst = this.pool.Spawn(this.testPrefab, Vector3.zero, Quaternion.identity);
            inst.localPosition = new Vector3(this.spawnAmount - count, 0, 0);

            count--;

            yield return new WaitForSeconds(this.spawnInterval);
        }

        // When done, start despawning
        this.StartCoroutine(Despawner());
    }


    /// <summary>
    /// 每隔spawnInterval秒回收一个相应的实例
    /// </summary>
    private IEnumerator Despawner()
    {
        while (this.pool.Count > 0)
        {
            // 当最后一个实例被Despawn,换句话说移除这个池(队列),这个池会复位
            Transform instance = this.pool[pool.Count - 1];
            this.pool.Despawn(instance);  // Internal count--

            yield return new WaitForSeconds(this.spawnInterval);
        }
    }

}

 

bubuko.com,布布扣

明天继续更新

Unity3d 实用的PoolManager教程,布布扣,bubuko.com

Unity3d 实用的PoolManager教程

原文:http://www.cnblogs.com/Keyle/p/3840490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!