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.
總結(jié):Destory會(huì)在場景中動(dòng)態(tài)刪除對象、資源等,并不會(huì)在內(nèi)存中銷毀并釋放,會(huì)有GC來決定什么時(shí)候銷毀,避免頻繁對內(nèi)存讀寫操作。DestoryImmediate會(huì)立即從內(nèi)從中釋放并銷毀對象。