Search Unity

Input system for pen/stylus use with editor tools?

Discussion in 'Input System' started by plauk, May 17, 2019.

  1. plauk

    plauk

    Joined:
    Jun 4, 2013
    Posts:
    10
    I'm trying to figure out how to use the new input system in conjunction with a custom editor window, specifically for pen (pressure/tilt) input on a tool I'm building. I've sort of run into a wall in that Input Actions etc don't seem to serialize as I would expect them to when used with scriptable objects (as opposed to Monobehaviours) and I'm really having trouble figuring out how to reference input from something like a custom editor window that isn't necessarily tied to a gameobject.

    All documentation and videos I've seen focus primarily on player input in game, rather than editor input and I'm wondering if anyone can point me in the right direction. Is this even an appropriate use for the input system, or is there another way to access pen input that I'm not aware of?
     
    Lars-Steenhoff likes this.
  2. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    731
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Note that input actions do not work in edit mode. They are a game-time only feature.

    As for EditorWindow code, you should be able to access the input system from any EditorWindow callback (such as OnGUI). See here.

    Note that there's some gotchas, though. The input system doesn't have a UI-centric model for processing input. Architecturally, it's game-oriented and has no event routing kind of mechanisms. So, unlike UnityEngine.Event, input coming in from the input system has no concept of UI element focus (the only focus mechanism it has is application wide).

    So in practice, you'll likely have to tie code explicitly to focus being on your EditorWindow.
     
  4. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Is it also possible to read the pressure from the scene view? I'm trying to see if I can add pressure to the Polybrush package.
     
    XYZhang_xn likes this.
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    @Lars-Steenhoff Heya Lars :)

    It can be read from anywhere the editor is inside an EditorWindow callback. In an experiment some time ago we used that to add gamepad navigation to the scene view. However, from what little I remember of that experiment, I recall we had to define a new specialized SceneView because we didn't have proper extension points to hook into. But if you can find the right hook for what you're looking for, then yes, you should be able to read input from within SceneView.
     
  6. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Thanks, sounds complicated .... Just gonna wait if pressure support is added to the tools by the devs.
     
  7. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    or maybe?
    public void OnSceneGUI(SceneView sceneView)
     
  8. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Input should work in there. If that callback fits your needs, you should be good to go :)
     
  9. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    I have no idea what I'm doing :
    But It reads the pressure when I resize the Wacom window
    But only a few times after that my input is not working anymore.

    Can you maybe see if its possible to make and editor example how to use the input system properly inside the editor?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEditor;
    6. using System;
    7.  
    8.  
    9. #if UNITY_EDITOR
    10. using UnityEngine.InputSystem.Editor;
    11. #endif
    12.  
    13.  
    14. public class Wacom : EditorWindow
    15. {
    16.  
    17.     private float _pressure = 0f;
    18.     [MenuItem("Tools/Wacom")]
    19.     public static void ShowWindow()
    20.     {
    21.  
    22.         EditorWindow.GetWindow<Wacom>("Wacom");
    23.  
    24.     }
    25.  
    26.     public void OnGUI()
    27.  
    28.     {
    29.      
    30.  
    31.         var pen = Pen.current;
    32.         //if (pen != null)
    33.         {
    34.             _pressure = pen.pressure.ReadValue();
    35.          
    36.         }
    37.  
    38.         DrawPressure();
    39.     }
    40.  
    41.     private void DrawPressure()
    42.     {
    43.         GUILayout.Label("Pressure:");
    44.         EditorGUILayout.BeginHorizontal();
    45.         Rect r = EditorGUILayout.GetControlRect(false);
    46.         GUI.color = Color.gray;
    47.         GUI.Box(r, GUIContent.none);
    48.         GUI.color = Color.green;
    49.         float w = r.width * _pressure;
    50.         Rect r2 = new Rect(r.x, r.y, w, r.height);
    51.         GUI.Box(r2, GUIContent.none);
    52.         EditorGUILayout.EndHorizontal();
    53.     }
    54.  
    55.  
    56. }
    57.  
     
  10. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Looking at this, I'm seeing some regressions with the functionality. Digging into it and also adding a sample and looking for ways in which we can prevent this from regressing in the future. Stay tuned.

    ////EDIT: Just to leave a trace of the issues here (tried only Windows):
    - EditorWindow space conversion seems to have been broken making coordinates not be relative to the currently active EditorWindow
    - Using the pen will also send mouse events but the position in those will not get updated while the pen moves.
     
    Last edited: Oct 16, 2019
    Lars-Steenhoff likes this.
  11. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Did you manage the EditorWindow space conversion bug?