Unity3D:扩展设备模拟器

推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生

扩展设备模拟器

设备模拟器支持插件来扩展其功能并在模拟器视图中更改控制面板的 UI。

创建插件

若要创建设备模拟器插件,请扩展设备模拟器插件类。

若要将 UI 插入设备模拟器视图,插件必须:

  • 重写该属性以返回非空字符串。title
  • 重写该方法以返回包含 UI 的可视元素。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";
    }
}

此文由3D建模学习工作室整理翻译,转载请注明出处!

上一篇:Unity3D:添加设备 (mvrlink.com)

下一篇:Unity3D:扩展设备模拟器 (mvrlink.com)

NSDT场景编辑器 | NSDT 数字孪生 | GLTF在线编辑器 | 3D模型在线转换 | UnrealSynth虚幻合成数据生成器 | 3D模型自动纹理化工具
2023 power by nsdt©鄂ICP备2023000829号