Unity教学项目Ceator Kit:FPS 源代码学习笔记(三)PoolSystem类(对象池)

using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;

//Simple ring buffer style pool system. You don't need to return the object to the pool, it just get pushed back to the
//end of the queue again. So use it only for object with short lifespan (projectile, particle system that auto disable)
public class PoolSystem : MonoBehaviour
{
    public static PoolSystem Instance { get; private set; }

    public static void Create()//创建一个对象池系统游戏对象
    {
        GameObject obj = new GameObject("PoolSystem");
        Instance = obj.AddComponent<PoolSystem>();
    }

    Dictionary<Object, Queue<Object>> m_Pools = new Dictionary<Object, Queue<Object>>();

    public void InitPool(UnityEngine.Object prefab, int size)//初始化对象池
    {
        if(m_Pools.ContainsKey(prefab))//是否有这个预制体
            return;
        
        Queue<Object> queue = new Queue<Object>();//对象队列

        for (int i = 0; i < size; ++i)//起始设置
        {
            var o = Instantiate(prefab);
            SetActive(o, false);
            queue.Enqueue(o);
        }

        m_Pools[prefab] = queue;//给预制体对应的队列复制
    }

    public T GetInstance<T>(Object prefab) where T:Object//获取预制体实例,T为泛型
    {
        Queue<Object> queue;
        if (m_Pools.TryGetValue(prefab, out queue))//出队
        {
            Object obj;
            
            if (queue.Count > 0)
            {
                obj = queue.Dequeue(); 
            }
            else
            {
                obj = Instantiate(prefab);
            }
            
            SetActive(obj, true);
            queue.Enqueue(obj);
            
            return obj as T;
        }

        UnityEngine.Debug.LogError("No pool was init with this prefab");
        return null;
    }
    
    static void SetActive(Object obj, bool active)//控制物体是否显示
    {
        GameObject go = null;

        if (obj is Component component)
        {
            go = component.gameObject;
        }
        else
        {
            go = obj as GameObject;
        }
            
        go.SetActive(active);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- Added Grenades - Bug Fixes - Adapted to new Photon Updates EZFPS is a multiplayer first person shooter template for Unity3d, using Photon Unity Networking. EZFPS is designed to make a simple FPS Deathmatch style game, with Ai bots, zombies, player classes, weapon loadouts, weapon skins and attachments, and player customization. As the player gets kills, they receive XP points, in-game currency, and rank up- allowing them to buy and unlock new weapons, attachments, skins, and cosmetics. The kit comes fully setup with 5 maps, a zombie-only mode, 9 weapons, and 3 classes, but an infinite amount of maps, weapons, and classes can be added easily by following the included tutorials and documentation. Youtube | Demo | Docs | Tutorials Features: - Player Classes - Synced Bot Ai - Synced Zombie Ai - Custom Loadouts - Player Cosmetic Customization - Weapon Attachments and Skins - Rank System - Weapon Unlocks - Machine Guns, Sniper Rifles, Shotguns, Knifes, Rocket Launchers, Grenade Launchers - Grenades - Settings Menu - More! 谷歌翻译: ——添加手榴弹 ——错误修复 -适应新的光子更新 EZFPS是一个多玩家的Unity3d第一人称射击模板,使用光子统一网络。EZFPS是设计来做一个简单的FPS死亡比赛风格的游戏,与Ai机器人,僵尸,玩家,武器加载,武器皮肤和附件,和玩家定制。当玩家获得技能时,他们将获得经验值、游戏内货币和等级——允许他们购买和解锁新的武器、附件、皮肤和化妆品。这个工具包有5张地图,只有僵尸模式,9件武器和3个职业,但是无限数量的地图,武器,和职业可以很容易地添加遵循所包含的教程和文档。 Youtube |演示|文档|教程 特点: ——玩家 -同步机器人Ai -同步僵尸Ai ——自定义面板 -玩家化妆品定制 -武器附件和皮肤 ——排名系统 ——武器解锁 -机关枪,狙击步枪,猎枪,刀,火箭发射器,手榴弹发射器 ——手榴弹 ——设置菜单 - - - - - -更多!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值