{"id":20139400,"url":"https://github.com/mackysoft/xpool","last_synced_at":"2025-04-09T18:22:34.525Z","repository":{"id":40992775,"uuid":"453590991","full_name":"mackysoft/XPool","owner":"mackysoft","description":"Object pooling system for Unity.","archived":false,"fork":false,"pushed_at":"2023-08-20T03:48:05.000Z","size":949,"stargazers_count":71,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-23T20:22:39.686Z","etag":null,"topics":["collection","csharp","fast","objectpool","pool","pooling","unity"],"latest_commit_sha":null,"homepage":"https://mackysoft.github.io/XPool/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mackysoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-30T04:56:34.000Z","updated_at":"2025-03-19T08:00:55.000Z","dependencies_parsed_at":"2024-11-13T21:45:25.199Z","dependency_job_id":"6f64aa01-55f5-4012-a83e-5b5b2fb2cd40","html_url":"https://github.com/mackysoft/XPool","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mackysoft%2FXPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mackysoft%2FXPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mackysoft%2FXPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mackysoft%2FXPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mackysoft","download_url":"https://codeload.github.com/mackysoft/XPool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085763,"owners_count":21045209,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["collection","csharp","fast","objectpool","pool","pooling","unity"],"created_at":"2024-11-13T21:45:09.963Z","updated_at":"2025-04-09T18:22:34.513Z","avatar_url":"https://github.com/mackysoft.png","language":"C#","funding_links":["https://github.com/sponsors/mackysoft"],"categories":[],"sub_categories":[],"readme":"﻿# XPool - Object Pooling System for Unity\n\n[![Tests](https://github.com/mackysoft/XPool/actions/workflows/tests.yaml/badge.svg)](https://github.com/mackysoft/XPool/actions/workflows/tests.yaml) [![Build](https://github.com/mackysoft/XPool/actions/workflows/build.yaml/badge.svg)](https://github.com/mackysoft/XPool/actions/workflows/build.yaml) [![Release](https://img.shields.io/github/v/release/mackysoft/XPool)](https://github.com/mackysoft/XPool/releases) [![openupm](https://img.shields.io/npm/v/com.mackysoft.xpool?label=openupm\u0026registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.mackysoft.xpool/)\n\n![XPool_Frame](https://user-images.githubusercontent.com/13536348/154980636-f949151e-f820-4d86-b9e9-302880671d14.png)\n\n**Created by Hiroya Aramaki ([Makihiro](https://twitter.com/makihiro_dev))**\n\n## What is XPool ?\n\nXPool is an object pooling library for Unity.\n\nThis was developed to be able to do all the pooling expected in application development with just this library.\n\n- All your classes can be pooled.\n- Short code, easy to use.\n- Fast performance\n- Scalability\n- Tested. It works stably.\n\n\n## \u003ca id=\"index\" href=\"#index\"\u003e Table of Contents \u003c/a\u003e\n\n- [📥 Installation](#installation)\n- [🔰 Usage](#usage)\n  - [Unity Object Pool](#unity-object-pool)\n    - [ParticleSystem Pool](#particle-system-pool)\n  - [Pure C# Object Pool](#object-pool)\n  - [Collection Pool](#collection-pool)\n  - [Non Allocated Collections](#non-allocated-collections)\n  - [How to write custom pool ?](#how-to-write-custom-pool)\n      - [ParticleSystemPool implementation](#particle-system-pool-implementation)\n  - [Optimization](#optimization)\n- [✉ Help \u0026 Contribute](#help-and-contribute)\n- [📔 Author Info](#author-info)\n- [📜 License](#license)\n\n# \u003ca id=\"installation\" href=\"#installation\"\u003e 📥 Installation \u003c/a\u003e\n\nDownload any version from releases.\n\nReleases: https://github.com/mackysoft/XPool/releases\n\n\n### Install via PackageManager\n\nOr, you can add this package by opening PackageManager and entering\n\n`https://github.com/mackysoft/XPool.git?path=Assets/MackySoft/MackySoft.XPool`\n\nfrom the `Add package from git URL` option.\n\n\n### Install via Open UPM\n\nOr, you can install this package from the [Open UPM](https://openupm.com/packages/com.mackysoft.xpool/) registry.\n\nMore details [here](https://openupm.com/).\n\n```\nopenupm add com.mackysoft.xpool\n```\n\n# \u003ca id=\"usage\" href=\"#usage\"\u003e 🔰 Usage \u003c/a\u003e\n\nThe full Scripting API is [here](https://mackysoft.github.io/XPool/api/MackySoft.XPool.html).\n\nScripting API: https://mackysoft.github.io/XPool/api/MackySoft.XPool.html\n\n## \u003ca id=\"unity-object-pool\" href=\"#unity-object-pool\"\u003e Unity Object Pool (GameObject, Component) \u003c/a\u003e\n\nPooling of Unity Object (GameObject, Component) can be performed using `GameObjectPool` or `ComponentPool\u003cT\u003e`.\nThese hierarchical objects can be rented by writing them in a similar way to `Instantiate` method.\n\n```cs\n// Example code is being written...\n```\n\n\u003c!--\n```cs\npublic class Projectile : MonoBehaviour {\n\n    public float speed;\n    public event Action OnHit;\n\n    void Update () {\n        transform.Translate(Vector3.forward * speed * Time.deltaTime);\n    }\n\n    void OnCollisionEnter (Collision collision) {\n        OnHit?.Invoke();\n    }\n}\n\npublic class OnHitTrigger : MonoBehaviour {\n    public void Initialize (Projectile projectile,IPool\u003cProjectile\u003e pool) {\n        projectile.OnHit += () =\u003e pool.Return(projectile);\n    }\n\n    \n}\n\npublic class Shooter : MonoBehaviour {\n\n    [SerializeField]\n    ComponentPool\u003cProjectile\u003e m_ProjectilePool = new ComponentPool\u003cProjectile\u003e();\n\n    void Awake () {\n        m_ProjectilePool.OnCreate = instance =\u003e instance.gameObject.AddComponent\u003cOnHitTrigger\u003e();\n        m_ProjectilePool.OnRent = instance =\u003e instance.gameObject.SetActive(true);\n        m_ProjectilePool.OnRetuern = instance =\u003e instance.gameObject.SetActive(false);\n        m_ProjectilePool.OnRelease = instance =\u003e Destroy(instance.gameObject);\n    }\n\n    public void Shoot (){\n        Projectile instance = m_ProjectilePool.Rent(transform.position,transform.rotation);\n        instance.OnHit += () =\u003e \n    }\n}\n```\n--\u003e\n\n### \u003ca id=\"particle-system-pool\" href=\"#particle-system-pool\"\u003e ParticleSystem Pool \u003c/a\u003e\n\nOptimized pools are available for some of the components implemented in Unity.\n\nThis is an example of `ParticleSystemPool` usage.\n\n```cs\npublic class HitParticleSystemEmitter : MonoBehaviour {\n\n    [SerializeField]\n    ParticleSystemPool m_HitParticleSystemPool = new ParticleSystemPool();\n\n    void OnCollisionEnter (Collision collision) {\n        // The rented ParticleSystem is automatically returned to the pool when completed.\n        m_HitParticleSystemPool.Rent(collision.contacts[0],Quaternion.identity);\n    }\n}\n```\n\nIf you need an optimized pool for other components, please refer to the [How to write custom pool ?](#how-to-write-custom-pool) section, or give me feedback via [issues](https://github.com/mackysoft/XPool/issues) or [pull requests](https://github.com/mackysoft/XPool/pulls).\n\n\n## \u003ca id=\"object-pool\" href=\"#object-pool\"\u003e Object Pool (Pure C# Object) \u003c/a\u003e\n\n`FactoryPool\u003cT\u003e` can be used to pool Pure C# Object.\n\n\u003e Unity Object are not supported, as they behave differently from Pure C# Object in that they can be set to null externally with `Destroy` method.\n\n```cs\n// Give the capacity and factory method to the constructor.\nvar pool = new FactoryPool\u003cMyClass\u003e(8,() =\u003e new MyClass());\n\n// Create new instance by factory if pool is empty.\nMyClass instance = pool.Rent();\n\n// Return instance to the pool.\npool.Return(instance);\n```\n\n\n## \u003ca id=\"collection-pool\" href=\"#collection-pool\"\u003e Collection Pool (`T[]`, `List\u003cT\u003e`, `Qeueue\u003cT\u003e`, `Stack\u003cT\u003e`, `HashSet\u003cT\u003e`, `Dictionary\u003cTKey,TValue\u003e`) \u003c/a\u003e\n\nAn optimized pool is provided for the generic collections provided in .NET Framework.\n\n```cs\n// Rent an array from the pool. \n// Note that the array length to be rented is the next two powers of minimumLength.\nT[] array = ArrayPool\u003cT\u003e.Shared.Rent(minimumLength: 10);\n\n// Return array to the pool.\nArrayPool\u003cT\u003e.Shared.Return(array);\n\n// ListPool\u003cT\u003e, QueuePool\u003cT\u003e, StackPool\u003cT\u003e, HashSetPool\u003cT\u003e, DictionaryPool\u003cTKey,TValue\u003e are also available.\nList\u003cT\u003e list = ListPool\u003cT\u003e.Shared.Rent();\nQueue\u003cT\u003e queue = QueuePool\u003cT\u003e.Shared.Rent();\nStack\u003cT\u003e stack = StackPool\u003cT\u003e.Shared.Rent();\nHashSet\u003cT\u003e hashSet = HashSetPool\u003cT\u003e.Shared.Rent();\nDictionary\u003cTKey,TValue\u003e dictionary = DictionaryPool\u003cTKey,TValue\u003e.Shared.Rent();\n```\n\n\n## \u003ca id=\"non-allocated-collections\" href=\"#non-allocated-collections\"\u003e Non allocated collections \u003c/a\u003e\n\nYou can use the TemporaryCollections API that leverages `ArrayPool\u003cT\u003e`.\n\nThese collections are a struct and internally use array rented from `ArrayPool\u003cT\u003e`.\n\nTherefore, it is fast and non-allocation.\n\n```cs\n// Create a temporary array.\nvar array = TemporaryArray\u003cT\u003e.Create(10);\n\n// You must release collection when you are done using it.\narray.Dispose();\n\n// TemporaryList\u003cT\u003e, TemporaryQueue\u003cT\u003e, TemporaryStack\u003cT\u003e are also available.\nTemporaryList\u003cT\u003e list = TemporaryList\u003cT\u003e.Create();\nTemporaryQueue\u003cT\u003e queue = TemporaryQueue\u003cT\u003e.Create();\nTemporaryStack\u003cT\u003e stack = TemporaryStack\u003cT\u003e.Create();\n```\n\n\n## \u003ca id=\"how-to-write-custom-pool\" href=\"#how-to-write-custom-pool\"\u003e How to write custom pool ? \u003c/a\u003e\n\nIf you want to implement a more customized pool, you can quickly create one by using the provided base classes.\n\nThe base class of the pool is in the `ObjectModel` namespace.\n\n- `MackySoft.XPool.ObjectMode.PoolBase\u003cT\u003e`\n- `MackySoft.XPool.Unity.ObjectModel.UnityObjectPoolBase\u003cT\u003e`\n- `MackySoft.XPool.Unity.ObjectModel.ComponentPoolBase\u003cT\u003e`\n\n```cs\nusing MackySoft.XPool.ObjectModel; // PoolBase\u003cT\u003e is here.\n\npublic class MyPool : PoolBase\u003cMyClass\u003e {\n\n    public MyPool () {\n    }\n\n    public MyPool (MyClass original,int capacity) : base(original,capacity) {\n    }\n\n    // Called when Rent is invoked and there are no instances in the pool.\n    protected override MyClass Factory () {\n        return new MyClass();\n    }\n\n    // Called when an instance is rented from the pool.\n    // This is also the case when a new instance is created by the Factory.\n    protected override void OnRent (MyClass instance) {\n\n    }\n\n    // Called when an instance is returned to the pool.\n    protected override void OnReturn (MyClass instance) {\n\n    }\n\n    // Called when the capacity is exceeded and the instance cannot be returned to the pool,\n    // or when the instance is released by the ReleaseInstances method.\n    protected override void OnRelease (MyClass instance) {\n    \n    }\n}\n```\n\n### \u003ca id=\"particle-system-pool-implementation\" href=\"#particle-system-pool-implementation\"\u003e ParticleSystemPool implementation \u003c/a\u003e\n\nAs an example, `ParticleSystemPool` is implemented using `ComponentPoolBase\u003cT\u003e`.\nIts functionality has been optimized for ParticleSystem.\n\n```cs\nusing System;\nusing UnityEngine;\nusing MackySoft.XPool.Unity.ObjectModel; // ComponentPoolBase\u003cT\u003e is here.\n\n[Serializable]\npublic class ParticleSystemPool : ComponentPoolBase\u003cParticleSystem\u003e {\n\n    [SerializeField]\n    bool m_PlayOnRent;\n\n    public bool PlayOnRent { get =\u003e m_PlayOnRent; set =\u003e m_PlayOnRent = value; }\n\n    public ParticleSystemPool () {\n    }\n\n    public ParticleSystemPool (ParticleSystem original,int capacity) : base(original,capacity) {\n    }\n\n    protected override void OnCreate (ParticleSystem instance) {\n        var main = instance.main;\n        main.stopAction = ParticleSystemStopAction.Callback;\n        var trigger = instance.gameObject.AddComponent\u003cParticleSystemStoppedTrigger\u003e();\n        trigger.Initialize(instance,this);\n    }\n\n    protected override void OnRent (ParticleSystem instance) {\n        if (m_PlayOnRent) {\n            instance.Play(true);\n        }\n    }\n\n    protected override void OnReturn (ParticleSystem instance) {\n        instance.Stop(true,ParticleSystemStopBehavior.StopEmitting);\n    }\n\n    protected override void OnRelease (ParticleSystem instance) {\n        UnityEngine.Object.Destroy(instance.gameObject);\n    }\n\n    public class ParticleSystemStoppedTrigger : MonoBehaviour {\n\n        ParticleSystem m_ParticleSystem;\n        IPool\u003cParticleSystem\u003e m_Pool;\n\n        internal void Initialize (ParticleSystem ps,IPool\u003cParticleSystem\u003e pool) {\n            m_ParticleSystem = ps;\n            m_Pool = pool;\n        }\n\n        void OnParticleSystemStopped () {\n            m_Pool?.Return(m_ParticleSystem);\n        }\n\n    }\n}\n```\n\n\n# \u003ca id=\"help-and-contribute\" href=\"#help-and-contribute\"\u003e ✉ Help \u0026 Contribute \u003c/a\u003e\n\nI welcome feature requests and bug reports in [issues](https://github.com/mackysoft/XPool/issues) and [pull requests](https://github.com/mackysoft/XPool/pulls).\n\nIf you feel that my works are worthwhile, I would greatly appreciate it if you could sponsor me.\n\nGitHub Sponsors: https://github.com/sponsors/mackysoft\n\n# \u003ca id=\"author-info\" href=\"#author-info\"\u003e 📔 Author Info \u003c/a\u003e\n\nHiroya Aramaki is a indie game developer in Japan.\n\n- Twitter: [https://twitter.com/makihiro_dev](https://twitter.com/makihiro_dev)\n- Blog: [https://mackysoft.net/blog](https://mackysoft.net/blog)\n\n\n# \u003ca id=\"license\" href=\"#license\"\u003e 📜 License \u003c/a\u003e\n\nThis library is under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmackysoft%2Fxpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmackysoft%2Fxpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmackysoft%2Fxpool/lists"}