// MIT License - Copyright (c) 2025 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE #if VCONTAINER_PRESENT namespace WallstopStudios.UnityHelpers.Integrations.VContainer { using System; using global::VContainer; using global::VContainer.Unity; using UnityEngine; using UnityEngine.Pool; /// /// Helpers for creating UnityEngine.Pool object pools plus extensions that hydrate pooled items /// through VContainer when you rent them via . /// public static class RelationalObjectPools { /// /// Creates a component pool to be combined with /// so items are injected and hydrated on rental time. /// public static ObjectPool CreatePoolWithRelations( Func createFunc, Action actionOnGet = null, Action actionOnRelease = null, Action actionOnDestroy = null, bool collectionCheck = true, int defaultCapacity = 10, int maxSize = 10000 ) where T : Component { if (createFunc == null) { throw new ArgumentNullException(nameof(createFunc)); } return new ObjectPool( createFunc, actionOnGet: actionOnGet, actionOnRelease: actionOnRelease, actionOnDestroy: actionOnDestroy, collectionCheck: collectionCheck, defaultCapacity: defaultCapacity, maxSize: maxSize ); } /// /// Creates a GameObject pool to be combined with /// so hierarchies are /// injected and hydrated on rental time. /// public static ObjectPool CreateGameObjectPoolWithRelations( GameObject prefab, Transform parent = null, Action actionOnGet = null, Action actionOnRelease = null, Action actionOnDestroy = null, bool collectionCheck = true, int defaultCapacity = 10, int maxSize = 10000 ) { if (prefab == null) { throw new ArgumentNullException(nameof(prefab)); } Func create = () => UnityEngine.Object.Instantiate(prefab, parent); return new ObjectPool( create, actionOnGet: actionOnGet, actionOnRelease: actionOnRelease, actionOnDestroy: actionOnDestroy, collectionCheck: collectionCheck, defaultCapacity: defaultCapacity, maxSize: maxSize ); } /// /// Rents an item from the pool, injects and assigns relational fields. /// public static T GetWithRelations(this ObjectPool pool, IObjectResolver resolver) where T : Component { T item = pool.Get(); if (item != null) { resolver?.Inject(item); resolver.AssignRelationalComponents(item); } return item; } /// /// Rents a GameObject from the pool, injects the hierarchy and assigns relational fields. /// public static GameObject GetWithRelations( this ObjectPool pool, IObjectResolver resolver ) { GameObject go = pool.Get(); if (go != null) { resolver?.InjectGameObject(go); resolver.AssignRelationalHierarchy(go, includeInactiveChildren: true); } return go; } } } #endif