Last active
October 12, 2021 19:07
-
-
Save thebeardphantom/007ed806398a8eda0b4ce99681dae3b0 to your computer and use it in GitHub Desktop.
Useful if you don't know the type of an object and you want to ensure you don't bypass Unity's native-side null checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool IsNull<T>(this T obj) where T : class | |
{ | |
if(obj is UnityEngine.Object unityObj) | |
{ | |
/* Invokes Unity's custom == check for nulls. | |
* This case technically only works because obj is | |
* a "fake null" on the C# side by this point. | |
* If its a true null it'd hit the else case instead, | |
* which fortunately still returns what we want. | |
*/ | |
return unityObj == null; | |
} | |
else | |
{ | |
// Uses regular C# null check | |
return obj == null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment