Hololens 对话框

作者:追风剑情 发布于:2023-1-16 16:07 分类:Unity3d

[官方文档] 对话框 - MRTK2

对话框标准资源

Packages/Mixed Reality Toolkit Foundation/SDK/Features/UX/Prefabs/Dialog

1111.png

2222.png

辅助类

using System;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.UI;
/// <summary>
/// 对话框辅助类
/// </summary>
public class DialogHelper : MonoBehaviour
{
    //路径: Packages/Mixed Reality Toolkit Foundation/SDK/Features/UX/Prefabs/Dialog/DialogLarge_192x192.prefab
    [SerializeField]
    [Tooltip("Assign DialogLarge_192x192.prefab")]
    private GameObject dialogPrefabLarge;

    [SerializeField]
    [Tooltip("Assign DialogMediume_192x128.prefab")]
    private GameObject dialogPrefabMedium;

    [SerializeField]
    [Tooltip("Assign DialogSmall_192x96.prefab")]
    private GameObject dialogPrefabSmall;

    private static DialogHelper Instance;
    private void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    // 对话框大小
    public enum DialogSize
    {
        Small,
        Medium,
        Large
    }

    // 对话框类型
    public enum DialogType
    {
        Confirmation,
        Choice
    }

    // 打开默认对话框
    public static void Open(string message)
    {
        Open(DialogSize.Small, DialogType.Confirmation, "Tips", message);
    }

    // 打开选择对话框
    public static void Choice(string message, Action<DialogResult> onClosed)
    {
        Open(DialogSize.Small, DialogType.Choice, "Tips", message, onClosed);
    }

    public static void Open(DialogSize dialogSize, DialogType dialogType, string title, string message, Action<DialogResult> onClosed=null)
    {
        if (Instance != null)
            Instance.OpenDialog(dialogSize, dialogType, title, message, onClosed);
    }

    private void OpenDialog(DialogSize dialogSize, DialogType dialogType, string title, string message, Action<DialogResult> onClosed = null)
    {
        GameObject dialogPrefab = null;
        switch (dialogSize)
        {
            case DialogSize.Small:
                dialogPrefab = dialogPrefabSmall;
                break;
            case DialogSize.Medium:
                dialogPrefab = dialogPrefabMedium;
                break;
            case DialogSize.Large:
                dialogPrefab = dialogPrefabLarge;
                break;
        }

        DialogButtonType dialogButtonType = DialogButtonType.None;
        switch (dialogType)
        {
            case DialogType.Confirmation:
                dialogButtonType = DialogButtonType.OK;
                break;
            case DialogType.Choice:
                dialogButtonType = DialogButtonType.Yes | DialogButtonType.No;
                break;
        }

        Dialog dialog = Dialog.Open(dialogPrefab, dialogButtonType, title, message, true);
        if (dialog != null)
        {
            dialog.OnClosed += onClosed;
        }
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号