日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Input 輸入

 有趣的永 2015-12-24
Desktop

Unity supports keyboard, joystick and gamepad input.

Unity支持,鍵盤,操縱桿和游戲手柄輸入。

Virtual axes and buttons can be created in the Input Manager, and end users can configure Keyboard input in a nice screen configuration dialog.

輸入管理器可以創(chuàng)建虛擬軸和按鈕,并終端用戶可以在屏幕配置對話框配置鍵盤輸入。

輸入管理器

You can setup joysticks, gamepads, keyboard, and mouse, then access them all through one simple scripting interface.

用戶可以設(shè)置操縱桿,游戲手柄,鍵盤和鼠標(biāo),然后可以通過簡單的腳本接口訪問它們。

From scripts, all virtual axes are accessed by their name.

從腳本,所有虛擬軸通過它們的名字來訪問。

Every project has the following default input axes when it's created:

當(dāng)創(chuàng)建時,每個項目都具有下面的默認(rèn)輸入軸:

  • Horizontal and Vertical are mapped to w, a, s, d and the arrow keys.
    水平和垂直被映射到w, a, s, d鍵和方向鍵
  • Fire1, Fire2, Fire3 are mapped to Control, Option (Alt), and Command, respectively.
    Fire1, Fire2, Fire3被分別映射到Ctrl,Option(Alt)和Command鍵
  • Mouse X and Mouse Y are mapped to the delta of mouse movement.
    Mouse X 和 Mouse Y被映射到鼠標(biāo)移動增量
  • Window Shake X and Window Shake Y is mapped to the movement of the window.
    Window Shake X 和 Window Shake Y 被映射到窗口的移動

Adding new Input Axes 添加新的輸入軸

If you want to add new virtual axes go to the Edit->Project Settings->Input menu. Here you can also change the settings of each axis.

如果想添加新的虛擬軸,選擇菜單Edit->Project Settings->Input menu。這里可以改變每個軸的設(shè)置。

You map each axis to two buttons on a joystick, mouse, or keyboard keys.

映射每個軸到操縱桿、鼠標(biāo)或鍵盤鍵的兩個按鈕。

  • Name 名稱
    The name of the string used to check this axis from a script.
    名稱用于從腳本使用這個軸
  • Descriptive Name 描述名稱
    Positive value name displayed in the input tab of the configuration dialog for standalone builds.
    正值名稱顯示于配置對話框的輸入標(biāo)簽,用于獨立版。
  • Descriptive Negative Name
    描述負(fù)名稱
    Negative value name displayed in the Input tab of the Configuration dialog for standalone builds.
    負(fù)值名稱顯示于配置對話框的Input標(biāo)簽,用于獨立版。
  • Negative Button 負(fù)按鈕
    The button used to push the axis in the negative direction.
    該按鈕用于在負(fù)方向移動軸
  • Positive Button 正按鈕
    The button used to push the axis in the positive direction.
    該按鈕用于在正方向移動軸
  • Alt Negative Button
    備選負(fù)按鈕
    Alternative button used to push the axis in the negative direction.
    備選按鈕用于在負(fù)方向移動軸
  • Alt Positive Button
    備選正按鈕
    Alternative button used to push the axis in the positive direction.
    備選按鈕用于在正方向移動軸
  • Gravity 重力
    Speed in units per second that the axis falls toward neutral when no buttons are pressed.
    當(dāng)沒有按鈕被按下,在單位每秒速度,軸下降到0。
  • Dead 死亡
    Size of the analog dead zone. All analog device values within this range result map to neutral.
    模擬的死區(qū)大小。 設(shè)定范圍內(nèi)所有模擬設(shè)備的值為0。
  • Sensitivity 靈敏度
    Speed in units per second that the the axis will move toward the target value. This is for digital devices only.
    在單位每秒速度,軸將移向目標(biāo)值。這近用于數(shù)碼設(shè)備。
  • Snap 捕捉
    If enabled, the axis value will reset to zero when pressing a button of the opposite direction.
    如果啟用,當(dāng)按下相反方向的按鈕,該軸值將重設(shè)為0。
  • Invert 反向
    If enabled, the Negative Buttons provide a positive value, and vice-versa.
    如果啟用,負(fù)按鈕將提供一個正值,反之亦然。
  • Type 類型
    The type of inputs that will control this axis. 控制軸的輸入設(shè)備類型
  • Axis 軸
    The axis of a connected device that will control this axis. 連接設(shè)備的軸將控制這個軸
  • Joy Num 操作桿
    The connected Joystick that will control this axis. 連接操縱桿將控制這個軸

Use these settings to fine tune the look and feel of input. They are all documented with tooltips in the Editor as well.

使用這些設(shè)置來進行效果和輸入的感覺微調(diào)。它們在編輯器中都帶有提示。

Using Input Axes from Scripts 從腳本使用輸入軸

You can query the current state from a script like this:

從像這樣的腳本,可以查詢當(dāng)前狀態(tài):

value = Input.GetAxis ("Horizontal");

An axis has a value between -1 and 1. The neutral position is 0. This is the case for joystick input and keyboard input.

一個軸具有-1和1之間的值。中間位置為0。這種情況用于操縱桿輸入和鍵盤輸入。

However, Mouse Delta and Window Shake Delta are how much the mouse or window moved during the last frame. This means it can be larger than 1 or smaller than -1 when the user moves the mouse quickly.

然而,鼠標(biāo)增量和Window Shake增量是鼠標(biāo)或窗口從上一幀到現(xiàn)在的移動。這意思是當(dāng)用戶快速移動鼠標(biāo)時,它可能大于1或小于-1。

It is possible to create multiple axes with the same name. When getting the input axis, the axis with the largest absolute value will be returned. This makes it possible to assign more than one input device to one axis name. For example, create one axis for keyboard input and one axis for joystick input with the same name. If the user is using the joystick, input will come from the joystick, otherwise input will come from the keyboard. This way you don't have to consider where the input comes from when writing scripts.

它可以創(chuàng)建具有相同名稱的多個軸。當(dāng)獲取輸入軸,該軸帶有最大絕對值將被返回。這可以使得分配多個輸入設(shè)備為一個軸名。例如,創(chuàng)建一個軸為鍵盤輸入,一個軸為操縱桿輸入,具有相同的名稱。如果用戶使用操縱桿,輸入將來自操縱桿,否則輸入將來自鍵盤。這樣,當(dāng)寫腳本時就不必考慮輸入來自什么設(shè)備了。

Button Names 按鈕名稱

To map a key to an axis, you have to enter the key's name in the Positive Button or Negative Button property in the Inspector.

要映射一個鍵到一個軸,在檢視面板的正按鈕或負(fù)按鈕的屬性中,必須輸入鍵的名稱。

The names of keys follow this convention:

輸入鍵的名稱如下:

  • Normal keys: "a", "b", "c" ...
    標(biāo)準(zhǔn)鍵:
  • Number keys: "1", "2", "3", ...
    數(shù)字鍵:
  • Arrow keys: "up", "down", "left", "right"
    方向鍵:
  • Keypad keys: "[1]", "[2]", "[3]", "[+]", "[equals]"
    小鍵盤鍵:
  • Modifier keys: "right shift", "left shift", "right ctrl", "left ctrl", "right alt", "left alt", "right cmd", "left cmd"
    修改鍵:
  • Mouse Buttons: "mouse 0", "mouse 1", "mouse 2", ...
    鼠標(biāo)按鈕:
  • Joystick Buttons (from any joystick): "joystick button 0", "joystick button 1", "joystick button 2", ...
    操縱桿按鈕(從任意操縱桿):
  • Joystick Buttons (from a specific joystick): "joystick button 0", "joystick button 1", "joystick 1 button 0", ...
    操縱桿按鈕(從指定操縱桿):
  • Special keys: "backspace", "tab", "return", "escape", "space", "delete", "enter", "insert", "home", "end", "page up", "page down"
    特殊鍵:
  • Function keys: "f1", "f2", "f3", ...
    功能鍵:

The names used to identify the keys are the same in the scripting interface and the Inspector.

在腳本和檢視面板,用來識別鍵的名稱是一樣的。

value = Input.GetKey ("a");

Mobile Input 移動設(shè)備輸入

On iOS and Android, the Input class offers access to touchscreen, accelerometer and geographical/location input.

在 iOS/Android Input類提供觸摸屏、加速度計和地理/位置輸入。

Access to keyboard on mobile devices is provided via the iOS keyboard.

訪問鍵盤是通過iOS keyboard。

Multi-Touch Screen 多點觸控屏幕

The iPhone and iPod Touch devices are capable of tracking up to five fingers touching the screen simultaneously. You can retrieve the status of each finger touching the screen during the last frame by accessing the Input.touches property array.

iPhone和iPod觸控設(shè)備是具有五個手指觸控屏幕同時跟蹤能力。通過訪問Input.touches屬性數(shù)值,可以取回每個手指觸摸屏幕在最后一幀的狀態(tài)。

Android

Android devices don't have a unified limit on how many fingers they track. Instead, it varies from device to device and can be anything from two-touch on older devices to five fingers on some newer devices.

Android設(shè)備,能跟蹤多少個手指沒有統(tǒng)一的限制。相反,在不同的設(shè)備上可能是不同的,從老設(shè)備的兩指觸控,到新設(shè)備的五指觸控。

Each finger touch is represented by an Input.Touch data structure:

每個手指觸控是通過Input.touches數(shù)據(jù)結(jié)構(gòu)描述的:

  • fingerId 手指索引
    The unique index for a touch. 觸摸的唯一索引。
  • position 位置
    The screen position of the touch. 觸摸屏幕的位置。
  • deltaPosition 增量位置
    The screen position change since the last frame.
    自最后一幀改變的屏幕位置。
  • deltaTime 增量時間
    Amount of time that has passed since the last state change.
    從最后狀態(tài)改變到現(xiàn)在經(jīng)過的時間
  • tapCount 點擊數(shù)
    The iPhone/iPad screen is able to distinguish quick finger taps by the user. This counter will let you know how many times the user has tapped the screen without moving a finger to the sides. Android devices do not count number of taps, this field is always 1.
    iPhone/iPad屏幕能夠識別用過的快速點擊, 這個計數(shù)器讓你知道用戶點擊屏幕多少次,而不是移動手指。android設(shè)備不對點擊計數(shù),這個方法總是返回1
  • phase 相位(狀態(tài))
    Describes so called "phase" or the state of the touch. It can help you determine if the touch just began, if user moved the finger or if he just lifted the finger.
    描述觸摸的狀態(tài),可以幫助開發(fā)者確定用戶是否剛開始觸摸,是否用戶移動手指,是否用戶剛剛抬起手指。

Phase can be one of the following:

相位(狀態(tài))可能是下列之一:

  • Began 開始
    A finger just touched the screen. 手指剛剛觸摸屏幕
  • Moved 移動
    A finger moved on the screen. 手指在屏幕上移動
  • Stationary 靜止
    A finger is touching the screen but hasn't moved since the last frame.
    手指觸摸屏幕,但是自最后一幀沒有移動
  • Ended 結(jié)束
    A finger was lifted from the screen. This is the final phase of a touch.
    手指從屏幕上抬起,這是觸控的最后狀態(tài)。
  • Canceled 取消
    The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch.
    系統(tǒng)取消了觸控跟蹤,如,用戶將設(shè)備放在了臉上或超過同時超過5個觸摸點。這是觸控的最后狀態(tài)。

Following is an example script which will shoot a ray whenever the user taps on the screen:

下面的例子,只要用戶在屏幕上點擊,將射出一條光線:

var particle : GameObject;
function Update () {
	for (var touch : Touch in Input.touches) {
		if (touch.phase == TouchPhase.Began) {
			// Construct a ray from the current touch coordinates
			//從當(dāng)前的觸摸坐標(biāo)建一條光線
			var ray = Camera.main.ScreenPointToRay (touch.position);
			if (Physics.Raycast (ray)) {
				// Create a particle if hit
				//如果觸摸就創(chuàng)建一個例子
				Instantiate (particle, transform.position, transform.rotation);
			}
		}
	}
}

Mouse Simulation 鼠標(biāo)模擬

On top of native touch support Unity iOS/Android provides a mouse simulation. You can use mouse functionality from the standard Input class.

除了原有的觸控之外,Unity iOS/Android還提供了鼠標(biāo)模擬。從標(biāo)準(zhǔn)的Input類中,可以使用鼠標(biāo)功能。

Accelerometer 加速度傳感器

As the mobile device moves, a built-in accelerometer reports linear acceleration changes along the three primary axes in three-dimensional space. 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.

在手機設(shè)備移動時,內(nèi)置加速度傳感器報告在三維空間,沿著三個主要軸上的線性加速度改變。沿著每個軸的加速度由硬件作為G-force值直接報告。值為1表示沿給定軸增加1g負(fù)載,值為-1.0表示減少1g。如果用戶在面前豎直拿著設(shè)備(home按鈕在底部),X軸指向右,Y軸指向上,Z軸指向前。

You can retrieve the accelerometer value by accessing the Input.acceleration property.

可以通過訪問Input.acceleration屬性,取回加速度傳感器的值。

The following is an example script which will move an object using the accelerometer:

下面的例子,將使用加速度傳感器移動一個物體:

var speed = 10.0;
function Update () {
	var dir : Vector3 = Vector3.zero;

	// we assume that the device is held parallel to the ground
	// and the Home button is in the right hand
	//假設(shè)該設(shè)備與地面平行,Home按鈕在右邊
	// remap the device acceleration axis to game coordinates:
	//重新映射設(shè)備加速度軸到游戲坐標(biāo):
	//  1) XY plane of the device is mapped onto XZ plane
	//		設(shè)備的XY平面映射到XZ平面
	//  2) rotated 90 degrees around Y axis
	//		沿著Y軸旋轉(zhuǎn)90度
	dir.x = -Input.acceleration.y;
	dir.z = Input.acceleration.x;

	// clamp acceleration vector to the unit sphere
	//鉗制加速度向量到單位球
	if (dir.sqrMagnitude > 1)
		dir.Normalize();

	// Make it move 10 meters per second instead of 10 meters per frame...
	//使它每秒移動10米,而不是每幀10米
	dir *= Time.deltaTime;

	// Move object 移動物體
	transform.Translate (dir * speed);
}

Low-Pass Filter 低通過濾器

Accelerometer readings can be jerky and noisy. Applying low-pass filtering on the signal allows you to smooth it and get rid of high frequency noise.

加速度傳感器數(shù)值可能被顛簸和噪音影響。應(yīng)用低通過濾器在信號上可以是它平滑,并擺脫高頻噪音的干擾。

The following script shows you how to apply low-pass filtering to accelerometer readings:

下面的例子顯示,怎樣應(yīng)用低通到加速度傳感器數(shù)值:

var AccelerometerUpdateInterval : float = 1.0 / 60.0;
var LowPassKernelWidthInSeconds : float = 1.0;

private var LowPassFilterFactor : float = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
private var lowPassValue : Vector3 = Vector3.zero;
function Start () {
	lowPassValue = Input.acceleration;
}

function LowPassFilterAccelerometer() : Vector3 {
	lowPassValue = Mathf.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
	return lowPassValue;
}

The greater the value of LowPassKernelWidthInSeconds, the slower the filtered value will converge towards the current input sample (and vice versa). You should be able to use the LowPassFilter() function instead of avgSamples().

LowPassKernelWidthInSeconds的值越大, 被過濾值將匯集當(dāng)前輸入采樣越慢,反之亦然。應(yīng)該使用LowPassFilter()函數(shù),而不是avgSamples()。

I'd like as much precision as possible when reading the accelerometer. What should I do?
我想盡可能的讀取加速度傳感器的更高精度,應(yīng)該怎樣做呢?

Reading the Input.acceleration variable does not equal sampling the hardware. Put simply, Unity samples the hardware at a frequency of 60Hz and stores the result into the variable. In reality, things are a little bit more complicated -- accelerometer sampling doesn't occur at consistent time intervals, if under significant CPU loads. As a result, the system might report 2 samples during one frame, then 1 sample during the next frame.

讀取 Input.acceleration變量,不等于從硬件采樣。簡而言之,Unity硬件樣品頻率為60Hz并儲存結(jié)果到變量。在現(xiàn)實中,是一個有點復(fù)雜的東西--加速度傳感器采樣不會發(fā)生在一致的時間間隔,如果大量的CPU負(fù)荷的情況下,因此,系統(tǒng)可能會報告一幀中的2個采樣,那么1個采樣在下一幀。

You can access all measurements executed by accelerometer during the frame. The following code will illustrate a simple average of all the accelerometer events that were collected within the last frame:

通過加速度傳感器在幀間可以訪問所有測量。

var period : float = 0.0;
var acc : Vector3 = Vector3.zero;
for (var evnt : iPhoneAccelerationEvent  in iPhoneInput.accelerationEvents) {
	acc += evnt.acceleration * evnt.deltaTime;
	period += evnt.deltaTime;
}
if (period > 0)
	acc *= 1.0/period;
return acc;

Further Reading 深入閱讀

The Unity mobile input API is originally based on Apple's API. It may help to learn more about the native API to better understand Unity's Input API. You can find the Apple input API documentation here:

Unity移動Input API本來是基于Apple's API。這可以幫助你學(xué)習(xí)更多關(guān)于原生API,更好的理解Untiy的Input API。在這里找到Apple的 Input API文檔:

Note: The above links reference your locally installed iPhone SDK Reference Documentation and will contain native ObjectiveC code. It is not necessary to understand these documents for using Unity on mobile devices, but may be helpful to some!

注意:上面的鏈接參考你本地安裝的iPHone SDK參考文檔,也將包含ObjectiveC代碼。對于在移動設(shè)備使用Unity開發(fā),沒有必要理解這些文檔,但是可能或許有些一下幫助。

iOS

Device geographical location 設(shè)備地理位置

Device geographical location can be obtained via the iPhoneInput.lastLocation property. Before calling this property you should start location service updates using iPhoneSettings.StartLocationServiceUpdates() and check the service status via iPhoneSettings.locationServiceStatus. See the scripting reference for details.

設(shè)備地理位置可以通過iPhoneInput.lastLocation屬性獲得。在調(diào)用此屬性之前,應(yīng)該使用iPhoneSettings.StartLocationServiceUpdates()先開始位置服務(wù)的更新并通過iPhoneSettings.locationServiceStatus檢查服務(wù)器狀態(tài)。

參見腳本更多細節(jié)。

頁面最近更新:2013-04-26

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多