Version: Unity 6.0 (6000.0)
语言 : 中文
添加设备
The Hierarchy window

扩展 Device Simulator

Device Simulator 支持用插件来扩展其功能,并在 Simulator 视图中更改控制面板的__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary

创建插件

要创建 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";
    }
}
添加设备
The Hierarchy window