0
\$\begingroup\$

I'm following the "Touch Samples" sample project that comes with the "input system" package.

In one of the sample scene, there is a script that defines a custom composite Binding (for example the 2D vector composite).

I'm practically rewriting the same scripts except changing some inputs to be added to the composite, but it's throwing me these errors, and since I'm relatively new to writing code don't know how to begin solving the issue. The manual and the scripting API are no help either.

these are the errors enter image description here

this is the example Script that adds a custom composite.


using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace InputSamples.Drawing
{
    /// <summary>
    /// Simple object to contain information for drag inputs.
    /// </summary>
    public struct PointerInput
    {


        public bool Contact;

        /// <summary>
        /// ID of input type.
        /// </summary>
        public int InputId;

        /// <summary>
        /// Position of draw input.
        /// </summary>
        public Vector2 Position;

        /// <summary>
        /// Orientation of draw input pen.
        /// </summary>
        public Vector2? Tilt;

        /// <summary>
        /// Pressure of draw input.
        /// </summary>
        public float? Pressure;

        /// <summary>
        /// Radius of draw input.
        /// </summary>
        public Vector2? Radius;

        /// <summary>
        /// Twist of draw input.
        /// </summary>
        public float? Twist;
    }

    // What we do in PointerInputManager is to simply create a separate action for each input we need for PointerInput.
    // This here shows a possible alternative that sources all inputs as a single value using a composite. Has pros
    // and cons. Biggest pro is that all the controls actuate together and deliver one input value.
    //
    // NOTE: In PointerControls, we are binding mouse and pen separately from touch. If we didn't care about multitouch,
    //       we wouldn't have to to that but could rather just bind `<Pointer>/position` etc. However, to source each touch
    //       as its own separate PointerInput source, we need to have multiple PointerInputComposites.
    #if UNITY_EDITOR
    [InitializeOnLoad]
    #endif
    public class PointerInputComposite : InputBindingComposite<PointerInput>
    {
        [InputControl(layout = "Button")]
        public int contact;

        [InputControl(layout = "Vector2")]
        public int position;

        [InputControl(layout = "Vector2")]
        public int tilt;

        [InputControl(layout = "Vector2")]
        public int radius;

        [InputControl(layout = "Axis")]
        public int pressure;

        [InputControl(layout = "Axis")]
        public int twist;

        [InputControl(layout = "Integer")]
        public int inputId;

        public override PointerInput ReadValue(ref InputBindingCompositeContext context)
        {
            var contact = context.ReadValueAsButton(this.contact);
            var pointerId = context.ReadValue<int>(inputId);
            var pressure = context.ReadValue<float>(this.pressure);
            var radius = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.radius);
            var tilt = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.tilt);
            var position = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.position);
            var twist = context.ReadValue<float>(this.twist);


            return new PointerInput
            {
                Contact = contact,
                InputId = pointerId,
                Position = position,
                Tilt = tilt != default ? tilt : (Vector2?)null,
                Pressure = pressure > 0 ? pressure : (float?)null,
                Radius = radius.sqrMagnitude > 0 ? radius : (Vector2?)null,
                Twist = twist > 0 ? twist : (float?)null,
            };
        }

        #if UNITY_EDITOR
        static PointerInputComposite()
        {
            Register();
        }

        #endif

        [RuntimeInitializeOnLoadMethod]
        private static void Register()
        {
            InputSystem.RegisterBindingComposite<PointerInputComposite>();
        }
    }
}


and this is my code for my custom composite:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.InputSystem.Layouts;
#if UNITY_EDITOR
using UnityEditor;
#endif


public struct myPointerInput
{
    public bool Press;
    public int PressCount;
    public Vector2 Position;
    public Vector2 Delta;
    public Vector2? Radius;

    //touch specific
    public bool? Tap;
    //public UnityEngine.InputSystem.TouchPhase? phase;
    public Vector2? StartPosition;
    public float? StartTime;

    //mouse Specific
    public Vector2? Scroll;
}




#if UNITY_EDITOR
[InitializeOnLoad]
#endif
public class MyPointerInput : InputBindingComposite<myPointerInput>
{
    [InputControl(layout = "Button")]
    public int press;
    [InputControl(layout = "Integer")]
    public int pressCount;
    [InputControl(layout = "Vector2")]
    public int position;
    [InputControl(layout = "Vector2")]
    public int delta;
    [InputControl(layout = "Vector2")]
    public int radius;

    //touch specific
    [InputControl(layout = "Button")]
    public int tap;
    //[InputControl(layout = "TouchPhase")]
    //public UnityEngine.InputSystem.TouchPhase phase;
    [InputControl(layout = "Vector2")]
    public int startPosition;
    [InputControl(layout = "Double")]
    public int startTime;

    //mouse Specific
    [InputControl(layout = "Vector2")]
    public int scroll;



    public override myPointerInput ReadValue(ref InputBindingCompositeContext context)
    {
        var press = context.ReadValueAsButton(this.press);
        var pressCount = context.ReadValue<int>(this.pressCount);
        var position = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.position);
        var delta = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.delta);
        var radius = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.radius);

        var tap = context.ReadValueAsButton(this.tap);
        //var phase = context.ReadValue<UnityEngine.InputSystem.TouchPhase>(this.phase);
        var startPosition = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.startPosition);
        var startTime = context.ReadValue<float>(this.startTime);

        var scroll = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.scroll);



        return new myPointerInput
        {
            Press = press,
            PressCount = pressCount,
            Position = position,
            Delta = delta,
            Radius = radius.sqrMagnitude > 0 ? radius : (Vector2?)null,
            //Tap = tap,
            StartPosition = startPosition,
            StartTime = startTime,
            Scroll = scroll
        };
    }

#if UNITY_EDITOR
    static MyPointerInput()
    {
        Register();
    }
#endif

    [RuntimeInitializeOnLoadMethod]
    private static void Register()
    {
        InputSystem.RegisterBindingComposite<myPointerInput>();
    }


}


these are the errors in Text form:

ArgumentException: Don't know how to convert PrimitiveValue to 'Object'
Parameter name: type
UnityEngine.InputSystem.Utilities.PrimitiveValue.ConvertTo (System.TypeCode type) (at Library/PackageCache/[email protected]/InputSystem/Utilities/PrimitiveValue.cs:227)
UnityEngine.InputSystem.Utilities.NamedValue.ConvertTo (System.TypeCode type) (at Library/PackageCache/[email protected]/InputSystem/Utilities/NamedValue.cs:28)
UnityEngine.InputSystem.Editor.Lists.ParameterListView.Initialize (System.Type registeredType, UnityEngine.InputSystem.Utilities.ReadOnlyArray`1[TValue] existingParameters) (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/ParameterListView.cs:146)
UnityEngine.InputSystem.Editor.InputBindingPropertiesView.InitializeCompositeProperties () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputBindingPropertiesView.cs:194)
UnityEngine.InputSystem.Editor.InputBindingPropertiesView.DrawGeneralProperties () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputBindingPropertiesView.cs:69)
UnityEngine.InputSystem.Editor.PropertiesViewBase.DrawGeneralGroup () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/PropertiesViewBase.cs:59)
UnityEngine.InputSystem.Editor.PropertiesViewBase.OnGUI () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/PropertiesViewBase.cs:39)
UnityEngine.InputSystem.Editor.InputActionEditorWindow.DrawPropertiesColumn (System.Single width) (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputActionEditorWindow.cs:699)
UnityEngine.InputSystem.Editor.InputActionEditorWindow.OnGUI () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputActionEditorWindow.cs:637)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <437ba245d8404784b9fbab9b439ac908>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.HostView.Invoke (System.String methodName) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.DockArea.OldOnGUI () (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)  
GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced.
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)  
  ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
Aborting
UnityEngine.GUILayoutGroup.GetNext () (at <817eebdd70f8402280b9cb11fff8b976>:0)
UnityEngine.GUILayoutUtility.DoGetRect (System.Single minWidth, System.Single maxWidth, System.Single minHeight, System.Single maxHeight, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at <817eebdd70f8402280b9cb11fff8b976>:0)
UnityEngine.GUILayoutUtility.GetRect (System.Single minWidth, System.Single maxWidth, System.Single minHeight, System.Single maxHeight, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at <817eebdd70f8402280b9cb11fff8b976>:0)
UnityEditor.EditorGUILayout.GetControlRect (System.Boolean hasLabel, System.Single height, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.EditorGUILayout.Popup (UnityEngine.GUIContent label, System.Int32 selectedIndex, UnityEngine.GUIContent[] displayedOptions, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.EditorGUILayout.Popup (UnityEngine.GUIContent label, System.Int32 selectedIndex, UnityEngine.GUIContent[] displayedOptions, UnityEngine.GUILayoutOption[] options) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEngine.InputSystem.Editor.InputBindingPropertiesView.DrawGeneralProperties () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputBindingPropertiesView.cs:72)
UnityEngine.InputSystem.Editor.PropertiesViewBase.DrawGeneralGroup () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/PropertiesViewBase.cs:59)
UnityEngine.InputSystem.Editor.PropertiesViewBase.OnGUI () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/PropertiesViewBase.cs:39)
UnityEngine.InputSystem.Editor.InputActionEditorWindow.DrawPropertiesColumn (System.Single width) (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputActionEditorWindow.cs:699)
UnityEngine.InputSystem.Editor.InputActionEditorWindow.OnGUI () (at Library/PackageCache/[email protected]/InputSystem/Editor/AssetEditor/InputActionEditorWindow.cs:637)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <437ba245d8404784b9fbab9b439ac908>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.HostView.Invoke (System.String methodName) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEditor.DockArea.OldOnGUI () (at <78f1ad0f25c84e3ca853e639f50d95f5>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.IMGUIContainer.DoIMGUIRepaint () (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.UIR.RenderChainCommand.ExecuteNonDrawMesh (UnityEngine.UIElements.UIR.DrawParams drawParams, System.Boolean straightY, System.Single pixelsPerPoint, System.Exception& immediateException) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
Rethrow as ImmediateModeException
UnityEngine.UIElements.UIR.RenderChain.Render (UnityEngine.Rect viewport, UnityEngine.Matrix4x4 projection, UnityEngine.UIElements.PanelClearFlags clearFlags) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.UIRRepaintUpdater.DrawChain (UnityEngine.Rect viewport, UnityEngine.Matrix4x4 projection) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.UIRRepaintUpdater.Update () (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.Panel.UpdateForRepaint () (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.Panel.Repaint (UnityEngine.Event e) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <3736d7cfd1f845a5922f99478cb469e7>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <817eebdd70f8402280b9cb11fff8b976>:0)
\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

I still don't know what's wrong with my script, but I wrote a identical one in another project and it worked like a charm.

Here is the new script:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.InputSystem.Layouts;
#if UNITY_EDITOR
using UnityEditor;
#endif


public struct myCompositData
{
    public bool Press;
    public int PressCount;
    public Vector2 Position;
    public Vector2 Delta;
    public Vector2 Radius;

    public bool Tap;
    //public UnityEngine.InputSystem.TouchPhase Phase;
    public Vector2 StartPosition;
    public double StartTime;

    public Vector2 Scroll;
}


#if UNITY_EDITOR
[InitializeOnLoad]
#endif
public class myComposit : InputBindingComposite<myCompositData>
{


#if UNITY_EDITOR
    static myComposit()
    {
        Initialize();
    }
#endif


    [RuntimeInitializeOnLoadMethod]
    private static void Initialize()
    {
        InputSystem.RegisterBindingComposite<myComposit>();
    }

    [InputControl(layout = "Button")]
    public int press;
    [InputControl(layout = "Integer")]
    public int pressCount;
    [InputControl(layout = "Vector2")]
    public int position;
    [InputControl(layout = "Vector2")]
    public int delta;
    [InputControl(layout = "Vector2")]
    public int radius;

    [InputControl(layout = "Button")]
    public int tap;
    //[InputControl(layout = "TouchPhase")]
    //public int phase;
    [InputControl(layout = "Vector2")]
    public int startPosition;
    [InputControl(layout = "Double")]
    public int startTime;

    [InputControl(layout = "Vector2")]
    public int scroll;



    public override myCompositData ReadValue(ref InputBindingCompositeContext context)
    {

        var press = context.ReadValueAsButton(this.press);
        var pressCount = context.ReadValue<int>(this.pressCount);
        var position = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.position);
        var delta = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.delta);
        var radius = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.radius);

        var tap = context.ReadValueAsButton(this.tap);
        //var phase = context.ReadValue<UnityEngine.InputSystem.TouchPhase>(this.phase);
        var startPosition = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.startPosition);
        var startTime = context.ReadValue<double>(this.startTime);

        var scroll = context.ReadValue<Vector2, Vector2MagnitudeComparer>(this.scroll);

        return new myCompositData()
        {
            Press = press,
            PressCount = pressCount,
            Position = position,
            Delta = delta,
            Radius = radius,

            Tap = tap,
            StartPosition = startPosition,
            StartTime = startTime,

            Scroll = scroll

        };
    }
}

If anyone could compare the two scripts and determine what I did wrong that would be great.

\$\endgroup\$
4
  • \$\begingroup\$ If you paste your new script in place of the old one, does it work in your old project? \$\endgroup\$
    – DMGregory
    Commented Jun 4, 2020 at 21:05
  • \$\begingroup\$ unfortunately, I had deleted the old project, however, when I paste the old script in place of the new one I still get the same errors. \$\endgroup\$
    – Venay
    Commented Jun 4, 2020 at 21:10
  • \$\begingroup\$ Also, I noticed that the input system is overall kind of unstable. for example, when I deleted the old script from the old project there were a lot of errors that I could not get rid of. that's why I had to make a new project. Unfortunately, I did not screen capture what the errors were. \$\endgroup\$
    – Venay
    Commented Jun 4, 2020 at 21:16
  • \$\begingroup\$ Don't forget to mark your answer as accepted if it worked for you. \$\endgroup\$
    – DMGregory
    Commented Jan 17, 2022 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .