LenweSeregon
LenweSeregon

Reputation: 132

Detect gameobject removal from user action

I'm working on a system that automatically generate and release scriptable object that represent uniqueness for gameobject with a given component attach.

The generation process seems to be working really fine, but I'm facing problem when I want to release the scriptable object. The given script is tagged "ExecuteInEditMode" and is implementating OnDestroy method to advertise a manager that his scriptable object should be deleted. The problem is that OnDestroy is called in 4 situation from what i can tell :

I've been able to avoid the 2 first on with this :

if (Application.isPlaying == false && Math.Abs(Time.timeSinceLevelLoad) > 0.00001f)

But I don't find any good solution for "On editor shutdown" I've seen that EditorApplication.quitting can be use for that case, but documentation warn that this event is not called when unity crash or is forced to quit, and I don't want to lose all my scriptable object if the editor crash.

I'm looking for a robust solution to avoid this problem if someone can help me please.

Thanks, have a nice day

Upvotes: 1

Views: 124

Answers (1)

sommmen
sommmen

Reputation: 7628

Just handle the application quitting event.

this event is not called when unity crash or is forced to quit, and I don't want to lose all my scriptable object if the editor crash.

When unity crashes, so will your program, so it won't work any ways. Just make sure that under normal operating procedure (so a normal shutdown) you save your modifications.

In Unity3D if you edit your scene and you don't save, if the editor crashes you lose all your changes. The same will happen for what you're building here. In order to mitigate this risk (since prevention is reasonably difficult if not impossible) you can opt to save every minute, always save each change to a temporary location, or save for example every play.

For example i added a script to my project that autosaves the scene(s) whenever i press play, so when editing the scene i press play now and then to test, and it all gets autosaved.

Upvotes: 1

Related Questions