| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using UnityEngine; |
| | | 7 | | |
| | | 8 | | namespace GDX |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="UnityEngine.Object" /> Based Extension Methods |
| | | 12 | | /// </summary> |
| | | 13 | | [VisualScriptingCompatible(2)] |
| | | 14 | | public static class ObjectExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Destroy a <see cref="UnityEngine.Object" /> appropriately based on the current state of the Editor/Playe |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="targetObject">The target <see cref="UnityEngine.Object" /> to be destroyed.</param> |
| | | 20 | | /// <param name="delay">How long should be waited before the <paramref name="targetObject" /> is destroyed?</par |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 22 | | public static void SafeDestroy(this Object targetObject, float delay = 0f) |
| | 0 | 23 | | { |
| | | 24 | | #if UNITY_EDITOR |
| | 0 | 25 | | if (Application.isPlaying) |
| | 0 | 26 | | { |
| | 0 | 27 | | Object.Destroy(targetObject, delay); |
| | 0 | 28 | | } |
| | | 29 | | else |
| | 0 | 30 | | { |
| | 0 | 31 | | Object.DestroyImmediate(targetObject); |
| | 0 | 32 | | } |
| | | 33 | | #else |
| | | 34 | | Object.Destroy(targetObject, delay); |
| | | 35 | | #endif // UNITY_EDITOR |
| | 0 | 36 | | } |
| | | 37 | | } |
| | | 38 | | } |