1. 按钮 PressableButton
(1)导入预制件:Mixed Reality > Toolkit > Toolbox
(2)在 Inspector > Interactable > Events > Onclick() 设置点击后运行的函数文件;
在 Inspector > Button Config Helper (Script) > Labels > MainLabelText 设置按钮文字; Enable See it / Say it Label 可以关闭浮动窗口;
在 Inspector > Icon 中选择标签图标;
2. 滑块 PinchSlider
导入预制件:位置为 Packages > Mixed Reality Toolkit Foundation > SDK > Features > UX > Prefabs > Sliders > PinchSlider.
可配置的属性:
- Slider Value: 当前滑块的值。
- Use Slider Step Divisions / Step Divisions: 设置步长的划分数量。
- Slider Axis: 设置滑块的轴向(X, Y, Z)。
- Slider Start/End Distance: 设置滑块的最小值和最大值。
- On Value Updated: 添加回调函数,响应滑块值的变化。以下为代码:
using Microsoft.MixedReality.Toolkit.UI;
using TMPro;
using UnityEngine;
public class SliderValueChange : MonoBehaviour
{
public PinchSlider pinchSlider;
public TextMeshPro textMesh = null;
public void OnSliderUpdatedNew(SliderEventData eventData)
{
if (textMesh == null)
{
textMesh = GetComponent<TextMeshPro>();
}
if (textMesh != null)
{
textMesh.text = $"{eventData.NewValue.ToString("F4")}";
}
}
}
使用举例:
using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Examples.Demos
{
public class SliderChangeColor : MonoBehaviour
{
[SerializeField]
private Renderer TargetRenderer;
public void OnSliderUpdatedRed(SliderEventData eventData)
{
TargetRenderer = GetComponentInChildren<Renderer>();
if ((TargetRenderer != null) && (TargetRenderer.material != null))
{
TargetRenderer.material.color = new Color(eventData.NewValue, TargetRenderer.sharedMaterial.color.g, TargetRenderer.sharedMaterial.color.b);
}
}
public void OnSliderUpdatedGreen(SliderEventData eventData)
{
TargetRenderer = GetComponentInChildren<Renderer>();
if ((TargetRenderer != null) && (TargetRenderer.material != null))
{
TargetRenderer.material.color = new Color(TargetRenderer.sharedMaterial.color.r, eventData.NewValue, TargetRenderer.sharedMaterial.color.b);
}
}
public void OnSliderUpdateBlue(SliderEventData eventData)
{
TargetRenderer = GetComponentInChildren<Renderer>();
if ((TargetRenderer != null) && (TargetRenderer.material != null))
{
TargetRenderer.material.color = new Color(TargetRenderer.sharedMaterial.color.r, TargetRenderer.sharedMaterial.color.g, eventData.NewValue);
}
}
}
}
滑块公开以下事件:
- OnValueUpdated - 每当滑块值更改时调用
- OnInteractionStarted - 当用户抓取滑块时调用
- OnInteractionEnded - 当用户释放滑块时调用
- OnHoverEntered - 当用户的手/控制器悬停在滑块上时调用,使用近交互或远交互。
- OnHoverExited - 当用户的手/控制器不再靠近滑块时调用。
示例场景: MRTK/Examples/Demos/UX/Slider/Scenes/
下的 SliderExample 场景
3. 系统键盘 System Keyboard
public TouchScreenKeyboard keyboard;
public void OpenSystemKeyboard()
{
keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, false);
}
private void Update()
{
if (keyboard != null)
{
keyboardText = keyboard.text;
}
}