a标签获取input值_unity 中文API之Input

Input 输入

abbff84b4bc3904f46b46605ef04366e.png

Input

Namespace: UnityEngine

Description 描述

Interface into the Input system.

输入系统的接口。

Use this class to read the axes set up in the Input Manager, and to access multi-touch/accelerometer data on mobile devices.

使用这个类能够读取输入管理器设置的按键,以及访问移动设备的多点触控或加速感应数据。

To read an axis use Input.GetAxis with one of the following default axes: “Horizontal” and “Vertical” are mapped to joystick, A, W, S, D and the arrow keys. “Mouse X” and “Mouse Y” are mapped to the mouse delta. “Fire1”, “Fire2” “Fire3” are mapped to Ctrl, Alt, Cmd keys and three mouse or joystick buttons. New input axes can be added in the Input Manager.

想要读取轴向使用Input.GetAxis方法获取下列默认轴: “Horizontal” 和“Vertical” 映射于控制杆、A、W、S、D和箭头键(方向键)。 “Mouse X” 和“Mouse Y” 映射于鼠标,“Fire1”, “Fire2” “Fire3”映射于键盘的Ctrl、Alt、Cmd键和鼠标中键或控制器的按钮。新的输入设置可以使用输入管理器来添加。

If you are using input for any kind of movement behaviour use Input.GetAxis. It gives you smoothed and configurable input that can be mapped to keyboard, joystick or mouse. Use Input.GetButton for action like events only. Don't use it for movement, Input.GetAxis will make the script code smaller and simpler.

如果你使用Input制作某种运动行为可以使用Input.GetAxis方法,它能够返回来自键盘、控制器或鼠标平缓并且可以设置的输入结果。使用Input.GetButton方法只用于像事件之类的动作。不要将它用于移动动作。Input.GetAxis方法可以使脚本代码更简洁。

Note also that the Input flags are not reset until “Update()”, so its suggested you make all the Input Calls in the Update Loop.

注意:每次输入在“Update()“之前不会再更新,所以建议你将所有的输入调用都写在Update方法中。(Update循环中)

Mobile Devices: 移动设备

iOS and Android devices are capable of tracking multiple fingers touching the screen simultaneously. You can access data on the status of each finger touching screen during the last frame by accessing the Input.touches property array.

iOS和Android设备能够支持多点触控。你可以通过Input.touches属性集合访问在最近一帧中触摸在屏幕上的每一根手指的状态数据。

As a device moves, its accelerometer hardware reports linear acceleration changes along the three primary axes in three-dimensional space. You can use this data to detect both the current orientation of the device (relative to the ground) and any immediate changes to that orientation.

当设备移动时,它们的加速感应器硬件将报告它们在三维空间中沿着三个主轴的线性加速变化数据。你可以使用这些数据检测设备当前的移动方向(相对于地面)和突然间的方向改 变。

Acceleration along each axis is reported directly by the hardware as G-force values. A value of 1.0 represents a load of about +1g along a given axis while a value of -1.0 represents -1g. If you hold the device upright (with the home button at the bottom) in front of you, the X axis is positive along the right, the Y axis is positive directly up, and the Z axis is positive pointing toward you.

硬件沿着某感应一轴加速就会立即返回重力值。如果值为1.0代表沿着给定轴的方向+1g的重力加速度,如果值为-1.0代表-1g的重力加速度。如果你保持设备垂直(主页键在下方)在你正前方,那么X轴就是指向你右侧的方向,Y轴指向正上方,Z轴就是你所面向的方向。

You can read the Input.acceleration property to get the accelerometer reading. You can also use the Input.deviceOrientation property to get a discrete evaluation of the device's orientation in three-dimensional space. Detecting a change in orientation can be useful if you want to create game behaviors when the user rotates the device to hold it differently.

你可以读取Input.acceleration属性获得设备的加速度信息。你也可以使用Input.deviceOrientation属性获取设备在三维空间中的方位偏移。检测方位变化在你想要制作游戏行为中会非常有用,当用户转动设备或拿着设备时它是不同的。

Note that the accelerometer hardware can be polled more than once per frame. To access all accelerometer samples since the last frame, you can read the Input.accelerationEvents property array. This can be useful when reconstructing player motions, feeding acceleration data into a predictor, or implementing other precise motion analysis.

注意:速度感应装置在每一帧中能够轮询多次,想访问上一帧的所有速度样本你可以读取Input.accelerationEvents属性集合。这在重组玩家动作中会非常有用。例如将加速数据放入一个预测器中或者实现其他一些精确的动作捕捉。

Static Variables 静态变量

acceleration Last measured linear acceleration of a device in three-dimensional space. (Read Only)

上一次测量的设备在三维空间中的线性加速度(只读)。 accelerationEventCount Number of acceleration measurements which occurred during last frame.

上一帧发生的所测量的的加速度次数。 accelerationEvents Returns list of acceleration measurements which occurred during the last frame. (Read Only) (Allocates temporary variables).

返回上一帧测量的加速度数据列表(只读)(分配临时变量) anyKey Is any key or mouse button currently held down? (Read Only)

当前是否有任意键或鼠标键被按下(只读)? anyKeyDown Returns true the first frame the user hits any key or mouse button. (Read Only)

在用户按下任意按键或鼠标按键首帧返回true(只读)。 compass Property for accessing compass (handheld devices only). (Read Only)

访问指南针的属性(仅手持设备)。 compensateSensors This property controls if input sensors should be compensated for screen orientation.

该属性控制输入传感器在屏幕方向的补偿。 compositionCursorPos The current text input position used by IMEs to open windows.

当前文本输入位置,使用于IME来打开窗口。 compositionString The current IME composition string being typed by the user.

当前用户正在输入的IME组合字符串。 deviceOrientation Device physical orientation as reported by OS. (Read Only)

由操作系统所报告的设备的物理方向(只读)。 gyro Returns default gyroscope.

返回默认的陀螺仪。 imeCompositionMode Controls enabling and disabling of IME input composition.

控制IME输入组合的启用和禁用。 imeIsSelected Does the user have an IME keyboard input source selected?

用户是否已选择IME键盘输入源? inputString Returns the keyboard input entered this frame. (Read Only)

返回在这一帧的键盘输入(只读) location Property for accessing device location (handheld devices only). (Read Only)

用于访问设备位置信息(仅手持设备)(只读)。 mousePosition The current mouse position in pixel coordinates. (Read Only)

在屏幕坐标空间当前鼠标的位置(只读)。 mouseScrollDelta The current mouse scroll delta. (Read Only)

当前鼠标滚动增量(只读)。 multiTouchEnabled Property indicating whether the system handles multiple touches.

指示系统是否启用多点触控。 simulateMouseWithTouches Enables/Disables mouse simulation with touches. By default this option is enabled.

启用/禁用鼠标模拟触摸。默认情况下启用此项。 touchCount Number of touches. Guaranteed not to change throughout the frame. (Read Only)

触摸的数量。每一帧之内都一定不会改变(只读) touches Returns list of objects representing status of all touches during last frame. (Read Only) (Allocates temporary variables).

返回代表上一帧所有的触摸状态的对象列表(只读)(分配临时变量) touchSupported Returns whether the device on which application is currently running supports touch input.

返回设备是否在当前运行的应用程序支持触摸输入。

Static Functions 静态函数

GetAccelerationEvent Returns specific acceleration measurement which occurred during last frame. (Does not allocate temporary variables).

返回上一帧发生的指定的加速度测量(不允许分配临时变量)。 GetAxis Returns the value of the virtual axis identified by axisName.

根据axisName名称返回虚拟输入轴中的值。 GetAxisRaw Returns the value of the virtual axis identified by axisName with no smoothing filtering applied.

通过axisName名称返回一个不使用平滑滤波器的虚拟轴值。 GetButton Returns true while the virtual button identified by buttonName is held down.

当由'buttonname”确定的虚拟按键被按下时,返回true。 GetButtonDown Returns true during the frame the user pressed down the virtual button identified by buttonName.

在用户按下由buttonName名称确定的虚拟按钮的那一帧返回true。 GetButtonUp Returns true the first frame the user releases the virtual button identified by buttonName.

在用户释放根据buttonName名称的虚拟按钮时返回true。 GetJoystickNames Returns an array of strings describing the connected joysticks.

返回一个用来描述已连接的摇杆的字符串数组。 GetKey Returns true while the user holds down the key identified by name. Think auto fire.

当用户按下由name名称确定的按键时,然后true。想想自动开火。 GetKeyDown Returns true during the frame the user starts pressing down the key identified by name.

当用户按下指定名称的按键时的那一帧返回true。 GetKeyUp Returns true during the frame the user releases the key identified by name.

在用户释放给定名字的按键的那一帧返回true。 GetMouseButton Returns whether the given mouse button is held down.

当指定的鼠标按钮被按下时返回true。 GetMouseButtonDown Returns true during the frame the user pressed the given mouse button.

在用户按下指定鼠标按键的那一帧返回true。 GetMouseButtonUp Returns true during the frame the user releases the given mouse button.

在用户释放指定鼠标按键的那一帧返回true。 GetTouch Returns object representing status of a specific touch. (Does not allocate temporary variables).

返回一个存放触摸信息的对象(不允许分配临时变量)。 IsJoystickPreconfigured Determine whether a particular joystick model has been preconfigured by Unity. (Linux-only).

确定Unity是否一个特定的操纵杆模型已预先配置。(仅Linux)。 ResetInputAxes Resets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.

在一帧中重置所有的输入,重置输入指令之后所有的方向轴都被设置为0并且所有的按键都被设置为0。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,可以通过重写派生自UnityEngine.Windows.WindowsWindow类的自定义窗口脚本的WndProc方法来处理Windows消息。通过重写WndProc方法,您可以获取和处理各种Windows消息,包括WM_INPUT消息。 下面是一个示例代码,展示了如何在Unity中处理WM_INPUT消息: ```csharp using UnityEngine; using System; using System.Runtime.InteropServices; public class RawInputExample : MonoBehaviour { const int WM_INPUT = 0x00FF; protected override void WndProc(ref Message message) { if (message.Msg == WM_INPUT) { IntPtr hRawInput = message.LParam; uint dataSize = 0; // 获取原始输入数据的大小 GetRawInputData(hRawInput, RID_INPUT, IntPtr.Zero, ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))); if (dataSize > 0) { IntPtr data = Marshal.AllocHGlobal((int)dataSize); // 获取原始输入数据 if (GetRawInputData(hRawInput, RID_INPUT, data, ref dataSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) == dataSize) { RAWINPUT rawInput = Marshal.PtrToStructure<RAWINPUT>(data); // 在这里处理原始输入数据 // 示例:检查输入设备类型是否为鼠标 if (rawInput.header.dwType == RIM_TYPEMOUSE) { RAWMOUSE mouseData = rawInput.mouse; // 在这里处理鼠标输入数据 int deltaX = mouseData.lLastX; int deltaY = mouseData.lLastY; Debug.Log("Mouse Delta: " + deltaX + ", " + deltaY); } } Marshal.FreeHGlobal(data); } base.WndProc(ref message); return; } base.WndProc(ref message); } [StructLayout(LayoutKind.Sequential)] struct RAWINPUTHEADER { public uint dwType; public uint dwSize; public IntPtr hDevice; public IntPtr wParam; } [StructLayout(LayoutKind.Explicit)] struct RAWINPUT { [FieldOffset(0)] public RAWINPUTHEADER header; [FieldOffset(16)] public RAWMOUSE mouse; // 其他输入设备类型的数据结构 } [StructLayout(LayoutKind.Sequential)] struct RAWMOUSE { public ushort usFlags; public uint ulButtons; public uint ulRawButtons; public int lLastX; public int lLastY; public uint ulExtraInformation; } const uint RIDEV_INPUTSINK = 0x00000100; const int RIM_TYPEMOUSE = 0x00000000; const int RID_INPUT = 0x10000003; [DllImport("user32.dll")] static extern uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader); [DllImport("user32.dll")] static extern bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevices, uint uiNumDevices, uint cbSize); [StructLayout(LayoutKind.Sequential)] struct RAWINPUTDEVICE { public ushort usUsagePage; public ushort usUsage; public uint dwFlags; public IntPtr hwndTarget; } void Start() { RAWINPUTDEVICE[] rawInputDevices = new RAWINPUTDEVICE[1]; rawInputDevices[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC rawInputDevices[0].usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE rawInputDevices[0].dwFlags = RIDEV_INPUTSINK; rawInputDevices[0].hwndTarget = this.Handle; if (!RegisterRawInputDevices(rawInputDevices, (uint)rawInputDevices.Length, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICE)))) { Debug.LogError("Failed to register raw input devices"); } } } ``` 上述示例代码中,重写了WndProc方法来处理Windows消息。在WndProc方法中,检查消息是否为WM_INPUT消息,并获取原始输入数据进行处理。示例代码中处理了鼠标输入数据,您可以根据实际需求进行相应的处理。 请注意,由于涉及到Windows API的调用,需要将上述代码放置在派生自UnityEngine.Windows.WindowsWindow类的自定义窗口脚本中,并确保脚本被正确添加到场景中。此外,还需要在Unity项目的Player Settings中启用"Allow 'unsafe' Code"选项。 希望这可以帮助您处理WM_INPUT消息并获取原始输入数据。如果有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值