/// <summary> /// Read the current, processed value of the control and store it into the given memory buffer. /// </summary> /// <param name="buffer">Buffer to store value in. Note that the value is not stored with the offset /// found in <see cref="InputStateBlock.byteOffset"/> of the control's <see cref="InputControl.stateBlock"/>. It will /// be stored directly at the given address.</param> /// <param name="bufferSize">Size of the memory available at <paramref name="buffer"/> in bytes. Has to be /// at least <see cref="InputControl.valueSizeInBytes"/>. If the size is smaller, nothing will be written to the buffer.</param> /// <seealso cref="InputControl.valueSizeInBytes"/> /// <seealso cref="InputControl.valueType"/> /// <seealso cref="InputControl.ReadValueFromStateIntoBuffer"/> public static unsafe void ReadValueIntoBuffer(this InputControl control, void *buffer, int bufferSize) { if (control == null) { throw new ArgumentNullException(nameof(control)); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } control.ReadValueFromStateIntoBuffer(control.currentStatePtr, buffer, bufferSize); }
/// <summary> /// Write the control's current value into <paramref name="statePtr"/>. /// </summary> /// <param name="control">Control to read the current value from and to store state for in <paramref name="statePtr"/>.</param> /// <param name="statePtr">State to receive the control's value in its respective <see cref="InputControl.stateBlock"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="control"/> is null or <paramref name="statePtr"/> is null.</exception> /// <remarks> /// This method is equivalent to <see cref="InputControl{TValue}.WriteValueIntoState"/> except that one does /// not have to know the value type of the given control. /// </remarks> /// <exception cref="NotSupportedException">The control does not support writing. This is the case, for /// example, that compute values (such as the magnitude of a vector).</exception> /// <seealso cref="InputControl{TValue}.WriteValueIntoState"/> public static unsafe void WriteValueIntoState(this InputControl control, void *statePtr) { if (control == null) { throw new ArgumentNullException(nameof(control)); } if (statePtr == null) { throw new ArgumentNullException(nameof(statePtr)); } var valueSize = control.valueSizeInBytes; var valuePtr = UnsafeUtility.Malloc(valueSize, 8, Allocator.Temp); try { control.ReadValueFromStateIntoBuffer(control.currentStatePtr, valuePtr, valueSize); control.WriteValueFromBufferIntoState(valuePtr, valueSize, statePtr); } finally { UnsafeUtility.Free(valuePtr, Allocator.Temp); } }