공부/Unity

[Unity] DontDestroyOnLoad()

돌멩이수프 2022. 5. 3. 11:29
728x90

Sound의 경우 게임 어디에서도 사용되는 Object다. 어떤 Object는 Scene이 바뀌면서 삭제됐다가 필요한 경우 다시 생성되지만, 계속 필요한 Object는 삭제되는 행위 자체가 쓸데없는 메모리 사용이다.

이때 DontDestroyOnLoad()를 사용해주면 게임이 꺼질 때까지 Object가 삭제되지 않고 필요할 때 언제든 꺼내 쓸 수 있다.

 

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

 

Unity - Scripting API: Object.DontDestroyOnLoad

The load of a new Scene destroys all current Scene objects. Call Object.DontDestroyOnLoad to preserve an Object during scene loading. If the target Object is a component or GameObject, Unity also preserves all of the Transform’s children. Object.DontDest

docs.unity3d.com

 

728x90