true operator in unity 3D

public static implicit operator bool(Object exists)
{
return !Object.CompareBaseObjects(exists, (Object) null);
}

 

private static bool CompareBaseObjects(Object lhs, Object rhs)
{
return Object.CompareBaseObjectsInternal(lhs, rhs);
}

[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private static bool CompareBaseObjectsInternal([Writable] Object lhs, [Writable] Object rhs);

When you get a c# object of type “GameObject”[2], it contains almost nothing. this is because Unity is a C/C++ engine. All the actual information about this GameObject (its name, the list of components it has, its HideFlags, etc) lives in the c++ side. The only thing that the c# object has is a pointer to the native object. We call these c# objects “wrapper objects”. The lifetime of these c++ objects like GameObject and everything else that derives from UnityEngine.Object is explicitly managed. These objects get destroyed when you load a new scene. Or when you callObject.Destroy(myObject); on them. Lifetime of c# objects gets managed the c# way, with a garbage collector. This means that it’s possible to have a c# wrapper object that still exists, that wraps a c++ object that has already been destroyed. If you compare this object to null, our custom == operator will return “true” in this case, even though the actual c# variable is in reality not really null.

 

//this is implicit opertaor overloading if(object) if(object!=null)
if (camera)
{
Debug.Log("camera is camera !=null");
camera = lastuserdCamera;
}
else
{
Debug.Log("camera is camera == null");
lastuserdCamera = camera;
}

posted @ 2014-05-19 10:53  penney  阅读(302)  评论(0)    收藏  举报