Packages/Mixed Reality Toolkit Foundation/SDK/Features/UX/Prefabs/Dialog
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; } } }