- Home /
Die Frage ist beantwortet, die korrekte Antwort wurde akzeptiert
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.
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.
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.