使用右箭头键更新相机位置

时间:2018-12-12 13:35:59

标签: c# visual-studio unity3d unity-container unityscript

你好,我在下面的代码中,我正在使用UI Buttons更新我的相机位置,这很正常,我想要执行相同的过程,但是通过按向右箭头键,例如,如果我按向右箭头键,相机会将其位置更改为指向然后停在A位置,当我再次按相同的箭头键时,相机会将其位置更改为B点,因为在代码中,我在不同的ui按钮上调用了不同的功能,所以谢谢,这是我的代码

002 TOWER NO. 7 UNIWORLD GARDEN SEC. 47 SOWA ROAD GURGAON Haryana 122001 India
002 TOWER NO. 7 UNIWORLD GARDEN SECTOR-47 SONA ROAD GURGAON Haryana 122001 India
09;SHIVALIK BUNGLAOW; ANANDNAGAR CROSS ROAD; NEAR MADHUR HALL;SATELLITE; 
AHMEDABAD Gujarat 380015 India
1 DEEPALI; PITAMPURA DELHI Delhi 110034 India
10; BRIGHTON TOWERS; CROSS ROAD NO.2; LOKHANDWALA COMPLEX; ANDHERI WEST MUMBAI Maharashtra 400053 India
100 Vaishali; Pitampura Delhi Delhi 110034 India
100 Vaishali; Pitampura; DELHI Delhi 110034 India

1 个答案:

答案 0 :(得分:0)

据我了解,您有一个普通的UI.Button组件,现在想在某个键盘键上执行与onClick中的此按钮相同的操作。

解决方案1:扩展按钮

通过在按钮对象上的Button组件旁边放置以下组件,我将简单地在获得某个onClick之后调用Button的KeyCode事件(相机

using UnityEngine;

// make sure you are not accidentely using 
// UnityEngine.Experimental.UIElements.Button
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public class KeyboardButton : MonoBehaviour
{
    // Which key should this Button react to?
    // Select this in the inspector for each Button
    public KeyCode ReactToKey;
    private Button _button;

    private void Awake()
    {
        _button = GetComponent<Button>();
    }

    // Wait for the defined key
    private void Update()
    {
        // If key not pressed do nothing
        if (!Input.GetKeyDown(ReactToKey)) return;

        // This simply tells the button to execute it's onClick event
        // So it does exactly the same as if you would have clicked it in the UI
        _button.onClick.Invoke();
    }
}

解决方案2:装回按钮

或者,如果您完全不想使用Button,则可以添加自己的UnityEvent,例如OnPress改为上述脚本

using UnityEngine;
using UnityEngine.Events;

public class KeyboardButton : MonoBehaviour
{
    // Which key should this Button react to?
    // Select this in the inspector for each Button
    public KeyCode ReactToKey;

    // reference the target methods here just as 
    // you would do with the Button's onClick
    public UnityEvent OnPress;

    // Wait for the defined key
    private void Update()
    {
        // If key not pressed do nothing
        if (!Input.GetKeyDown(ReactToKey)) return;

        OnPress.Invoke();
    }
}