public static unsafe void WriteValueIntoState <TValue>(this InputControl <TValue> control, TValue value, void *statePtr) where TValue : struct { if (control == null) { throw new ArgumentNullException(nameof(control)); } if (statePtr == null) { throw new ArgumentNullException(nameof(statePtr)); } var valuePtr = UnsafeUtility.AddressOf(ref value); var valueSize = UnsafeUtility.SizeOf <TValue>(); control.WriteValueFromBufferIntoState(valuePtr, valueSize, statePtr); }
/// <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); } }