// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; using Object = UnityEngine.Object; namespace Microsoft.MixedReality.Toolkit { /// /// Extension methods for Unity's Object class /// public static class UnityObjectExtensions { /// /// Enable Unity objects to skip "DontDestroyOnLoad" when editor isn't playing so test runner passes. /// public static void DontDestroyOnLoad(this Object target) { #if UNITY_EDITOR if (UnityEditor.EditorApplication.isPlaying) #endif Object.DontDestroyOnLoad(target); } /// /// Destroys a Unity object appropriately depending if running in edit or play mode. /// /// Unity object to destroy /// Time in seconds at which to destroy the object, if applicable. public static void DestroyObject(Object obj, float t = 0.0f) { if (Application.isPlaying) { Object.Destroy(obj, t); } else { #if UNITY_EDITOR // Must use DestroyImmediate in edit mode but it is not allowed when called from // trigger/contact, animation event callbacks or OnValidate. Must use Destroy instead. // Delay call to counter this issue in editor UnityEditor.EditorApplication.delayCall += () => { Object.DestroyImmediate(obj); }; #else Object.DestroyImmediate(obj); #endif } } /// /// Tests if an interface is null, taking potential UnityEngine.Object derived class implementers into account /// which require their overridden operators to be called. /// /// True if either the managed or native object is null, false otherwise. public static bool IsNull(this T @interface) where T : class => @interface == null || @interface.Equals(null); /// /// Tests if an interface is null, taking potential UnityEngine.Object derived class implementers into account /// which require their overridden operators to be called. /// /// Falseif either the managed or native object is null, true otherwise. public static bool IsNotNull(this T @interface) where T : class => !@interface.IsNull(); /// /// Properly checks an interface for null and returns the MonoBehaviour implementing it. /// /// True if the implementer of the interface is a MonoBehaviour and the MonoBehaviour is not null. public static bool TryGetMonoBehaviour(this T @interface, out MonoBehaviour monoBehaviour) where T : class => (monoBehaviour = @interface as MonoBehaviour) != null; } }