Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Vector3Control.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.LowLevel;
  3. using UnityEngine.InputSystem.Utilities;
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// A floating-point 3D vector control composed of three <see cref="AxisControl">AxisControls</see>.
  8. /// </summary>
  9. public class Vector3Control : InputControl<Vector3>
  10. {
  11. [InputControl(offset = 0, displayName = "X")]
  12. public AxisControl x { get; set; }
  13. [InputControl(offset = 4, displayName = "Y")]
  14. public AxisControl y { get; set; }
  15. [InputControl(offset = 8, displayName = "Z")]
  16. public AxisControl z { get; set; }
  17. public Vector3Control()
  18. {
  19. m_StateBlock.format = InputStateBlock.FormatVector3;
  20. }
  21. protected override void FinishSetup()
  22. {
  23. x = GetChildControl<AxisControl>("x");
  24. y = GetChildControl<AxisControl>("y");
  25. z = GetChildControl<AxisControl>("z");
  26. base.FinishSetup();
  27. }
  28. public override unsafe Vector3 ReadUnprocessedValueFromState(void* statePtr)
  29. {
  30. switch (m_OptimizedControlDataType)
  31. {
  32. case InputStateBlock.kFormatVector3:
  33. return *(Vector3*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
  34. default:
  35. return new Vector3(
  36. x.ReadUnprocessedValueFromStateWithCaching(statePtr),
  37. y.ReadUnprocessedValueFromStateWithCaching(statePtr),
  38. z.ReadUnprocessedValueFromStateWithCaching(statePtr));
  39. }
  40. }
  41. public override unsafe void WriteValueIntoState(Vector3 value, void* statePtr)
  42. {
  43. switch (m_OptimizedControlDataType)
  44. {
  45. case InputStateBlock.kFormatVector3:
  46. *(Vector3*)((byte*)statePtr + (int)m_StateBlock.byteOffset) = value;
  47. break;
  48. default:
  49. x.WriteValueIntoState(value.x, statePtr);
  50. y.WriteValueIntoState(value.y, statePtr);
  51. z.WriteValueIntoState(value.z, statePtr);
  52. break;
  53. }
  54. }
  55. public override unsafe float EvaluateMagnitude(void* statePtr)
  56. {
  57. ////REVIEW: this can go beyond 1; that okay?
  58. return ReadValueFromStateWithCaching(statePtr).magnitude;
  59. }
  60. protected override FourCC CalculateOptimizedControlDataType()
  61. {
  62. if (
  63. m_StateBlock.sizeInBits == sizeof(float) * 3 * 8 &&
  64. m_StateBlock.bitOffset == 0 &&
  65. x.optimizedControlDataType == InputStateBlock.FormatFloat &&
  66. y.optimizedControlDataType == InputStateBlock.FormatFloat &&
  67. z.optimizedControlDataType == InputStateBlock.FormatFloat &&
  68. y.m_StateBlock.byteOffset == x.m_StateBlock.byteOffset + 4 &&
  69. z.m_StateBlock.byteOffset == x.m_StateBlock.byteOffset + 8
  70. )
  71. return InputStateBlock.FormatVector3;
  72. return InputStateBlock.FormatInvalid;
  73. }
  74. }
  75. }