LoginSignup
18
14

More than 5 years have passed since last update.

UnityでAndroidのバックボタン対応を1行で解決する

Last updated at Posted at 2018-01-06

やりたいこと

AndroidのBackボタン(左向きの三角ボタン)を押した時にアプリを終了させたい時の話です。

Daydreamでは品質要件にて実装がすべきと明確に決まっています。
Daydream App Quality Requirements - Functionality  |  Google VR  |  Google Developers

方法

今まではこういうノリのコードを書いていたのですが、

test.cs
void Update()
{
    if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape))
    {
        Application.Quit();
        return;
     }
}

実は一行で対応できることを知りました。

solution.cs
Input.backButtonLeavesApp = true;

Unity - Scripting API: Input.backButtonLeavesApp

Daydreamは「X」ボタンとバックボタンの両方がこれ1行で対応できました。

18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14