Unity3d 輸入設備相關指令 ( 滑鼠 鍵盤 搖桿 ) 龍華科技大學多媒體與遊戲發展科學系林志勇編輯請勿外流轉載, 上傳網路 2013/12/28
Mouse( 滑鼠 )
OnMouse(Down, Drag, Enter, Exit, Over, Up) 跟遊戲物件 (GameObject) 有關
OnMouseDown // Loads the level named "SomeLevel" as a response // to the user clicking on the object function OnMouseDown () { } Application.LoadLevel ("SomeLevel");
OnMouseDrag // Darken the material color while user holds down the mouse. function OnMouseDrag () { } renderer.material.color -= Color.white * Time.deltaTime;
OnMouseEnter // Attach this script to a mesh to make // it red when the mouse is over the mesh function OnMouseEnter () { renderer.material.color = Color.red; }
OnMouseExit // Assigns a white color to the material // attached to this mesh. function OnMouseExit () { renderer.material.color = Color.white; }
OnMouseOver // Fades the red component of the material to zero // while the mouse is over the mesh function OnMouseOver () { } renderer.material.color -= Color(0.1, 0, 0) * Time.deltaTime;
OnMouseUp // Register when mouse dragging has ended. OnMouseUp is called // when the mouse button is released. See Also: OnMouseDown, OnMouseDrag. function OnMouseUp () { } Debug.Log("Drag ended!");
Hierarchy Create Sphere 林志勇
OnMouse(Enter, Over, Exit, Down) 透過滑鼠不同動作 改變球的顏色 // Attach this script to a mesh to make // it red when the mouse is over the mesh function OnMouseEnter () { renderer.material.color = Color.red; } function OnMouseOver () { renderer.material.color -= Color(0.1, 0, 0) * Time.deltaTime; } function OnMouseExit () { renderer.material.color = Color.white; } function OnMouseDown () { renderer.material.color = Color.green; } 林志勇
Sphere OnMouse
OnMouseDrag.js var speed = 100; function OnMouseDrag () { transform.position += Vector3.right * Time.deltaTime*Input.GetAxis ("Mouse X") * speed; transform.position += Vector3.forward * Time.deltaTime*Input.GetAxis ("Mouse Y")* speed; } 林志勇
Cube OnMouseDrag 林志勇
JavaScript 與 VC# JavaScript VC#
Input
Input.GetMouse
Input.GetMouse(Button, ButtonDown, ButtonUp) 偵測滑鼠按鈕狀態 ( 跟遊戲物件無關 ) 林志勇
Input.GetMouseButton // Detects clicks from the mouse and prints a message // depending on the click detected. function Update() { if(input.getmousebutton(0)) Debug.Log("Pressed left click."); if(input.getmousebutton(1)) Debug.Log("Pressed right click."); if(input.getmousebutton(2)) Debug.Log("Pressed middle click."); } 林志勇
Input.GetMouseButtonDown // Detects clicks from the mouse and prints a message // depending on the click detected. function Update() { if(input.getmousebuttondown(0)) Debug.Log("Pressed left click."); if(input.getmousebuttondown(1)) Debug.Log("Pressed right click."); if(input.getmousebuttondown(2)) Debug.Log("Pressed middle click."); } 林志勇
Input.GetMouseButtonUp // Detects clicks from the mouse and prints a message // depending on the click detected. function Update() { if(input.getmousebuttonup(0)) Debug.Log("Pressed left click."); if(input.getmousebuttonup(1)) Debug.Log("Pressed right click."); if(input.getmousebuttonup(2)) Debug.Log("Pressed middle click."); } 林志勇
Hierarchy Create GUI Text 林志勇
GetMouse.js function Update() { } if(input.getmousebutton(0)){ guitext.text="pressed left click."; } if(input.getmousebutton(1)){ guitext.text="pressed right click."; } if(input.getmousebutton(2)){ guitext.text="pressed middle click."; } 林志勇
GUI Text GetMouse
MousePosition( 滑鼠位置 ) & RayCast
OnMouse.js Input.mousePosition var particle : GameObject; function Update () { if (Input.GetButtonDown ("Fire1")) { // Construct a ray from the current mouse coordinates var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray,hit,100)) { // Create a particle if hit Instantiate (particle, hit.point, transform.rotation); } } } 林志勇
Main Camera OnMouse Particle Flame
MouseSensor.js var target1: Transform; var target2: Transform; function Update () { if (Input.GetMouseButton(0)) { var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; if (Physics.Raycast(ray, hit)) { if (hit.transform == target1) { print("hit target 1"); } else if (hit.transform == target2) { print("hit target 2"); } } else { print("hit nothing"); } } } Lcy
Main Camera MouseSensor Target1 Cube Target2 Sphere
MouseScrollWheel.js Input.GetAxis("Mouse ScrollWheel") var teststring : String = ""; function OnGUI () { var wheelvalue = Input.GetAxis("Mouse ScrollWheel"); if (wheelvalue!= 0){ teststring = teststring + " " + wheelvalue; GUI.Label(Rect(0,0,320,480),testString); } transform.translate(0, wheelvalue*10,0); } 林志勇
Sphere MouseScrollWheel
Main Camerea MouseXY Lcy
MouseXY.js var mouse : Texture2D; var mouseps = Vector2.zero; function Update () { mouseps = Input.mousePosition; } function OnGUI () { Screen.showCursor = false; GUI.DrawTexture(Rect(mousePs.x,Screen.height-mousePs.y,25,25),mouse); gameobject.find("guitexth").guitext.text ="Screen.height:" +Screen.height.ToString(); gameobject.find("guitexty").guitext.text ="mouseps.y:" +mouseps.y.tostring(); var diffh; diffh=screen.height-mouseps.y; gameobject.find("guitexthy").guitext.text =diffh.tostring(); GUI.DrawTexture(Rect(0,100,25,25),mouse); } Lcy
MouseBMP Lcy
Input.GetButton, GetButtonUp, GetButtonDown
Input.GetButton, GetButtonUp, GetButtonDown
GameObject Create Empty Lcy 37
GameObject Lcy 38
Javascript Lcy 39
InputPress.js function Update () { if(input.getbuttonup("jump")){ Debug.Log("We Have Hit the Space Bar!"); } } Lcy 40
GameObject InputPress Lcy 41
Edit ProjectSettings Input Lcy 42
Name Jump Positive Button space Lcy 43
Input.anyKey, anykeydown 林志勇
Input.anyKey, anykeydown
Input.anyKey // 檢測是否有任何按鍵被按到 function Update() { } if(input.anykey) Debug.Log("A key or mouse click has been detected"); 林志勇
Input.anyKeyDown // 檢測是否有任何按鍵被按到 ( 按下一瞬間 ) function Update() { } if(input.anykeydown) Debug.Log("A key or mouse click has been detected"); 林志勇
inputstring 林志勇
inputstring // Shows how to read typing input from the keyboard // (eg. the user entering his name). // You need to attach this script to a GUIText object. function Update () { for (var c : char in Input.inputString) { // Backspace - Remove the last character if (c == "\b"[0]) { if (guitext.text.length!= 0) guitext.text = guitext.text.substring(0, guitext.text.length - 1); }else if (c == "\n"[0] c == "\r"[0]) { // End of entry // "\n" for Mac, "\r" for windows. print ("User entered his name: " + guitext.text); }else { // Normal text input - just append to the end guitext.text += c; } } } 林志勇
Input.GetKey(Down, Up)
Input.GetKey
Input.GetKey
Input 林志勇
Input 林志勇
Horizontal 林志勇
Vertical 林志勇
Fire1 林志勇
Jump 林志勇
Mouse X 林志勇
Mouse ScrollWheel 林志勇
Window Shake X 林志勇
Horizontal 林志勇
Fire1 林志勇
Jump 林志勇
Input.GetAxis, GetAxisRaw
Input.GetAxis
Input.GetAxis // A very simplistic car driving on the x-z plane. var speed : float = 10.0; var rotationspeed : float = 100.0; function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis ("Vertical") * speed; var rotation : float = Input.GetAxis ("Horizontal") * rotationspeed; // Make it move 10 meters per second instead of 10 meters per frame... translation *= Time.deltaTime; rotation *= Time.deltaTime; // Move translation along the object's z-axis transform.translate (0, 0, translation); // Rotate around our y-axis transform.rotate (0, rotation, 0); } 林志勇
Input.GetAxis
Input.GetAxis // Performs a mouse look. var horizontalspeed : float = 2.0; var verticalspeed : float = 2.0; function Update () { // Get the mouse delta. This is not in the range -1...1 var h : float = horizontalspeed * Input.GetAxis ("Mouse X"); var v : float = verticalspeed * Input.GetAxis ("Mouse Y"); transform.rotate (v, h, 0); }
Input.GetAxisRaw function Update () { } var speed : float = Input.GetAxisRaw("Horizontal") * Time.deltaTime; transform.rotate (0, speed, 0); 林志勇
Input Size 18 25 林志勇
Input Rename 林志勇
Input 林志勇
Input.GetJoystickNames // Prints a joystick name if movement is detected. function Update () { } // requires you to set up axes "Joy0X" - "Joy3X" and "Joy0Y" - "Joy3Y" in the Input Manger for (var i : int = 0; i < 4; i++) { } if (Mathf.Abs(Input.GetAxis("Joy"+i+"X")) > 0.2 Mathf.Abs(Input.GetAxis("Joy"+i+"Y")) > 0.2) Debug.Log (Input.GetJoystickNames()[i]+" is moved"); 林志勇
搖桿 (JoyStick)
讀取搖桿狀態函數 Input.GetAxis("X axis") Input.GetAxisRaw("X axis") Input.GetButton("joystick button 0") 無法區分不同搖桿按鈕 Input.GetKey(KeyCode.Joystick1Button0) 可區分不同搖桿按鈕
搖桿按鈕替代鍵碼 KeyCode.JoystickButton0
讀取搖桿旋轉軸數值 Input.GetAxis
Input.GetAxis ( Vertical ) ( 垂直軸 ) Input.GetAxis ( Horizontal )( 水平軸 ) // A very simplistic car driving on the x-z plane. var speed : float = 10.0; var rotationspeed : float = 100.0; function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis ("Vertical") * speed; var rotation : float = Input.GetAxis ("Horizontal") * rotationspeed; // Make it move 10 meters per second instead of 10 meters per frame... translation *= Time.deltaTime; rotation *= Time.deltaTime; // Move translation along the object's z-axis transform.translate (0, 0, translation); // Rotate around our y-axis transform.rotate (0, rotation, 0); }
讀取搖桿旋轉軸值及按鈕值
讀取搖桿旋轉軸值及按鈕值 var speed : float = 10.0; var rotationspeed : float = 10.0; function Update () { if(input.getkey(keycode.joystickbutton0)) { transform.rotate(5, 0, 0); print("up arrow key is held down"); } if(input.getkey("joystick button 1")) { transform.rotate(0, 5, 0); print("up arrow key is held down"); } if(input.getkey("joystick button 2")) { transform.rotate(0, 0, 5); print("up arrow key is held down"); } var translation : float = Input.GetAxis ("Vertical") * speed; var rotation : float = Input.GetAxis ("Horizontal") * rotationspeed; // Make it move 10 meters per second instead of 10 meters per frame... translation *= 0.1; rotation *= 0.01; // Move translation along the object's z-axis transform.translate (0, 0, translation); // Rotate around our y-axis transform.translate (rotation, 0, 0); }
範例程式 http://wiki.etc.cmu.edu/unity3d/index.php/joystick/controller
Arcade Stick for Sony Playstation 3
Microsoft Xbox 360 Controller
搖桿示意圖
測試程式介面
搖桿輸入裝置設定 Edit Project Settings Input
增加 Size 數量, 設定新增搖桿轉軸
測試程式
讀取搖桿按鈕值 Input.GetKey(KeyCode.Joystick4Button0)
讀取搖桿轉軸數值 Input.GetAxisRaw("J2-3")
無線搖桿控制台 裝置管理員 人性介面裝置
羅技搖桿測試程式搖桿 1,2,4 對應 Unity3d Joystick1,2,4( 無 Joystick 3)
Mode 亮 x, y Axis 5th, 6thAxis
Mode 不亮 5th, 6thAxis x, y Axis
Input.GetButton(Down, Up)
Input.GetButton
Unity3d IPhone
Android 手機 Bundle Identifier 設定
Bundle Identifier Bundle Identifier: Bundle Identifier 就是 package name Package Name 可自訂任何名字, 但業界有通用格式, 例如 : com.mycompanyname.mygame net.yourcompanyname.yourgame Bundle Identifier(Package Name) 在上架 Android Market 時, 很重要 因為同一個 PackageName 只能存在一個 Bundle Identifier(PackageName) 取 com.mywebsite.mygame, 但是已經有人取這個名字並已上架了,Android Market 就不會讓你用同樣的名字 簡單來說, 就是作為獨一的識別名字 Bundle Version: 備註程式第幾版本 Bundle Code: 備註程式是哪一個小版號
iphoneutils.vibrate(); 手機震動 // Press button to vibrate function OnGUI() { } if (GUI.Button(Rect(0, 10, 100, 32), "Vibrate!")) iphoneutils.vibrate();
Input.GetTouch Input.touchCount TouchPhase.Moved TouchPhase.Began
Touch
TouchPhase
Input.touchCount
Input.GetTouch
Input.acceleration
Screen.orientation
Input.deviceOrientation
Input.gyro
省電 // Disable screen dimming Screen.sleepTimeout = 0.0f;
http://unity3d.com/support/documentation/script Reference/Screen.html
// Start in landscape mode function Start () { Screen.orientation = ScreenOrientation.LandscapeLeft; }