文档

Unity Engine

Unity Engine

扩展 Device Simulator

Explains how to create Device Simulator plugins to extend the functionality and user interface of the Simulator view.
阅读时间1 分钟最后更新于 1 个月前

Device Simulator 支持用插件来扩展其功能,并在 Simulator 视图中更改控制面板的 UI。

创建插件

要创建 Device Simulator 插件,请扩展 DeviceSimulatorPlugin 类。
要将 UI 插入到 Device Simulator 视图中,插件必须:
  • 覆盖
    title
    属性以返回非空字符串。
  • 覆盖
    OnCreateUI
    方法以返回包含 UI 的 VisualElement。
如果插件不符合这些条件,Device Simulator 会实例化插件,但不会在 Simulator 视图中显示其 UI。
以下示例演示了如何创建覆盖游戏作品属性,并将 UI 添加到 Simulator 视图的插件。
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"; }}