C# Input

Input 输入

输入系统的接口

Input.acceleration 加速度

上一次测量的设备在三维空间中的线性加速度

public float speed = 10.0F;
void Update() {
    Vector3 dir = Vector3.zero;
    dir.x = -Input.acceleration.y;
    dir.z = Input.acceleration.x;
    if (dir.sqrMagnitude > 1)
    dir.Normalize();
    dir *= Time.deltaTime;
    transform.Translate(dir * speed);
}

Input.accelerationEventCount 加速度事件次数

上一帧发生的所测量的的加速度次数

 void Update() {
     if (Input.accelerationEventCount > 0)
         print("We got new acceleration measurements");
}

Input.accelerationEvents 加速度事件列表

返回上一帧测量的加速度数据列表

void Update() {
    Vector3 acceleration = Vector3.zero;
    foreach (AccelerationEvent accEvent in Input.accelerationEvents) {
        acceleration += accEvent.acceleration * accEvent.deltaTime;
    }
    print(acceleration);
}

Input.anyKey 任意键

当前是否有任意键或鼠标键被按下

void Update() {
    if (Input.anyKey)
        Debug.Log("A key or mouse click has been detected");
}

Input.anyKeyDown 任意键按下

在用户按下任意按键或鼠标按键首帧返回true

void Update() {
    if (Input.anyKeyDown)
        Debug.Log("A key or mouse click has been detected");
}

Input.compass 指南针

访问指南针的属性

public static Compass compass;

Input.compensateSensors 补偿传感器

该属性控制输入传感器在屏幕方向的补偿

public static bool compensateSensors;

Input.compositionCursorPos 输入法组合鼠标位置

当前文本输入位置,使用于IME来打开窗口

public static Vector2 compositionCursorPos;

Input.compositionString 输入法组合字符串

当前用户正在输入的IME组合字符串

public static string compositionString;

Input.deviceOrientation 设备方向

由操作系统所报告的设备的物理方向

 void Update() {
     if (Input.deviceOrientation == DeviceOrientation.FaceDown)
         audio.Play();
}

Input.GetAccelerationEvent 获取加速度事件

返回上一帧发生的指定的加速度测量

void Update() {
    Vector3 acceleration = Vector3.zero;
    int i = 0;
    while (i < Input.accelerationEventCount) {
        AccelerationEvent accEvent = Input.GetAccelerationEvent(i);
        acceleration += accEvent.acceleration * accEvent.deltaTime;
        ++i;
    }
    print(acceleration);
}

Input.GetAxis 获取轴

根据axisName名称返回虚拟输入轴中的值

public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update() {
    float translation = Input.GetAxis("Vertical") * speed;
    float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    translation *= Time.deltaTime;
    rotation *= Time.deltaTime;
    transform.Translate(0, 0, translation);
    transform.Rotate(0, rotation, 0);
}

Input.GetAxisRaw 获取原始轴

通过axisName名称返回一个不使用平滑滤波器的虚拟轴值

void Update() {
    float speed = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
    transform.Rotate(0, speed, 0);
}

Input.GetButton 获取按钮

当由'buttonname”确定的虚拟按键被按下时,返回true

public GameObject projectile;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Update() {
    if (Input.GetButton("Fire1") && Time.time > nextFire) {
        nextFire = Time.time + fireRate;
        GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
    }
}

Input.GetButtonDown 获取按钮按下

在用户按下由buttonName名称确定的虚拟按钮的那一帧返回true

public GameObject projectile;
void Update() {
    if (Input.GetButtonDown("Fire1"))
        Instantiate(projectile, transform.position, transform.rotation) as GameObject;
}

Input.GetButtonUp 获取按钮弹起

在用户释放根据buttonName名称的虚拟按钮时返回true

public GameObject projectile;
void Update() {
    if (Input.GetButtonUp("Fire1"))
        Instantiate(projectile, transform.position, transform.rotation) as GameObject;
}

Input.GetJoystickNames 获取摇杆名称列表

返回一个用来描述已连接的摇杆的字符串数组

void Update() {
    int i = 0;
    while (i < 4) {
        if (Mathf.Abs(Input.GetAxis("Joy" + i + "X")) > 0.2F || Mathf.Abs(Input.GetAxis("Joy" + i + "Y")) > 0.2F)
            Debug.Log(Input.GetJoystickNames()[i] + " is moved");
        i++;
    }
}

Input.GetKey 获取键

当用户按下由name名称确定的按键时,然后true

void Update() {
    if (Input.GetKey("up"))
        print("up arrow key is held down");
    if (Input.GetKey("down"))
        print("down arrow key is held down");
}

Input.GetKeyDown 获取键按下

当用户按下指定名称的按键时的那一帧返回true

void Update() {
    if (Input.GetKeyDown("space"))
        print("space key was pressed");
}

Input.GetKeyUp 获取键弹起

在用户释放给定名字的按键的那一帧返回true

void Update() {
    if (Input.GetKeyUp("space"))
        print("space key was released");
}

Input.GetMouseButton 获取鼠标按钮

当指定的鼠标按钮被按下时返回true

void 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 获取鼠标按钮按下

在用户按下指定鼠标按键的那一帧返回true

void 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 获取鼠标按钮弹起

在用户释放指定鼠标按键的那一帧返回true

void 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.");
}

Input.GetTouch 获取触摸

返回一个存放触摸信息的对象

public float speed = 0.1F;
void Update() {
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
        Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
    }
}

Input.gyro 陀螺仪

返回默认的陀螺仪

public static Gyroscope gyro;

Input.imeCompositionMode IME组合方式

控制IME输入组合的启用和禁用

public static IMECompositionMode imeCompositionMode;

Input.imeIsSelected IME是否已选择

用户是否已选择IME键盘输入源

public static bool imeIsSelected;

Input.inputString 输入字符串

返回在这一帧的键盘输入

public GUIText gt;
void Start() {
    gt = GetComponent<GUIText>();
}
void Update() {
    foreach (char c in Input.inputString) {
        if (c == "\b"[0])
            if (gt.text.Length != 0)
                gt.text = gt.text.Substring(0, gt.text.Length - 1);
        else
            if (c == "\n"[0] || c == "\r"[0])
                print("User entered his name: " + gt.text);
            else
                gt.text += c;
        }
}

Input.IsJoystickPreconfigured 是否是预配置摇杆

如果该摇杆已经配置返回true,否则返回false

public static bool IsJoystickPreconfigured(string joystickName);

Input.location 位置

用于访问设备位置信息

public static LocationService location;

Input.mousePosition 鼠标位置

在屏幕坐标空间当前鼠标的位置

public GameObject particle;
void Update() {
    if (Input.GetButtonDown("Fire1")) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray))
            Instantiate(particle, transform.position, transform.rotation) as GameObject;
    }
}

Input.multiTouchEnabled 启用多点触控

指示系统是否启用多点触控

public static bool multiTouchEnabled;

Input.ResetInputAxes 重置输入轴

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

void Example() {
    Input.ResetInputAxes();
}

Input.simulateMouseWithTouches 鼠标模拟触摸

启用/禁用鼠标模拟触摸。默认情况下启用此项

public static bool simulateMouseWithTouches;

Input.touchCount 触摸个数

触摸的数量。每一帧之内都一定不会改变

void Update() {
    if (Input.touchCount > 0)
        print(Input.touchCount);
}

Input.touches 触摸列表

返回代表上一帧所有的触摸状态的对象列表

void Update() {
    int fingerCount = 0;
    foreach (Touch touch in Input.touches) {
        if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
            fingerCount++;
    }
    if (fingerCount > 0)
        print("User has " + fingerCount + " finger(s) touching the screen");
}

Input.touchSupported 触摸支持

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

public static bool touchSupported;
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值