当前位置:   article > 正文

Unity3D 调用Windows弹窗_unity 弹窗

unity 弹窗

前言

Unity3D可以使用UGUI自己设计弹窗,也可以像Winform一样使用Windows系统本身的弹窗(跨平台请谨慎使用)。

原文:Unity调用Windows弹框、提示框(确认与否,中文)

其他:https://www.jianshu.com/p/5a930906df03

部分效果如下:

代码如下所示:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.InteropServices;//调用外部库,需要引用命名空间
  5. using System;
  6. using UnityEngine.UI;
  7. public class UseWindowsMessagePop : MonoBehaviour
  8. {
  9. //引入dll
  10. [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  11. public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法
  12. public Transform ButtonsParent;//按钮组
  13. private int returnNumber; //返回值
  14. private void Start()
  15. {
  16. int i = 0;
  17. foreach (Transform item in ButtonsParent)
  18. {
  19. if (item.GetComponent<Button>())
  20. {
  21. item.name = item.GetChild(0).GetComponent<Text>().text = i.ToString();
  22. item.GetComponent<Button>().onClick.AddListener(() => Button(int.Parse(item.name)));
  23. i++;
  24. }
  25. }
  26. }
  27. /// <summary>
  28. /// 按钮对应弹框
  29. /// </summary>
  30. /// <param name="index"></param>
  31. private void Button(int index)
  32. {
  33. Debug.Log("click:" + index);
  34. switch (index)
  35. {
  36. case 0:
  37. returnNumber = MessageBox(IntPtr.Zero, "Chinar-0:返回值均:1", "确认", 0);
  38. print(returnNumber);
  39. break;
  40. case 1:
  41. returnNumber = MessageBox(IntPtr.Zero, "Chinar-1:确认:1,取消:2", "确认|取消", 1);
  42. print(returnNumber);
  43. break;
  44. case 2:
  45. returnNumber = MessageBox(IntPtr.Zero, "Chinar-2:中止:3,重试:4,忽略:5", "中止|重试|忽略", 2);
  46. print(returnNumber);
  47. break;
  48. case 3:
  49. returnNumber = MessageBox(IntPtr.Zero, "Chinar-3:是:6,否:7,取消:2", "是 | 否 | 取消", 3);
  50. print(returnNumber);
  51. break;
  52. case 4:
  53. returnNumber = MessageBox(IntPtr.Zero, "Chinar-4:是:6,否:7", "是 | 否", 4);
  54. print(returnNumber);
  55. break;
  56. case 5:
  57. returnNumber = MessageBox(IntPtr.Zero, "Chinar-5:重试:4,取消:2", "重试 | 取消", 5);
  58. print(returnNumber);
  59. break;
  60. case 6:
  61. returnNumber = MessageBox(IntPtr.Zero, "Chinar-6:取消:2,重试:10,继续:11", "取消 | 重试 | 继续", 6);
  62. print(returnNumber);
  63. break;
  64. }
  65. }
  66. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/783233
推荐阅读
相关标签
  

闽ICP备14008679号