- Home /
Callback for when an object is deleted from the scene by the user in EditorMode.
Hi guys,
Just a quick question. Is there any way of knowing when a particular object is being deleted by the user in either the scene or hierarchy view. The reason I ask is because I keep track of references to objects in an editor script so in order to keep them up to date I need a callback or some indication of knowing when this happens and update my references. I already looked through the EditorScripts Documentation but didn't find what I was looking for. Or I might have missed it.
any help would be highly appreciated.
.sanders
okay I am currently looking in on what HierarchyWindowChanged might reveal to me but I'm afraid I will need to keep track and check too many object to know which one was deleted.
Answer by strachpr01 · Aug 04, 2012 at 05:35 AM
Take a look at the OnDestroyed function in the scriptin reference. It should let you know when objects are being destroyed
Well I am not talking about runtime mode here, this is strictly editor mode. thanks anyway
Answer by SinPin · Dec 06, 2012 at 01:48 AM
Hey ;)
Late answer - I had the same problem 10 minutes ago and just wanted to mention, that the answer of strachpr01 is absolutely correct, just missing one detail:
The OnDestroyed function in Editor is only called, if the Editor Script is tagged with the Attribute "ExecuteInEditMode()" Find more details here
Here is an example:
// ExecuteInEditMode:
// Call all CallBacks also at Editor-Time (Update, OnGUI, OnDestroy, ...)
[ExecuteInEditMode()]
public class MyScript : MonoBehaviour
{
// Called when an object with this script is deleted in editor.
public void OnDestroy()
{
Debug.Log("I was called.");
}
}
In the Unity Editor, I have a gameObject with the Script "MyScript" as component. When I delete this gameObject, the OnDestroy will be called.
Worked perfectly for me ;)
Yours SinPin
Answer by Homer-Johnston · Nov 12, 2014 at 05:28 AM
Extra late answer - I create a singleton class called EditorState with the following code (C#):
using UnityEngine;
using System;
using System.Collections;
[ExecuteInEditMode]
public class EditorState : MonoBehaviour
{
#if UNITY_EDITOR
// Modified singleton implementation; will not spawn instances of itself, user _must_ create one manually in the scene.
// This is because the code will try to find an instance between edit and play mode when objects are deleted, and it
// creates an unwanted clone which persists when exiting play mode.
private static EditorState _instance;
private static object _lock = new object();
private static EditorState Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = (EditorState)FindObjectOfType(typeof(EditorState));
}
return _instance;
}
}
}
/// <summary>
/// Returns whether or not the editor is in edit mode.
/// </summary>
public static bool InEditMode
{
get
{
if (Instance == null)
return false;
return Instance.inEditMode;
}
}
[SerializeField]
private bool inEditMode = false;
[NonSerialized]
private bool enteringPlayMode = false;
[NonSerialized]
private bool editModeCallbackAdded = false;
[NonSerialized]
private bool updateCallbackAdded = false;
void OnEnable()
{
if (!editModeCallbackAdded)
{
editModeCallbackAdded = true;
UnityEditor.EditorApplication.playmodeStateChanged += EditModeCallback;
}
if (!updateCallbackAdded)
{
updateCallbackAdded = true;
UnityEditor.EditorApplication.update += UpdateCallback;
}
}
private void EditModeCallback()
{
if (!Application.isPlaying && inEditMode)
enteringPlayMode = true;
}
private void UpdateCallback()
{
if (inEditMode == Application.isPlaying && !enteringPlayMode)
inEditMode = !inEditMode;
else if (enteringPlayMode)
inEditMode = false;
}
#endif
}
Note: singleton code stolen from http://wiki.unity3d.com/index.php/Singleton . This basically brute-force checks using the EditorApplication.update callback to check Application.isPlaying. It uses another callback on EditorApplication.playmodeStateChanged to check for an edge case that the brute force check doesn't catch.
To use this, note that you will need a dummy gameobject in your scene with the script attached. In your own scripts you can simply check EditorState.InEditMode, e.g.:
private void OnDestroy
{
if (EditorState.InEditMode)
{
// Object was deleted by User or Unity in edit mode
}
}
Your answer
Follow this Question
Related Questions
About batches and triangles 1 Answer
How to move whole folder from project view to hierarchy view 0 Answers
Unloaded scenes continuously keep adding? 1 Answer
Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 Answers
How to prevent Unity3D making unnecessary changes after saving scene with no actual changes 0 Answers