【问题标题】:How to detect if unity game is running on (web keyboard) or (mobile touch)如何检测统一游戏是否在(网络键盘)或(移动触摸)上运行
【发布时间】:2017-07-07 01:12:20
【问题描述】:

基本上,我有一个由移动设备中的物理键盘和触摸屏支持的统一游戏。我已经完成了物理键盘的移动脚本,现在我正在编写触摸屏代码。

如何实现该检测功能?

我是这么想的……

private void HandleInput()
{
   if (detect if physical keyboard here...)
   {

        if  (Input.GetKey(KeyCode.RightArrow))
        {
            _normalizedHorizontalSpeed = 1;
        } 
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            _normalizedHorizontalSpeed = -1;
        }
   } else if (detect touch screen here...)
   {
       for (int i = 0; i < Input.touchCount; ++i)
       {
          if (Input.GetTouch(i).phase == TouchPhase.Began)
          {
             some code here...
          }
       }
   }
}

欣赏

【问题讨论】:

    标签: mobile unity5 unity3d-2dtools


    【解决方案1】:

    @ryemoss 提供的解决方案很棒,但检查将在 运行时 进行评估。如果你想避免每帧检查,我建议你使用Platform dependent compilation。由于预处理器指令,根据目标平台,只有所需的代码会被编译到您的应用程序中

    #if UNITY_IOS || UNITY_ANDROID || UNITY_WP_8_1
       for (int i = 0; i < Input.touchCount; ++i)
       {
          if (Input.GetTouch(i).phase == TouchPhase.Began)
          {
             some code here...
          }
       }
    #else
       if  (Input.GetKey(KeyCode.RightArrow))
        {
            _normalizedHorizontalSpeed = 1;
        } 
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            _normalizedHorizontalSpeed = -1;
        }
    #endif
    

    但是,请注意,如果您使用 Unity Remote,此方法会更难在编辑器中进行调试。

    【讨论】:

    • 感谢@Hellium 的帮助
    【解决方案2】:

    您正在寻找Application.platform。 类似以下内容应该可以实现您正在寻找的内容或阅读here 以获得更多设备。

    if (Application.platform == RuntimePlatform.WindowsPlayer)
        Debug.Log("Do something special here");
    else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        Debug.Log("Do something else here");
    

    但是,您最好不要一起检查此检查,因为它是多余的!如果您按右箭头或左箭头,则您已经知道用户正在使用键盘。

    【讨论】:

    • 感谢您的回答@ryemoss
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多