1、Destory
public static voidDestroy(Objectobj, floatt= 0.0F);
Description
Removes a gameobject, component or asset.
The objectobjwill be destroyed now or if a time is specifiedtseconds from now. Ifobjis aComponentit will remove the component from theGameObjectand destroy it. Ifobjis aGameObjectit will destroy theGameObject, all its components and all transform children of theGameObject. Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.
2、Object.DestroyImmediate
public static voidDestroyImmediate(Objectobj, boolallowDestroyingAssets= false);
Description
Destroys the objectobjimmediately.
Use this function with care since it can destroy assets permanently and immediately. Also note that you should never iterate through arrays and destroy the elements you are iterating over. This function should only be used when writing editor code.
In game code you should useObject.Destroyinstead ofObject.DestroyImmediate. You are strongly recommended to use Object.Destroy always. Destroy is executed at a safe time. DestroyImmediate happens immediately.
總結:Destory會在場景中動態刪除對象、資源等,并不會在內存中銷毀并釋放,會有GC來決定什么時候銷毀,避免頻繁對內存讀寫操作。DestoryImmediate會立即從內從中釋放并銷毀對象。