• 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 /
This question was closed Aug 18, 2020 at 05:00 PM by kurtpieper392 for the following reason:

Die Frage ist beantwortet, die korrekte Antwort wurde akzeptiert

avatar image
Question by kurtpieper392 · Aug 17, 2020 at 03:38 PM · editor-scriptingkeyongui errorkeyboardevent

Tastenabfrage im EditMode 100x pro Sekunde ?

Hallo, ich habe eine Frage. Ich möchte in einem Editorscript im Editmode Eine Tastenabfrage durchführen. Dafür nehme ich die folgenden Code:

 void  OnSceneGUI(){

             if (Event.current.alt)
             {

 //some stuff


             }

 }

Jetzt möchte ich das 100x pro Sekunde abfragen.Ich habe bin dabei auf diesen Code gestoßen

 void OnEnable()
 {
     EditorApplication.update += OnSceneGUI;
 
 
 }



Das funktioniert nicht,und wenn ich anstatt von OnSceneGUI eine eigene Methode nehmen würde, hätte ich keine Zugriff auf die Event Klasse.

Kann mir jemand helfen dieses Problem zu lösen,oder kennt jemand eine andere herangehensweise? Ich danke im voraus für Antworten.

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Aug 17, 2020 at 04:04 PM

Sorry but you've mixed completely different and independent concepts here. The Event class contains information about the current raised event. It only has meaning inside of an Event callback that is called by Unity. The Event class does not allow you to poll anything. Furthermore you named your method "OnSceneGUI" which is a callback from Unity which is called automatically by unity. You never ever want to call Unity callbacks manually through any other means. Registering it to the generic update delegate makes no sense.


It's not clear what you actually want to do. The whole Unity editor is completely event based. It this is part of a custom editor / inspector you can simply override the RequiresConstantRepaint method and return true.

 public override bool RequiresConstantRepaint()
 {
     return true;
 }

This will make the inspector to redraw itself constantly. However you should be careful with such tools. To manually cause a redraw of the sceneview(s) you usually use SceneView.RepaintAll.


Regardless of all that you can always just set a boolean variable to true while a key is held down.

 bool isAltDown = false;
 
 void OnSceneGUI()
 {
     Event e = Event.current;
     if (e.isKey)
         isAltDown = e.alt;
 }

Though as I said it highly depends on whats the point of your code that should run 100 times a second. For example in my UVViewer editor window I redraw the sceneview constantly when the CTRL key is held down because I draw things in my editor window as well as the scene view and this needs to be updated.


So just to make that crystal clear: Just because you subscribed the OnSceneGUI method to the update delegate, doesn't mean you can access the Event class (or Event.current to be more precise). The Event class is not magically bound to a certain method but is explicitly set by Unity before it calls this callback. If you call this callback from somewhere else, there is no active event.

Just for reference, my IMGUI crash course.

Comment

People who like this

0 Show 0 · 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

Answer by kurtpieper392 · Aug 18, 2020 at 04:55 PM

Ok danke für deine Antwort , ich denke aber ich werde sie löschen da sie aus jetziger sicht keinen sinn ergibt.

Comment

People who like this

0 Show 0 · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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

Related Questions

Prevent unity hotkeys in editor 0 Answers

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

unused keys for editor scripts 0 Answers

EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer

Closing an editor window causes OnGui error 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