综合---专业版Unity技巧分享:使用定制资源配置文件:制作一个对象池

来源:互联网 发布:上海甲子网络 编辑:程序博客网 时间:2024/06/07 03:25

使用定制资源配置文件,可以配置对象池

http://www.360doc.com/content/14/0323/13/12282510_363016017.shtml


制作出来的效果就是和监视面板中的选项是相同的,制作流程:,

制作一个对象池

1.创建一个你要在检视面板设置的数据结构

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class GameObjectPool
{
    [SerializeField]
    public string name;//资源池名称
    [SerializeField]
    private GameObject prefab;//资源池里面的物体
    [SerializeField]
    private int maxCount;//资源池最大数量
    [NonSerialized]
    List<GameObject> goList = new List<GameObject>();//用来保存预制体的一个LISt集合

//这个用来返回一个改池中的对象,
    public GameObject _GetPoolGameObject()
    {
        //如果池中元素小于最大元素的话
        if (goList.Count < maxCount)
        {
            foreach (var item in goList)
            {
                //如果存在不活跃的物体,我们就是用它
                if (!item.activeInHierarchy)
                {
                    //这里不需要将他的可见度调成可见吗
                    item.SetActive(true);
                    return item;
                }
            }
            //遍历所有池子元素都活跃,我们就生成一个并且加入池子
            GameObject go = GameObject.Instantiate(prefab);
            goList.Add(go);
            return go;
        }
        //如果池中元素不大于的话(有一些代码可以省略,但不想省略
        else
        {

//我们将集合中的第一个对象销毁删掉,然后重新实例化一个对象,将他加入到我们的集合中
            GameObject.Destroy(goList[0]);
            goList.RemoveAt(0);
            GameObject go = GameObject.Instantiate(prefab);
            goList.Add(go);
            return go;
        }
    }
}

2.由于我们是要有很多这样的池子,如子弹池,特效池,敌人池等等,所以我们应该用List来保存,继承自SO,这样才可以被数据化

public class GameObjectPoolList : ScriptableObject {
    public List<GameObjectPool> poolList ;
}

3.将PoolLIst数据化,创建到检视面板

public class PoolManagerEditor {
    [MenuItem("Manager/Create GameObjectPoolConfig")]
    static void CreateGameObjectPoolList()
    {
        GameObjectPoolList poolList = ScriptableObject.CreateInstance<GameObjectPoolList>();
        string path = "Assets/Resources/gameobjectpool.asset";
        AssetDatabase.CreateAsset(poolList, path);
        AssetDatabase.SaveAssets();
    }
}

4.用一个对象池管理器来管理所有的对象池,可以通过管理器来得到某个对象池之中的对象,这时我们就可以用这个对象做爱做的事,比如说把这个对象发射出去,蒸炸煮等等

public class PoolManager {
    private static PoolManager instance;
    public static PoolManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new PoolManager();
            }
            return instance;
        }
    }
    private Dictionary<string, GameObjectPool> m_poolDic;
    private PoolManager()
    {
        string path = "gameobjectpool";
        GameObjectPoolList poolList = Resources.Load<GameObjectPoolList>(path);
        m_poolDic = new Dictionary<string, GameObjectPool>();
        foreach (var item in poolList.poolList)
        {
            m_poolDic.Add(item.name, item);
        }
    }
    public void _Init()
    {


    }
    /// <summary>
    /// 得到某个池子的实例
    /// </summary>
    /// <param name="poolName">根据池子的名字</param>
    /// <returns></returns>
    public GameObject _GetPoolGameObject(string poolName)
    {
        GameObjectPool pool;
        if(m_poolDic.TryGetValue(poolName, out pool))//得到这个对象池
        {

//得到某个对象,是某个对象池自身的属性,我们将权利转交给那个对象池,让他来返回我们对象
            return pool._GetPoolGameObject();//得到对象池的一个内部的物体
        }
        else
        {
            Debug.LogError("GameObjectPool" + poolName + "is not exist");
            return null;
        }
    }
}

//5.初始化这个对象,由于是单例模式,很简单啦:

在StrangeIOC中调用:在StartCommand中调用(因为我们要频繁的使用,而且在很多地方都能用到,所以我们不能Inject),直接使用最简单的单利就行

//得5.到一个对象池中的对象

PoolManager.Instance._GetPoolGameObject("Bullet");

阅读全文
0 0
原创粉丝点击