123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace Chiva.Toolkit.Function.NoBorder
- {
- public class WindowsSetting : MonoSingleton<WindowsSetting>,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler
- {
- /// <summary>
- /// 是否可以拖拽
- /// </summary>
- [SerializeField]
- private bool m_IsDragging;
- [SerializeField]
- private Rect m_ScenePosition;
- /// <summary>
- /// 当前项目正式发布时的产品名称
- /// </summary>
- [SerializeField]
- private string m_ProductName;
- public bool playOnAwake;
- bool runBord= false;
- void Awake()
- {
- m_ProductName = Application.productName;
- if (playOnAwake)
- {
- SetBorderModel(true,m_ScenePosition.width,m_ScenePosition.height);
- }
- else
- {
- WindowsTools.GetCurrentWindowHandle(m_ProductName);
- }
- }
- public void SetBorderModel(bool bord,float width, float height)
- {
- CanvasScaler canvasScaler = transform.GetComponent<CanvasScaler>();
- if (bord)
- {
- if (!string.IsNullOrEmpty(m_ProductName))
- {
- WindowsTools.GetCurrentWindowHandle(m_ProductName);
- runBord = true;
- canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;
- SetNoBorderModel(width, height);
- }
- }
- else
- {
- runBord = false;
- canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
- Screen.SetResolution((int)width, (int)height,true);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (!runBord) return;
- if (m_IsDragging == true)
- {
- WindowsTools.DragWindow();
- }
- }
- /// <summary>
- /// 最小化按钮
- /// </summary>
- public void OnMinBtnClick()
- {
- WindowsTools.SetMinWindows();
- }
- /// <summary>
- /// 设置无边框
- /// </summary>
- public void SetNoBorder(float width,float height)
- {
- float posX = (Screen.currentResolution.width - width) / 2;
- float posY = (Screen.currentResolution.height - height) / 2;
- Rect rect = new Rect(posX, posY, width, height);
- WindowsTools.SetNoBorderWindow(rect);
- }
- /// <summary>
- /// 设置无边框模式
- /// </summary>
- public void SetNoBorderModel(float width, float height)
- {
- Screen.SetResolution(
- (int)width,
- (int)height,
- false);
- //等待当前帧完成,然后去除边框
- StartCoroutine(WaitNoBorder(width,height));
- }
- /// <summary>
- /// 等待去除边框
- /// </summary>
- /// <returns></returns>
- private IEnumerator WaitNoBorder(float width, float height)
- {
- yield return new WaitForEndOfFrame();
- yield return new WaitForFixedUpdate();
- if (!Screen.fullScreen)
- {
- SetNoBorder(width,height);
- }
- }
-
- /// <summary>
- /// 退出
- /// </summary>
- public void ShutDownFunction()
- {
- Application.Quit();
- }
- #region 内置函数
- public void OnPointerDown(PointerEventData eventData)
- {
- //m_IsDragging = true;
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- //m_IsDragging = false;
- }
- public void OnPointerUp(PointerEventData eventData)
- {
- //m_IsDragging = false;
- }
- #endregion
- }
- }
|