WindowsSetting.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace Chiva.Toolkit.Function.NoBorder
  6. {
  7. public class WindowsSetting : MonoSingleton<WindowsSetting>,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler
  8. {
  9. /// <summary>
  10. /// 是否可以拖拽
  11. /// </summary>
  12. [SerializeField]
  13. private bool m_IsDragging;
  14. [SerializeField]
  15. private Rect m_ScenePosition;
  16. /// <summary>
  17. /// 当前项目正式发布时的产品名称
  18. /// </summary>
  19. [SerializeField]
  20. private string m_ProductName;
  21. public bool playOnAwake;
  22. bool runBord= false;
  23. void Awake()
  24. {
  25. m_ProductName = Application.productName;
  26. if (playOnAwake)
  27. {
  28. SetBorderModel(true,m_ScenePosition.width,m_ScenePosition.height);
  29. }
  30. else
  31. {
  32. WindowsTools.GetCurrentWindowHandle(m_ProductName);
  33. }
  34. }
  35. public void SetBorderModel(bool bord,float width, float height)
  36. {
  37. CanvasScaler canvasScaler = transform.GetComponent<CanvasScaler>();
  38. if (bord)
  39. {
  40. if (!string.IsNullOrEmpty(m_ProductName))
  41. {
  42. WindowsTools.GetCurrentWindowHandle(m_ProductName);
  43. runBord = true;
  44. canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
  45. SetNoBorderModel(width, height);
  46. }
  47. }
  48. else
  49. {
  50. runBord = false;
  51. canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
  52. Screen.SetResolution((int)width, (int)height,true);
  53. }
  54. }
  55. // Update is called once per frame
  56. void Update()
  57. {
  58. if (!runBord) return;
  59. if (m_IsDragging == true)
  60. {
  61. WindowsTools.DragWindow();
  62. }
  63. }
  64. /// <summary>
  65. /// 最小化按钮
  66. /// </summary>
  67. public void OnMinBtnClick()
  68. {
  69. WindowsTools.SetMinWindows();
  70. }
  71. /// <summary>
  72. /// 设置无边框
  73. /// </summary>
  74. public void SetNoBorder(float width,float height)
  75. {
  76. float posX = (Screen.currentResolution.width - width) / 2;
  77. float posY = (Screen.currentResolution.height - height) / 2;
  78. Rect rect = new Rect(posX, posY, width, height);
  79. WindowsTools.SetNoBorderWindow(rect);
  80. }
  81. /// <summary>
  82. /// 设置无边框模式
  83. /// </summary>
  84. public void SetNoBorderModel(float width, float height)
  85. {
  86. Screen.SetResolution(
  87. (int)width,
  88. (int)height,
  89. false);
  90. //等待当前帧完成,然后去除边框
  91. StartCoroutine(WaitNoBorder(width,height));
  92. }
  93. /// <summary>
  94. /// 等待去除边框
  95. /// </summary>
  96. /// <returns></returns>
  97. private IEnumerator WaitNoBorder(float width, float height)
  98. {
  99. yield return new WaitForEndOfFrame();
  100. yield return new WaitForFixedUpdate();
  101. if (!Screen.fullScreen)
  102. {
  103. SetNoBorder(width,height);
  104. }
  105. }
  106. /// <summary>
  107. /// 退出
  108. /// </summary>
  109. public void ShutDownFunction()
  110. {
  111. Application.Quit();
  112. }
  113. #region 内置函数
  114. public void OnPointerDown(PointerEventData eventData)
  115. {
  116. //m_IsDragging = true;
  117. }
  118. public void OnPointerExit(PointerEventData eventData)
  119. {
  120. //m_IsDragging = false;
  121. }
  122. public void OnPointerUp(PointerEventData eventData)
  123. {
  124. //m_IsDragging = false;
  125. }
  126. #endregion
  127. }
  128. }