docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Object pooling

    Netcode for GameObjects (Netcode) provides built-in support for Object Pooling, which allows you to override the default Netcode destroy and spawn handlers with your own logic. This allows you to store destroyed network objects in a pool to reuse later. This is useful for often used objects, such as projectiles, and is a way to increase the application's overall performance. By pre-instantiating and reusing the instances of those objects, object pooling removes the need to create or destroy objects at runtime, which can save a lot of work for the CPU. This means that instead of creating or destroying the same object over and over again, it's simply deactivated after use, then, when another object is needed, the pool recycles one of the deactivated objects and reactivates it.

    See Introduction to Object Pooling to learn more about the importance of pooling objects.

    NetworkPrefabInstanceHandler

    You can register your own spawn handlers by including the INetworkPrefabInstanceHandler interface and registering with the NetworkPrefabHandler.

        public interface INetworkPrefabInstanceHandler
        {
            NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation);
            void Destroy(NetworkObject networkObject);
        }
    

    Netcode will use the Instantiate and Destroy methods in place of default spawn handlers for the NetworkObject used during spawning and despawning. Because the message to instantiate a new NetworkObject originates from a Host or Server, both won't have the Instantiate method invoked. All clients (excluding a Host) will have the instantiate method invoked if the INetworkPrefabInstanceHandler implementation is registered with NetworkPrefabHandler (NetworkManager.PrefabHandler) and a Host or Server spawns the registered/associated NetworkObject.

    In This Article
    Back to top
    Copyright © 2025 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)