docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class MessagePool<T>

    A thread-safe pool of IMessage instances of type T that reduces garbage allocation by recycling message objects.

    Inheritance
    object
    MessagePool<T>
    Implements
    IMessagePool
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.SimulationPro.Foundation
    Assembly: Unity.SimulationPro.Foundation.dll
    Syntax
    public class MessagePool<T> : IMessagePool where T : IMessage, new()
    Type Parameters
    Name Description
    T

    The IMessage type managed by this pool. Must have a public parameterless constructor.

    Remarks

    Pass an instance to SetMessagePool(IMessagePool) to enable pooling on a publisher. Call GetOrCreateMessage() to obtain a message instance, fill it in, then publish it. After delivery the subscriber or publisher can return it to the pool via AddMessage(T).

    Constructors

    MessagePool(int)

    Creates a new MessagePool<T> with the specified maximum capacity.

    Declaration
    public MessagePool(int maxSize = 10)
    Parameters
    Type Name Description
    int maxSize

    The maximum number of messages retained in the pool. Excess recycled messages are discarded.

    Methods

    AddMessage(T)

    Returns a typed message to the pool for reuse.

    Declaration
    public void AddMessage(T messageToRecycle)
    Parameters
    Type Name Description
    T messageToRecycle

    The message instance to recycle.

    GetOrCreateMessage()

    Get a message from the pool, or create one if necessary.

    Declaration
    public T GetOrCreateMessage()
    Returns
    Type Description
    T

    A new or recycled message of type T, which may contain garbage data. Be sure to fill it in.

    Implements

    IMessagePool
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)