Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by .sanders · Aug 03, 2012 at 05:46 PM · sceneunityeditorhierarchy

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image .sanders · Aug 03, 2012 at 05:51 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image .sanders · Aug 13, 2012 at 11:04 AM 0
Share

Well I am not talking about runtime mode here, this is strictly editor mode. thanks anyway

avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paraself · Sep 02, 2013 at 03:59 AM 0
Share

It's also called when hitting PLAY button in editor....

avatar image
1

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
         }
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges