/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Networking.Game
{
using UnityEngine;
using UnityEngine.SceneManagement;
///
/// Bridge component between the networking spawning system and the ObjectPool.
///
public abstract class NetworkObjectPool : MonoBehaviour
{
private static NetworkObjectPool s_Instance;
private static NetworkObjectPool Instance { get { return s_Instance; } }
///
/// Does the NetworkObjectPool exist?
///
/// True if the NetworkObjectPool exists.
public static bool IsNetworkActive()
{
return s_Instance != null;
}
///
/// The object has been enabled.
///
protected virtual void OnEnable()
{
// The object may have been enabled outside of the scene unloading.
if (s_Instance == null) {
s_Instance = this;
SceneManager.sceneUnloaded -= SceneUnloaded;
}
}
///
/// Spawns the object over the network. This does not instantiate a new object on the local client.
///
/// The object that the object was instantiated from.
/// The object that was instantiated from the original object.
/// Is the object owned by the scene? If fales it will be owned by the character.
public static void NetworkSpawn(GameObject original, GameObject instanceObject, bool sceneObject)
{
if (s_Instance == null) {
Debug.LogError("Error: Unable to spawn object - the Network Object Pool doesn't exist.");
return;
}
s_Instance.NetworkSpawnInternal(original, instanceObject, sceneObject);
}
///
/// Internal method which spawns the object over the network. This does not instantiate a new object on the local client.
///
/// The object that the object was instantiated from.
/// The object that was instantiated from the original object.
/// Is the object owned by the scene? If fales it will be owned by the character.
protected abstract void NetworkSpawnInternal(GameObject original, GameObject instanceObject, bool sceneObject);
///
/// Destroys the object instance on the network.
///
/// The object to destroy.
public static void Destroy(GameObject obj)
{
if (s_Instance == null) {
Debug.LogError("Error: Unable to destroy object - the Network Object Pool doesn't exist.");
return;
}
s_Instance.DestroyInternal(obj);
}
///
/// Internal method which destroys the object instance on the network.
///
/// The object to destroy.
protected abstract void DestroyInternal(GameObject obj);
///
/// Returns if the specified object was spawned with the network object pool.
///
/// The object instance to determine if was spawned with the object pool.
/// True if the object was spawned with the network object pool.
public static bool SpawnedWithPool(GameObject obj)
{
if (s_Instance == null) {
return false;
}
return s_Instance.SpawnedWithPoolInternal(obj);
}
///
/// Internal method which returns if the specified object was spawned with the network object pool.
///
/// The object instance to determine if was spawned with the object pool.
/// True if the object was spawned with the network object pool.
protected abstract bool SpawnedWithPoolInternal(GameObject obj);
///
/// Reset the initialized variable when the scene is no longer loaded.
///
/// The scene that was unloaded.
private void SceneUnloaded(Scene scene)
{
s_Instance = null;
SceneManager.sceneUnloaded -= SceneUnloaded;
}
///
/// The object has been disabled.
///
protected virtual void OnDisable()
{
SceneManager.sceneUnloaded += SceneUnloaded;
}
#if UNITY_2019_3_OR_NEWER
///
/// Reset the static variables for domain reloading.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void DomainReset()
{
s_Instance = null;
}
#endif
}
}