unity 对象池 存储不同预制体

对象池效果图在这里插入图片描述
思路:
听课查询资料都是存储单一物体,突发奇想看看是否能存储不同的物体
分为三个脚本

using System.Collections.Generic;
using UnityEngine;

public class Pool
{
    //单例
    static Pool _instance;
    public static  Pool Instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = new Pool();
            }
            return _instance;
        }
    }

    //池子存放的物体
    GameObject obj1;
    GameObject obj2;
    GameObject obj3;

    List<GameObject> pool;

    private Pool()
    {
        //创建list
        pool = new List<GameObject>();
        //动态获取预制体
        obj1 = Resources.Load<GameObject>("Prefabs/prop1");
        obj2 = Resources.Load<GameObject>("Prefabs/prop2");
        obj3 = Resources.Load<GameObject>("Prefabs/prop3");
    }

    public GameObject GetObject()
    {
        GameObject obj;
        //随机生成
        int i = Random.Range(1, 90);
        if (pool.Count == 0)
        {
            if (i < 30)
            {
                //创建一个预制体
                obj = GameObject.Instantiate(obj1);
                //将预制体设置为非激活
                obj.SetActive(false);
                //给预制体添加组件
                obj.AddComponent<Prop>();
                //向list加入这个预制体
                pool.Add(obj);

            }
            else if (i < 60)
            {
                //创建一个预制体
                obj = GameObject.Instantiate(obj2);
                //将预制体设置为非激活
                obj.SetActive(false);
                //给预制体添加组件
                obj.AddComponent<Prop>();
                //向list加入这个预制体
                pool.Add(obj);
            }
            else
            {
                //创建一个预制体
                obj = GameObject.Instantiate(obj3);
                //将预制体设置为非激活
                obj.SetActive(false);
                //给预制体添加组件
                obj.AddComponent<Prop>();
                //向list加入这个预制体
                pool.Add(obj);
            }
        }
        //如果有预制体
        //每次取随机
        obj = pool[Random.Range(0,pool.Count)];
        //激活
        obj.SetActive(true);
        //并且从pool中移除
        pool.RemoveAt(0);
        return obj;
    }

    //回收
    public void Recycle(GameObject obj)
    {
        //设置为非激活
        obj.SetActive(false);
        //加入pool
        pool.Add(obj);
    }
}

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

public class Prop : MonoBehaviour
{
	//协程
    private void OnEnable()
    {
        StartCoroutine(Recycle());
    }

    void Update()
    {
    	//物体移动
        transform.Translate(Vector3.forward * 10 * Time.deltaTime);
    }


    IEnumerator Recycle()
    {
    	//一秒后回收物体
        yield return new WaitForSeconds(1f);
        Pool.Instance.Recycle(gameObject);
    }

}

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

//这个脚本挂在空物体上
public class CreateProp : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //按空格产生子弹
            GameObject prop = Pool.Instance.GetObject();
            //初始化位置
            prop.transform.position = transform.position;
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值