ドキュメント

Unity Engine

Unity Engine

デバイスシミュレーターの拡張

デバイスシミュレータープラグインを作成して、シミュレータービューの機能とユーザーインターフェースを拡張する方法を説明しています。
読み終わるまでの所要時間 3 分最終更新 22日前

デバイスシミュレーターは、機能を拡張し、シミュレータービューのコントロールパネルの UI を変更するプラグインをサポートしています。

プラグインの作成

デバイスシミュレータープラグインを作成するには、DeviceSimulatorPlugin クラスを拡張します。
デバイスシミュレータービューに UI を挿入するには、プラグインで以下を行う必要があります。
  • title
    プロパティをオーバーライドして、空ではない文字列を返します。
  • UI を含む VisualElement を返すように
    OnCreateUI
    メソッドをオーバーライドします。
プラグインがこれらの条件を満たさない場合、デバイスシミュレーターはプラグインをインスタンス化しますが、シミュレータービューにその UI はディスプレイされません。
以下の例は、 title プロパティをオーバーライドし、シミュレータービューに UI を追加するプラグインの作成方法を示しています。
public class TouchInfoPlugin : DeviceSimulatorPlugin{ public override string title => "Touch Info"; private ラベル m_TouchCountLabel; private Label m_LastTouchEvent; private ボタン m_ResetCountButton; [SerializeField] private int m_TouchCount = 0; public override void OnCreate() { deviceSimulator.touchScreenInput += touchEvent => { m_TouchCount += 1; UpdateTouchCounterText(); m_LastTouchEvent.text = $"Last touch event: {touchEvent.phase.ToString()}"; }; } public override VisualElement OnCreateUI() { VisualElement root = new VisualElement(); m_LastTouchEvent = new Label("Last touch event:なし m_TouchCountLabel = new Label(); UpdateTouchCounterText(); m_ResetCountButton = new Button {text = "Reset Count" }; m_ResetCountButton.clicked += () => { m_TouchCount = 0; UpdateTouchCounterText(); }; root.Add(m_LastTouchEvent); root.Add(m_TouchCountLabel); root.Add(m_ResetCountButton); return root; } private void UpdateTouchCounterText() { if (m_TouchCount > 0) m_TouchCountLabel.text = $"Touches recorded: {m_TouchCount}"; else m_TouchCountLabel.text = "No taps recorded"; }}