デバイスシミュレーターの拡張
Explains how to create Device Simulator plugins to extend the functionality and user interface of the Simulator view.
読み終わるまでの所要時間 2 分最終更新 1ヶ月前
Device Simulator (デバイスシミュレーター) はプラグインに対応し、機能の拡張や、シミュレータービューのコントロールパネルの UI の変更を行います。
プラグインの作成
Device Simulator プラグインを作成するには、DeviceSimulatorPlugin クラスを拡張します。
デバイスシミュレータービューに UI を挿入するには、プラグインが以下を満たす必要があります。
- プロパティをオーバーライドして、空でない文字列を返すようにする。
title - メソッドをオーバーライドして、UI を含む VisualElement を返す。
OnCreateUI
これらの条件を満たさないプラグインは、デバイスシミュレーターでインスタンス化されますが、シミュレータービューに UI は表示されません。
次の例は、title プロパティをオーバーライドし、シミュレータービューに UI を追加するプラグインの作成方法を示しています。
public class TouchInfoPlugin : DeviceSimulatorPlugin{ public override string title => "Touch Info"; private Label m_TouchCountLabel; private Label m_LastTouchEvent; private Button 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: None"); 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"; }}