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 www4 · May 02, 2018 at 10:28 PM · scripting problemeditorinputeditor-scriptingscene view

How can I check if a key is down in scene view?

Hi, I needed to automate placing bits of terrain in my game in editor. For this purpose, I have created a script presented below. It works perfectly in play mode, but it's supposed to work specifically in edit mode.

 using System.Collections;
 using UnityEngine;
   [ExecuteInEditMode]
   [InitializeOnLoad]
   public class placeTerrainBits : MonoBehaviour {
 
     private placeTerrainBits script;
 
     void OnEnable() {
         script = gameObject.GetComponent<placeTerrainBits>();
         EditorApplication.update += Update;
     }
 
     void Update() {
         Debug.Log ("Updating");
         if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftControl)) {
             Place();
         }
     }
     void Place() {
       // irrelevant code
     }
 }

Unfortunately, this script seems not to be "listening" for keyboard input in scene view. The Update method is working fine, as confirmed by a quick test, but the script seems to fail at the input-checking stage. Is there any way I can actually make the script to check for input in scene view? Thanks

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 · May 02, 2018 at 11:15 PM

This is wrong on so many levels ^^.

  • First of all MonoBehaviours are runtime scripts and not editor scripts.

  • ExecuteInEditMode is not meant to implement editing features but just to enable runtime features during edit mode. An example is the particlesystem which is still updated even the game is not playing.

  • InitializedOnLoad only triggers the static constructor and doesn't work for instance classes.

  • EditorApplication is an editor class which can not be used inside a runtime class.

  • You should never directly call a callback of Unity. You subscribe the Update callback to the update delegate of the editor. You should always use a seperate method which you subscribe to such a delegate.

  • The Input class only works at runtime. It can't be used at edit time.

  • The update delegate is a generic update which has no relation to the sceneview.


What you should do is creating either a normal class and using InitializedOnLoad and use the static constructor of the class to subscribe your delegate and / or initialize a singleton instance of your class. Or create an EditorWindow in which you can subscribe inside OnEnable (and unsubscribe in OnDisable)


If you want to handle key or mouse events in the scene view you should subscribe a method to SceneView.onSceneGUIDelegate. It works like OnGUI. It allows you to handle any sort of event using the Event class including mouse, keyboard and repaint events.


Keep in mind that InitializeOnLoad can't be "disabled". It will initialize the object when Unity opens the project. It's usually better to create an EditorWindow or a CustomEditor (inspector for a runtime script) to implement any additional editing functions. It allows to easily enable / disable that functionality.


To actually check for a mouse down even while the control key is held down you would do

 // inside OnSceneGUI / onSceneGUIDelegate callback
 Event e = Event.current;
 if (e.control && e.type == EventType.MouseDown && e.button = 0)
 {
     Debug.Log("click");
 }

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 www4 · May 03, 2018 at 05:50 PM 0
Share

Thank you for your thorough answer. I've ended up creating an EditorWindow as you suggested.

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

180 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 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 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 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 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 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

How to check if a key is down in editor script 2 Answers

Check if mouse is down while in Edit Mode 1 Answer

Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer

myObjetc..GetType().GetCustomAttributes(typeof(RequireComponent) not working correctly 1 Answer

Get callback when assets loading or deserialization finished? 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