C# 检查Unity3D 2019.3.05f中的检查器中是否已分配游戏对象
问题: 如何检查是否已在中的检查器中指定了单一行为的公共游戏对象 Unity3D,因为nullC# 检查Unity3D 2019.3.05f中的检查器中是否已分配游戏对象,c#,unity3d,isnull,C#,Unity3d,Isnull,问题: 如何检查是否已在中的检查器中指定了单一行为的公共游戏对象 Unity3D,因为null(object==null)的比较失败 具体示例: 我将为Unity3D编写一个通用方法,它可以在任何可为null的对象上调用。它检查对象是否为空,如果为空,则写入Debug.LogError(customMessage)。方法如下所示: public static bool IsNull<T>([CanBeNull] this T myObject, string message = "
(object==null)
的比较失败
具体示例:
我将为Unity3D编写一个通用方法,它可以在任何可为null的对象上调用。它检查对象是否为空,如果为空,则写入Debug.LogError(customMessage)
。方法如下所示:
public static bool IsNull<T>([CanBeNull] this T myObject, string message = "")
{
if (myObject!= null) return false;
Debug.LogError("The object is null! " + message);
return true;
}
对于大多数用例,我的方法工作得很好,并且在编码/调试期间节省了大量时间。但我现在的问题是,如果我没有在编辑器中对“testObject”签名,我的测试将无法工作,因为testObject似乎不为null,但它也不可用,因为它没有赋值。在这种情况下,控制台输出为:
假空的 为什么
(myObject==null)
是false,而Debug.Log(testObject)
给了我null
,只要在unity inspector中没有指定相应的对象
编辑/解决方案:
多亏了derHugo的帮助,我最终得到了以下通用代码片段:
public static bool IsNull<T>(this T myObject, string message = "") where T : class
{
switch (myObject)
{
case UnityEngine.Object obj when !obj:
Debug.LogError("The object is null! " + message);
return true;
case null:
Debug.LogError("The object is null! " + message);
return true;
default:
return false;
}
}
public static bool为null(此T myObject,字符串消息=”),其中T:class
{
开关(myObject)
{
case UnityEngine.Object对象当!obj:
LogError(“对象为null!”+消息);
返回true;
大小写为空:
LogError(“对象为null!”+消息);
返回true;
违约:
返回false;
}
}
Unity有一个等式操作符的自定义实现(请参阅这通常会导致混淆/意外行为)
即使UnityEngine.Object
的值等于null
,它有时也不是==null
。
对象
仍然在其中存储一些元数据,例如,您会得到一个MissingReferenceException
,而不是通常的NullReferenceException
,因为Unity实现了一些自定义异常,这些异常通常包含为什么的更多细节,该值等于null
尤其是自从
public GameObject testObject = null;
是一个公共
字段,它会自动序列化,因此,在序列化过程中,分配给它的null
值将被覆盖
哪个
GameObject
派生自一个隐式操作符
对象是否存在
与其直接进行==null
比较,不如检查
public static bool IsNull<T>(this T myObject, string message = "") where T : UnityEngine.Object
{
if(!myObject)
{
Debug.LogError("The object is null! " + message);
return false;
}
return true;
}
public static bool为null(此T myObject,字符串消息=”),其中T:UnityEngine.Object
{
if(!myObject)
{
LogError(“对象为null!”+消息);
返回false;
}
返回true;
}
对于适用于任何引用类型的通用方法,您可以使用
public static bool IsNull<T>(this T myObject, string message = "") where T : class
{
if (myObject is UnityEngine.Object obj)
{
if (!obj)
{
Debug.LogError("The object is null! " + message);
return false;
}
}
else
{
if (myObject == null)
{
Debug.LogError("The object is null! " + message);
return false;
}
}
return true;
}
public static bool为null(此T myObject,字符串消息=”),其中T:class
{
if(myObject是UnityEngine.Object对象)
{
如果(!obj)
{
LogError(“对象为null!”+消息);
返回false;
}
}
其他的
{
if(myObject==null)
{
LogError(“对象为null!”+消息);
返回false;
}
}
返回true;
}
谢谢,这很好。谢谢。有没有一种方法可以重载此方法,使其也适用于所有非Unity引擎对象,然后我可以用“经典”的方式检查所有其他null值?
public static bool IsNull<T>(this T myObject, string message = "") where T : class
{
if (myObject is UnityEngine.Object obj)
{
if (!obj)
{
Debug.LogError("The object is null! " + message);
return false;
}
}
else
{
if (myObject == null)
{
Debug.LogError("The object is null! " + message);
return false;
}
}
return true;
}