I recently began working with Unity’s object pooling API, and noticed that implementing the IObjectPool interface is forcing me to implement a method that returns a PooledObject struct. However, this struct doesn’t actually have any accessible members, so what is its use? It’s also not in the Unity documentation (Unity - Scripting API: IObjectPool<T0>), which is strange.
This is what the assembly shows:
using System;
using UnityEngine.Pool;
public struct PooledObject<T> : IDisposable where T : class
{
private readonly T m_ToReturn;
private readonly IObjectPool<T> m_Pool;
internal PooledObject (T value, IObjectPool<T> pool)
{
m_ToReturn = value;
m_Pool = pool;
}
void IDisposable.Dispose ()
{
m_Pool.Release (m_ToReturn);
}
}
What is happening with IDisposable.Dispose? I thought C# discourages us from implementing IDisposable on structs.
My best guess is that this is supposed to somehow automatically return objects to their pools if the user forgets to do so, but I couldn’t make this happen in my testing.