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)

 

posted @ 2023-02-01 20:15  sun_dust_shadow  阅读(133)  评论(0编辑  收藏  举报