Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using AnimationCurve in an Input Processor

Discussion in 'Input System' started by DarthDisembowel, Dec 17, 2020.

  1. DarthDisembowel

    DarthDisembowel

    Joined:
    Jun 11, 2014
    Posts:
    54
    I'm trying to setup a custom input processor that will evaluate stick values through a curve, but I can't seem to get it to work and can't find any complete examples of a working custom processor. Declaring the animation curve in the processor generates an error "ArgumentException: Don't know how to convert PrimitiveValue to 'Object'", and setting up a custom editor for the curve almost works, but the curve gets reset whenever I launch the game. I can see why it does so, but not sure how to arrange it to avoid it.

    I will swallow my pride and show the code I'm using, with the disclaimer that I've never messed with Unity editor interfaces before. Can anyone highlight the problem, or point me to a working example I can use as reference (the one in the Unity documentation appears to be incomplete)?

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4. using UnityEngine.InputSystem.Editor;
    5.  
    6.  
    7. #if UNITY_EDITOR
    8. [InitializeOnLoad]
    9. #endif
    10.  
    11. public class InputResponseCurve : InputProcessor<Vector2>
    12. {
    13. #if UNITY_EDITOR
    14.     static InputResponseCurve()
    15.     {
    16.         Initialize();
    17.     }
    18. #endif
    19.  
    20.     [RuntimeInitializeOnLoadMethod]
    21.     static void Initialize()
    22.     {
    23.         InputSystem.RegisterProcessor<InputResponseCurve>();
    24.     }
    25.  
    26.     InputResponseCurveEditor curveEvaluator = new InputResponseCurveEditor();
    27.  
    28.     public override Vector2 Process(Vector2 value, InputControl control)
    29.     {
    30.         if (value.x >= 0)
    31.         {
    32.             value.x = curveEvaluator.EvaluateResponseCurve(value.x);
    33.         }
    34.         else
    35.         {
    36.             value.x = -curveEvaluator.EvaluateResponseCurve(Mathf.Abs(value.x));
    37.         }
    38.         return value;
    39.     }
    40. }
    41.  
    42. #if UNITY_EDITOR
    43. public class InputResponseCurveEditor : InputParameterEditor<InputResponseCurve>
    44. {
    45.     AnimationCurve curve = AnimationCurve.EaseInOut(0,0,1,1);
    46.  
    47.     public override void OnGUI()
    48.     {
    49.         curve = EditorGUILayout.CurveField("Response Curve", curve);
    50.     }
    51.  
    52.     public float EvaluateResponseCurve(float value)
    53.     {
    54.         return curve.Evaluate(value);
    55.     }
    56. }
    57. #endif
     
  2. ttnet

    ttnet

    Joined:
    Jul 22, 2020
    Posts:
    2
  3. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    88
    Also looking for an answer to this and hit a dead end after re-reading the docs several times. Going to have to move on and just create the curve evaluation in a script rather than as a custom input processor. I hit the same error "ArgumentException: Don't know how to convert PrimitiveValue to 'Object'. Parameter name: type".

    Did you ever find a fix for it being reset whenever you launch the game?
     
  4. Antonidiuss

    Antonidiuss

    Joined:
    Apr 13, 2019
    Posts:
    5
    June 23. Unity 2022.3.1 LTS - same problem