Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to conditionally execute code when object has been dropped in a scene (scene view or hierarchy)

Discussion in 'Scripting' started by Aberdyne, Nov 24, 2016.

  1. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Hello everyone.

    I'm doing some editor script coding. I would like to execute some code when an object (with a particular component) is dropped in the scene or in the hierarchy window BUT only if the control key is held down.

    So I need to solve these two problems:
    1) detect object creation
    2) detect control being held down

    Unfortunately, I'm unable to solve these two problems at the same time... I tried in two ways.

    The first way is through my custom inspector. I can solve (2) easily using Event.current.control but not (1). I tried using EditorApplication.hierarchyWindowChanged but the documentation says "Each time an object is (or a group of objects are) created, renamed, parented, unparented or destroyed this callback is raised.". I did not find any way to know which one of these (created, renamed,...).

    So I tried the other way, through my MonoBehaviour script... I came up with this for (1)

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class MyComponent : MonoBehaviour
    3. {
    4. ...
    5. #if UNITY_EDITOR
    6.   private void Awake()
    7.   {
    8.     if (!Application.isPlaying)
    9.       Debug.Log("Created");
    10.   }
    11. #endif
    12. ...
    13. }
    Whenever I drop an object with MyComponent on it, Debug.Log("Created") would be called. The problem is how do I detect (2) now? I assumed you cannot get the current key down in the editor from a MonoBehaviour. Have I missed something?
     
    Last edited: Nov 24, 2016
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Read again. You seem to have missed the fact that I already tried from editor script (custom inspector) ;)

    If only I could detect the creation of the object in editor script too (like I did through the MonoBehaviour script) it would be perfect...
     
    LeftyRighty likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    If somebody would find a reliable way to figure out the input state during edit time without having to be in a GUI-frame, I'd pay a lot of money for that.

    Afaik, it's simply not possible to do those two things. Unity's really not got any good ways to check that an object's been dragged into the scene, and Unity's really not got any good ways to check if control's pressed at edit time. Figuring out either requires hacks, and none of the hacks I know of for either overlap.