Skip to content

Instantly share code, notes, and snippets.

@harrissaleem
Last active June 30, 2025 17:05
Show Gist options
  • Save harrissaleem/4db886a735a2f5af88abfaeca6505290 to your computer and use it in GitHub Desktop.
Save harrissaleem/4db886a735a2f5af88abfaeca6505290 to your computer and use it in GitHub Desktop.
This is a sample RealtimePoolManager

RealtimePoolManager Setup and Usage Guide

Overview

This guide provides essential information on setting up and using the RealtimePoolManager in Unity projects that utilize Normcore for real-time multiplayer experiences. Proper setup and usage are crucial for optimal performance and stability, especially when dealing with multiple scenes and dynamic connections to different Normcore rooms.

Prerequisites

  • Basic understanding of Unity and Normcore.
  • RealtimePoolManager script ready in your project.

Setup Instructions

Attaching to GameObject

  1. Realtime GameObject Attachment:
    • Ensure that the RealtimePoolManager script is attached to the Realtime GameObject in your Unity scene. This is crucial for the manager to function correctly within the context of the Normcore framework.

Scene Management and Prefab Preloading

  1. Handling Multiple Scenes and Normcore Rooms:
    • The script is designed to handle the dynamics of connecting and disconnecting from multiple Normcore rooms across different scenes.
    • It ensures that a maximum number of prefabs are preloaded, maintaining efficiency and smooth transitions between scenes.

Important Considerations

  1. Prefab Deletion in Scene Unloads:
    • Be aware that in some cases, prefabs might get deleted when unloading scenes. This typically happens if their destruction methods are not being properly called.
    • Ensure that scene transitions and prefab lifecycle are managed correctly to avoid unintended deletions or memory leaks.

Best Practices

  • Regularly test scene transitions to ensure prefabs are handled correctly.
  • Monitor the performance during connections and disconnections to/from Normcore rooms.
  • Keep the RealtimePoolManager script updated with the latest project changes.

Troubleshooting

  • If you encounter unexpected behaviors, first verify if the RealtimePoolManager is correctly attached and configured.
  • Check console logs for any errors or warnings that might provide insights into issues during scene transitions or prefab management.

Contributing

Feedback and contributions are welcome. If you have suggestions or improvements, please feel free to submit a pull request or open an issue.


For more detailed documentation and advanced usage, refer to the official Normcore Documentation.

Happy Coding!

using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;
using System.Collections;
[System.Serializable]
public struct ObjectPool_Data
{
public GameObject Prefab;
public int AmountToPool;
public bool PreLoad;
}
public class RealtimePoolManager : MonoBehaviour, IRealtimePrefabInstantiateDelegate, IRealtimePrefabLoadDelegate
{
public List<ObjectPool_Data> ObjectPool_Datas;
private Dictionary<string, Stack<GameObject>> objectPool = new();
void Awake()
{
// This script needs to be attached to the same component as Realtime
var realtime = GetComponent<Realtime>();
realtime.didConnectToRoom -= PreLoad;
realtime.didConnectToRoom += PreLoad;
}
void PreLoad(Realtime realtime)
{
foreach (var data in ObjectPool_Datas)
{
if (!objectPool.TryGetValue(data.Prefab.name, out Stack<GameObject> poolStack))
{
poolStack = new Stack<GameObject>();
objectPool[data.Prefab.name] = poolStack;
}
for (int i = poolStack.Count; i < data.AmountToPool; i++)
{
GameObject obj = Instantiate(data.Prefab, transform, true);
obj.name = data.Prefab.name;
obj.SetActive(false);
poolStack.Push(obj);
}
}
}
#region IRealtimePrefabInstantiateDelegate Implementation
public GameObject InstantiateRealtimePrefab(GameObject prefab)
{
GameObject obj;
// RealtimePoolObject is used to check if this prefab needs to be pooled or not. Its just a script that is attached to the prefabs in case if its not preloaded but we want it to be pooled.
var pooledObj = prefab.GetComponent<RealtimePoolObject>();
if (pooledObj != null)
{
if (objectPool.TryGetValue(prefab.name, out Stack<GameObject> poolStack) && poolStack.Count > 0)
{
obj = poolStack.Pop();
obj.SetActive(true);
obj.transform.parent = null;
return obj;
}
}
obj = Instantiate(prefab);
obj.name = prefab.name;
return obj;
}
public void DestroyRealtimePrefab(GameObject prefabInstance)
{
var pooledObj = prefabInstance.GetComponent<RealtimePoolObject>();
if (pooledObj != null)
{
prefabInstance.SetActive(false);
prefabInstance.transform.parent = transform;
if (objectPool.TryGetValue(prefabInstance.name, out Stack<GameObject> poolStack))
{
poolStack.Push(prefabInstance);
}
else
{
poolStack = new Stack<GameObject>();
poolStack.Push(prefabInstance);
objectPool[prefabInstance.name] = poolStack;
}
}
else
{
Destroy(prefabInstance);
}
}
#endregion
#region IRealtimePrefabLoadDelegate Implementation
public GameObject LoadRealtimePrefab(RealtimePrefabMetadata prefabMetadata)
{
return Resources.Load<GameObject>(prefabMetadata.prefabName);
}
#endregion
}
using UnityEngine;
/// <summary>
/// This is used to check if this prefab needs to be pooled or not.
/// Its just a script that is attached to the prefabs in case if its not preloaded but we want it to be pooled.
/// </summary>
public class RealtimePoolObject : MonoBehaviour
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment