#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;

[ExecuteInEditMode]
public class DummyDelete : MonoBehaviour {

	public static bool EditorApplicationQuit = false;

	static bool WantsToQuit() {
		EditorApplicationQuit = true;
		return true;
	}

	// Initialize fireing an event on quit Unity
	void OnEnable() {
		EditorApplication.wantsToQuit += WantsToQuit;
	}

	private void OnDestroy() {

		// No action on press [Play] and quitting the Unity Editor
		if (Time.frameCount == 0 || EditorApplicationQuit) {
			//Debug.Log ("hit");
			return;
		}

		var go = this.gameObject;
		var pos = go.transform.position;
		var rot = go.transform.rotation;

		var headertext = "Delete Object...";
		var maintext = "Do you want to delete the game object :" + go.name;
		var ops1 = "Delete";
		var ops2 = "Cancel";

		if (EditorUtility.DisplayDialog(headertext, maintext, ops1, ops2)) {
			//Debug.Log("Do anything before destroy");
		} else {
			var sceneCurrent = SceneManager.GetActiveScene();
			SceneManager.SetActiveScene(go.scene);

			var goCopy = Instantiate(go);
			goCopy.transform.parent = go.transform.parent;
			goCopy.transform.localPosition = pos;
			goCopy.transform.localRotation = rot;

			goCopy.name = go.name;
			goCopy.SetActive(true);

			SceneManager.SetActiveScene(sceneCurrent);
			Selection.activeGameObject = goCopy;
			//Debug.Log("The GameObject wasn't deleted!");
			return;
		}
	}
}
#endif
