{"id":23015976,"url":"https://github.com/s-rayleigh/prefabpool","last_synced_at":"2025-04-23T04:13:57.317Z","repository":{"id":186005046,"uuid":"673124514","full_name":"s-rayleigh/PrefabPool","owner":"s-rayleigh","description":"Prefab-oriented object pool for Unity.","archived":false,"fork":false,"pushed_at":"2025-04-10T12:10:23.000Z","size":33,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T04:13:50.218Z","etag":null,"topics":["object-pool","pool","pooling","prefab","unity"],"latest_commit_sha":null,"homepage":"","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/s-rayleigh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2023-07-31T23:47:27.000Z","updated_at":"2025-04-10T12:08:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"c7239db4-ab13-43e3-aff8-9f046c21c710","html_url":"https://github.com/s-rayleigh/PrefabPool","commit_stats":null,"previous_names":["s-rayleigh/prefabpool"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-rayleigh%2FPrefabPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-rayleigh%2FPrefabPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-rayleigh%2FPrefabPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-rayleigh%2FPrefabPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s-rayleigh","download_url":"https://codeload.github.com/s-rayleigh/PrefabPool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250366716,"owners_count":21418772,"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":["object-pool","pool","pooling","prefab","unity"],"created_at":"2024-12-15T11:14:10.756Z","updated_at":"2025-04-23T04:13:57.305Z","avatar_url":"https://github.com/s-rayleigh.png","language":"C#","readme":"# Prefab Pool\n[![openupm](https://img.shields.io/npm/v/com.rayleigh.prefab-pool?label=openupm\u0026registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.rayleigh.prefab-pool/)\n\nProvides efficient and lazy object pool, designed to use with the prefabs referenced by a component.\n\nFeatures:\n- Convenient API\n- Simple and efficient\n- Highly configurable\n- Test coverage\n\n# Why\nUnlike Unity's built-in object pool (available since version 2021), this pool aims to minimize the need for\nconfiguration and reduce the amount of code required to make it work. It is not a replacement for traditional\nobject pools, where a dedicated pool is allocated per object type, but a solution to a specific problem, where there\nis a necessity to create a large number of instances of different prefabs.\n\n# Installation\nFollow the guide by [this link](https://openupm.com/packages/com.rayleigh.prefab-pool/#modal-manualinstallation) to \ninstall the package using OpenUPM (recommended).\n\nOr you can add the package to your project via [UPM](https://docs.unity3d.com/Manual/upm-ui-giturl.html) using \nthis Git URL: https://github.com/s-rayleigh/PrefabPool.git\n\n# Usage\nYou can access the pool in either of these ways:\n- Use the static methods of the `GlobalPrefabPool`. They will also create the pool object in the \n`GlobalPrefabPool.Instance` property on the first usage.\n- Use the `GlobalPrefabPool.Instance` static property that acts like a Singleton.\n- Create an object of the `PrefabPool` class.\n- Use your favorite DI framework to inject the `PrefabPool` class.\n\nWhen creating a pool instance, you can also specify the name of the object to use as a parent for\ninstances returned to the pool using the `itemsParentName` argument of a constructor.\n\nTo get an object from the pool, use the `Get` or `TryGet` methods. The pool itself is prefab-oriented, so you need \nto use prefab as an argument referenced by a component (e.g., `Transform` or any inherited from `MonoBehaviour`).\n\nTo return an object to the pool, use the `Release` method. \nYou should only return the object to the pool from which it's taken.\n\nIf the pool has no available objects, it will create new ones, but you can create them in advance using the `Prewarm` \nmethod. It also keeps track of the number of objects it has created, which you can access using the `CountAll`, \n`CountInactive`, and `CountActive` methods.\n\nYou can also configure the pool **per prefab** using the `Configure` method and providing the `PoolParameters` struct, \nwhich allows you to specify the following parameters:\n- `maxCapacity` – the max capacity of the pool (`int.MaxValue` by default).\n- `activateOnGet` – whether to activate an object when it's taken from the pool (`true` by default).\n- `groupReturned` – enables grouping returned instances into an object to simplify scene objects hierarchy navigation. \n- Actions to perform:\n  - `onCreate` – when an object created by the pool.\n  - `onGet` – when an object taken from the pool.\n  - `onRelease` – when an object returned to the pool.\n  - `onDestroy` – when an object destroyed by the pool.\n\nYou can adjust these parameters at any time.\n\nWhen the pool reaches its max capacity, any returned object will be destroyed, the `Get` method will fail with an \nexception, and the `TryGet` method will return false.\n\nTo destroy all the inactive objects in the pool, use the `ClearInactive` method. You can also specify prefab as an \nargument to destroy only its instances.\n\nIf you want to receive the events in the component through which the prefab is referenced, you can implement these \ninterfaces: `IPoolGetHandler`, `IPoolReleaseHandler`, and `IPoolDestroyHandler`.\n\nPlease note that the pool is not thread-safe and is intended to be used on the Unity's main thread.\n\n# Examples\n```csharp\nclass MyComponent : MonoBehaviour, IPoolReleaseHandler\n{\n    [SerializeField]\n    private Renderer rend;\n\n    public void SetMaterial(Material material) =\u003e this.rend.sharedMaterial = material;\n\n    public void OnPoolRelease() =\u003e this.rend.sharedMaterial = null;\n}\n\nclass Spawner : MonoBehaviour\n{\n    [SerializeField]\n    private MyComponent prefab;\n\n    [SerializeField]\n    private Material mat;\n    \n    private List\u003cMyComponent\u003e instances = new();\n\n    private void Awake()\n    {   \n        for(var i = 0; i \u003c 500; i++)\n        {\n            var instance = GlobalPrefabPool.Get(this.prefab);\n            instance.SetMaterial(this.mat);\n            this.instances.Add(instance);\n        }\n    }\n\n    private void OnDestroy()\n    {\n        foreach(var instance in this.instances) GlobalPrefabPool.Release(instance);\n    }\n}\n```\n\nConfiguration example:\n```csharp\nGlobalPrefabPool.Configure(this.prefab, new(\n    maxCapacity: 500,\n    onGet: obj =\u003e Debug.Log($\"Object {obj.name} is taken from the pool.\")\n));\n```\n\nSee the tests for more examples.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-rayleigh%2Fprefabpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs-rayleigh%2Fprefabpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-rayleigh%2Fprefabpool/lists"}