0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unity Standalone Playerのクラッシュダイアログを回避する

Posted at

はじめに

Unityのスタンドアロンプレーヤーがクラッシュすると、以下のようなダイアログが表示されます。

Screenshot1.png

アプリを使用する状況によっては、このダイアログを表示したくない場合があるでしょう。
この記事では、このダイアログを回避する方法と、その有効性の検証を行います。

前提

  • この記事のコードは、以下の環境で検証しました。
  • Unity 2021.3.12f1
  • Windows 11

回避方法

-silent-crashesというコマンドライン引数は、エディタのオプションとしては記載がありますが、スタンドアロンプレーヤーのオプションとしては記載されていません。

このオプションが、スタンドアロンプレーヤーでも使用できるみたいです。

有効性の検証

下記のコードを以下のようなUnityEngine.UI.Textなオブジェクトにアタッチしました。

Hierarchy
🧊Canvas
 🧊Text (Legacy)
CrashTest.cs
using UnityEngine;
using UnityEngine.UI;

public class CrashTest : MonoBehaviour {

    private const float TimeLimit = 5.0f;
    private Text infomation;
    private float timeLeft;

    private void Start () {
        infomation = GetComponentInChildren<Text> ();
        timeLeft = TimeLimit;
    }

    private void Update () {
        if (timeLeft > 0f) {
            timeLeft -= Time.deltaTime;
            if (timeLeft > 0f) {
                infomation.text = $"{timeLeft:F5}";
            } else {
                infomation.text = "crashed";
                // 表示のために次のフレームまで待つ
                Invoke ("Crash", 1/60f);
            }
        }
    }

    private void Crash () {
#if UNITY_EDITOR
        // エディタ自体がクラッシュするので、単に再生を終了
        UnityEditor.EditorApplication.isPlaying = false;
#else
        // ビルド後は強制クラッシュ
        UnityEngine.Diagnostics.Utils.ForceCrash (UnityEngine.Diagnostics.ForcedCrashCategory.Abort);
#endif
    }

}

以上のプロジェクトを、Windows Standalone Playeとしてビルドし、以下のコマンドで実行しました。

> WindowsPlayerCrashTest.exe

こちらは、ダイアログが表示されます。

> WindowsPlayerCrashTest.exe -silent-crashes

こちらは、ダイアログが表示されなくなりました。

おわりに

なぜ、エディタのコマンドライン引数としてしか記載されていないのかは謎ですが、スタンドアロンプレーヤーでも有効なようです。

この検証は、Windowsでしか行われておらず、MacやLinuxでも同様になるかは確認していません。
他の環境での結果や、この手法に関する公式情報をご存じでしたら、コメントをお寄せいただけると助かります。

最後までお読みいただきありがとうございました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?