You are on page 1of 119

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 1825

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


EditProject SettingsInput

Size


Input.GetKey(KeyCode.Joystick4Button0)


Input.GetAxisRaw("J2-3")





1,2,4Unity3d 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 Identifierpackage namePackage 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;
}

You might also like