unity UnityEngine.Object 和 == ?. ??.的关系。
参考:
https://answers.unity.com/questions/1705335/how-can-throw-missing-reference-exception-this-gam.html
https://docs.unity3d.com/ScriptReference/Object.html
结论:
1.UnityEngine.Object是覆盖写了System.object的==和 !=
2.但是 ?. 和 ??.没有覆写,使用这两个是不安全的
3.关键概念:在UnityEngine层面的Fake Null。 UnityEngine.Object和nativeCode要有counterpart对应关联,并且说明这个Unity资源是可安全使用的,当Object ==null表示不能用并且交给GC去处理了。
What the method does is actually destroying the object on the native side and mark the managed wrapper object as "being destroyed". Such an object will essentially pretend that it is null since it's no longer useable since the native counterpart is missing. That fake null object will eventually be garbage collected once all references to it are gone.
4.网址内一个典型的例子:
1 // Object also creates a fake null object because it has no native counterpart 2 MyMonoBehaviour obj = new MyMonoBehaviour(); 3 4 // obj==null will be evaluated to true, since it is fake null. Since the variable type is 5 // UnityEngine.Object derived the custom == operator will be used. 6 if (obj == null) 7 8 // o==null will NOT evaluate to true since operators are not virtual methods. 9 // In t$$anonymous$$s case the System,Object == operator will be used. 10 object o = obj; 11 if (o == null)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!