2026-03-20 11:56:50 -04:00
using System ;
2025-10-03 00:02:43 -04:00
using CW.Common ;
2026-03-20 11:56:50 -04:00
using UnityEditor ;
using UnityEngine ;
2025-10-03 00:02:43 -04:00
namespace Lean.Pool.Examples
{
2026-03-20 11:56:50 -04:00
/// <summary>
/// This component can be added to your prefab GameObject, and it will throw warnings if it is instantiated
/// without the use of <b>LeanPool.Spawn</b>, or despawned without the use of <b>LeanPool.Despawn</b>.
/// </summary>
2025-10-03 00:02:43 -04:00
[HelpURL(LeanPool.HelpUrlPrefix + "LeanPoolDebugger")]
2026-03-20 11:56:50 -04:00
[AddComponentMenu(LeanPool.ComponentPathPrefix + "Pool Debugger")]
public class LeanPoolDebugger : MonoBehaviour
{
[SerializeField] private LeanGameObjectPool cachedPool ;
[NonSerialized] private bool skip ;
protected virtual void Start ( )
{
if ( ! Exists ( ) )
{
Debug . LogWarning ( "This clone was NOT spawned using LeanPool.Spawn?!\n" + name , this ) ;
enabled = false ;
}
}
protected virtual void Update ( )
{
if ( cachedPool = = null )
{
Debug . LogWarning ( "The pool this prefab was spawned using has been destroyed.\n" + name , this ) ;
enabled = false ;
}
else if ( ! Exists ( ) )
{
Debug . LogWarning ( "This clone was despawned using LeanPool.Despawn, but it's still active?!\n" + name ,
this ) ;
enabled = false ;
}
}
protected virtual void OnDestroy ( )
{
if ( skip ) return ;
if ( Exists ( ) )
Debug . LogWarning (
"This clone has been destroyed, and it was NOT despawned using LeanPool.Despawn?!\n" + name , this ) ;
}
protected virtual void OnApplicationQuit ( )
{
skip = true ;
}
private bool Exists ( )
{
if ( LeanPool . Links . TryGetValue ( gameObject , out cachedPool ) ) return true ;
if ( LeanGameObjectPool . TryFindPoolByClone ( gameObject , ref cachedPool ) ) return true ;
return false ;
}
2025-10-03 00:02:43 -04:00
#if UNITY_EDITOR
2026-03-20 11:56:50 -04:00
protected virtual void OnEnable ( )
{
EditorApplication . playModeStateChanged + = Changed ;
}
protected virtual void OnDisable ( )
{
EditorApplication . playModeStateChanged - = Changed ;
}
private void Changed ( PlayModeStateChange state )
{
if ( state = = PlayModeStateChange . ExitingPlayMode ) skip = true ;
}
2025-10-03 00:02:43 -04:00
#endif
2026-03-20 11:56:50 -04:00
}
2025-10-03 00:02:43 -04:00
}
#if UNITY_EDITOR
namespace Lean.Pool.Examples.Editor
{
2026-03-20 11:56:50 -04:00
using TARGET = LeanPoolDebugger ;
[CanEditMultipleObjects]
[CustomEditor(typeof(TARGET), true)]
public class LeanPoolDebugger_Editor : CwEditor
{
protected override void OnInspector ( )
{
TARGET tgt ;
TARGET [ ] tgts ;
GetTargets ( out tgt , out tgts ) ;
Info (
"This component can be added to your prefab GameObject, and it will throw warnings if it is instantiated without the use of <b>LeanPool.Spawn</b>, or despawned without the use of <b>LeanPool.Despawn</b>." ) ;
}
}
2025-10-03 00:02:43 -04:00
}
#endif