テストのためのヒッチのシミュレーションを行う
ヒッチのシミュレーションを行い、パフォーマンスの問題による時間の変化をゲームでどのように処理するかをテストします。
読み終わるまでの所要時間 4 分最終更新 18日前
意図的なヒッチの導入は、遅延スパイク、フレームドロップ、スタッター、ストレス下での UI 応答性の低下などのパフォーマンスの問題をゲームがどのように処理するかをテストする一般的なテクニックです。
以下の簡単な MonoBehaviour スクリプトは、シーン内のすべてのゲームオブジェクトにアタッチ、設定、添付できます。メインスレッドを一定の間隔でハングさせ、フレーム時間のがたつきやスパイクをシミュレートします。テスト用にヒッチの頻度と継続時間を簡単に調整できます。
using UnityEngine;using Unity.Profiling;public class HitchSimulator :MonoBehaviour{ 静的読み取り専用 ProfilerMarker k_HitchSimulatorMarker = new ProfilerMarker("[HitchSimulator] CauseHitch() introducing artificial hitches"); [Tooltip("がたつきハイクを起こす頻度 (秒単位)] public float hitchInterval = 5f; [Tooltip("各がたつきが持続する時間 (ミリ秒単位)] public int hitchDurationMs = 200; private float _nextHitchTime = 0f; void Update() { if (Time.time >= _nextHitchTime) { Debug.LogWarning($"[HitchSimulator] Causing a hitch for {hitchDurationMs} ms at {Time.time:F2}s"); CauseHitch(hitchDurationMs); _nextHitchTime = Time.time + hitchInterval; } } void CauseHitch(int durationMs) { // Adding a clarifying profiler marker so no one accidentally confuses this for an actual issue. k_HitchSimulatorMarker.Auto(); float start = Time.realtimeSinceStartup; // Busy wait for the specified duration while ((Time.realtimeSinceStartup - start) < durationMs / 1000f) { // Just spin } }}
スクリプトを使用するには:
-
スクリプトをという名前の新しい C# ファイルにコピーします。
HitchSimulator.cs -
シーン内のアクティブなゲームオブジェクトにアタッチ、設定、添付します。
-
Inspector でパラメータを設定します。
- : がたつき頻度。デフォルトは 5 秒です。
hitchInterval - : それぞれのがたつきの長さデフォルトは 200 ミリ秒です。
hitchDurationMs
-
再生モードに入り、プロファイラーで意図的なフレームヒッチを観察したり、視覚的にスタッターを観察します。
追加リソース
- Unity プロファイラー:プロファイラーを使用して、ヒッチ中のゲームの動作を監視します。
- パフォーマンス最適化:Unity でパフォーマンスを最適化するための一般的なヒント。