Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Firedan1176 · Nov 27, 2015 at 04:49 PM · editorcustom editorcustomeditor

EditorWindow.OnSelectionChange() in scene view?

I'd like to be able to detect pretty much any user interaction within the editor (moving objects, saving the scene, etc) so that I could know when these changes are being made. I'd like to simply get a debug message right now when the scene is changed, such as if I select a different object in the hierarchy/scene view, I'd like to get a simple debug message saying "OnSelectionChange", but this doesn't happen:

 using UnityEditor;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class RealtimeStuff : EditorWindow {
     void OnSelectionChange() {
         Debug.Log("OnSelectionChange");
     }
 }

Is this possible with the scene/hierarchy view, or does this message only work with custom editor windows? What can I do to achieve what I want? Keep in mind I don't have any scripts on any gameobjects, I simply wish for this to work without being on an object (which as far as I know is impossible).

EDIT I know that OnSelectionChange doesn't get called when I select a different object, but I have all the other messages from EditorWindow with debug messages, so I should get something, right?

I have also added [CustomEditor(typeof(SceneView))] above my class, and changed EditorWindow to Editor.

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Nov 27, 2015 at 05:22 PM

EditorWindow callbacks are only invoked when the window is open. If the class is everything you have it shouldn't do anything. Also the ExecuteInEditMode attribute is ment for MonoBehaviour instances. If you don't create an instance of your EditorWindow it can't possible do anything.

 using UnityEditor;
 using UnityEngine;
 
 public class RealtimeStuff : EditorWindow
 {
     [MenuItem("Tools/RealtimeStuff")]
     static void OpenWindow()
     {
         GetWindow<RealtimeStuff>();
     }
     void OnSelectionChange() {
         Debug.Log("OnSelectionChange");
     }
     void OnGUI()
     {
         GUILayout.Label("This is some window content");
     }
 }

This allows you to actually open your editor window. While the window is open you should receive OnSelectionChange messages.

Alternatively you can use a class that is marked with InitializeOnLoad and add a callback to Selection.selectionChanged or any other callback in EditorApplication like hierarchyWindowChanged in the static constructor of the class.

 [InitializeOnLoad]
 public class Extension
 {
     static Extension()
     {
         Selection.selectionChanged += OnSelectionChange;
         EditorApplication.hierarchyWindowChanged += OnHierarchyChanged;
     }
     static void OnSelectionChange()
     {
         Debug.Log("Selection changed");
         // use the Selection class to determine which object(s) are selected
     }
     static void OnHierarchyChanged()
     {
         Debug.Log("Hierarchy changed");
     }
 }

Keep in mind that this class is loaded as soon as your project is opened in Unity.

Comment
Add comment · Show 3 · 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 Firedan1176 · Nov 27, 2015 at 05:54 PM 0
Share

Thanks, is there any way to detect if I select a different object within the scene view, and/or get its properties such as transform's position?

avatar image Firedan1176 Firedan1176 · Nov 27, 2015 at 06:01 PM 0
Share

I was able to detect if I select an object in the hierarchy, but that new EditorWindow must be visible/docked. Can I make this possible without being docked?

avatar image Bunny83 Firedan1176 · Nov 27, 2015 at 09:45 PM 0
Share

Uhm, the selectionChanged event of the Selection class will be called whenever you select a different object. The Selection class is also used to deter$$anonymous$$e what object / objects are selected.

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

32 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

layout serializable objects in unity 1 Answer

Drag from custom editor ObjectField 0 Answers

The ‘ContextClick’ event of the ‘TreeView’ class does not fire from a custom inspector; but if you do it from an editor window it fires. 0 Answers

Expanding a custom property drawer of a list item forces further items in the list to disappear. 2 Answers

Editor Targets are updated but SerializedPropertys arent 1 Answer


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