Unity对象池试水

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity对象池试水相关的知识,希望对你有一定的参考价值。

  其实接触对象池这个概念已经很久了,网上也有很多现成的插件,比较有名的是PoolManager,功能听说很完善,我并没有细看。现在我想根据网上的一些案例和教程写一个应用于现在这个项目的小ObjectPool,这里有一篇文章对ObjectPool有一定的解释,有兴趣的可以看看。我项目里面有几十个反复要使用的模型,就在这一块下手吧,主要来实现存取两块代码,为了便于检索采用List<T>来实现存取。

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

public class ObjectPool : MonoBehaviour
{
    //// Use this for initialization
    private volatile static ObjectPool _instance = null;
    private Dictionary<int, GameObject> myObjectPool = new Dictionary<int, GameObject>();
    private Action<GameObject> myonetimeInitAction;
    private static readonly object _lock = new object();
    /// <summary>
    /// 初始化
    /// </summary>
    public static ObjectPool Instance
    {
        get
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    GameObject obj = new GameObject("ObjectPool");
                    _instance = obj.AddComponent(typeof(ObjectPool)) as ObjectPool;
                    //_instance.Init();
                }
                return _instance;
            }
        }
    }
    public void Init()
    {
        Store(1, "Cube");
    }
    /// <summary>
    /// 获取
    /// </summary>
    /// <param name="id">ID号</param>
    /// <returns></returns>
    public GameObject Pick(int id)
    {
        if (myObjectPool.Count == 0)
            return null;
        if (myonetimeInitAction != null)
            myonetimeInitAction(myObjectPool[id]);
        myObjectPool[id].SetActive(true);
        return myObjectPool[id];
    }
    /// <summary>
    /// 回收
    /// </summary>
    /// <param name="id">ID号</param>
    public void RecyleObj(int id)
    {
        myObjectPool[id].SetActive(false);
        myObjectPool[id].transform.parent = transform;
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id">ID号</param>
    public void Remove(int id)
    {
        if (myObjectPool.ContainsKey(id))
        {
            Destroy(myObjectPool[id]);
            myObjectPool.Remove(id);
        }
    }
    /// <summary>
    /// 存储
    /// </summary>
    /// <param name="id">ID号</param>
    /// <param name="obj">池内添加物体</param>
    private void Store(int id, GameObject obj)
    {
        if (myObjectPool.ContainsKey(id))
        {
            Debug.Log("Repeat ID !");
            return;
        }
        GameObject newObj = Instantiate(obj) as GameObject;
        newObj.transform.parent = transform;
        newObj.SetActive(false);
        myObjectPool.Add(id, newObj);
    }
    /// <summary>
    /// 存储
    /// </summary>
    /// <param name="id">ID号</param>
    /// <param name="Path">Prefab地址</param>
    private void Store(int id, string Path)
    {
        if (myObjectPool.ContainsKey(id))
        {
            Debug.Log("Repeat ID !");
            return;
        }
        GameObject newObj = Instantiate(Resources.Load(Path)) as GameObject;
        newObj.transform.parent = transform;
        newObj.SetActive(false);
        myObjectPool.Add(id, newObj);
    }
    /// <summary>
    /// 池内物体列表
    /// </summary>
    /// <param name="Path">XML地址</param>
    public void ReadSubjectXml(string Path)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml((Resources.Load(Path) as TextAsset).text);
        XmlNodeList subjects = xmlDoc.SelectSingleNode("Model").ChildNodes;
        foreach (XmlElement view in subjects)
        {
            foreach (XmlElement content in view.ChildNodes)
            {
                Store(Convert.ToInt32(content.GetAttribute("ID")), content.InnerText);
            }
        }

    }
}

  写完以后感觉其实原理还是挺简单的,毕竟我对象池的作用只是用来GameObject的处理。

以上是关于Unity对象池试水的主要内容,如果未能解决你的问题,请参考以下文章

Unity - Animator组件

Unity - Animator组件

Unity中timeline出现脚本错误怎么解决

Unity编程Unity动画系统

unity 5.6 animation与anmimator

Unity报错:Read only asset Packages/com.xxxxx.xxx.xxxx/Editor/VSCodeDiscovery.cs.IPGSD has no meta(代码片段