Unity限制弹出UI的位置,防止超出屏幕显示区域

Unity限制弹出UI的位置,防止超出屏幕显示区域,仅供记录下次方便使用

直接上代码

/*****************************************************************
*Copyright(C) 2019 by 万事顺遂  
 *All rights reserved. 
 *FileName:#SCRIPTFULLNAME# 
 *Author:zhen
 *Version:1.0 
 *UnityVersion:2020.3.1f1
 *Date:2023/10/24 10:08:36 
 *Description:    限制弹出UI的位置,放置超出屏幕显示区域
 *History:
*******************************************************************/
using QFramework.GroupTower;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Zhen
{
    /// <summary>
    /// 限制类型
    /// </summary>
    public enum LimitPopUI
    {,
        限制上,
        限制下,
        限制左,
        限制右,
        限制上左,
        限制上右,
        限制下左,
        限制下右,
    }

    public class LimitPopUIPosScript : MonoBehaviour
    {
        //二者可以使同一个,也可以是不同的,锚点在中心
        public GameObject target;
        public GameObject targetPanel;
        LimitPopUI LimitPopUI = LimitPopUI.;
        /// <summary>
        /// 限制弹出UI的位置,放置超出屏幕显示区域
        /// </summary>
        public void LimitPopUIPosFunc()
        {
            LimitPopUI = LimitPopUI.;
            if ((Screen.height - Input.mousePosition.y) <= target.transform.GetComponent<RectTransform>().rect.height)
            {
                LimitPopUI = LimitPopUI.限制上;
            }
            if (Input.mousePosition.y <= target.transform.GetComponent<RectTransform>().rect.height)
            {
                LimitPopUI = LimitPopUI.限制下;
            }
            if (Input.mousePosition.x <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制左;
            }
            if ((Screen.width - Input.mousePosition.x) <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制右;
            }
            if ((Screen.height - Input.mousePosition.y) <= target.transform.GetComponent<RectTransform>().rect.height && Input.mousePosition.x <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制上左;
            }
            if ((Screen.height - Input.mousePosition.y) <= target.transform.GetComponent<RectTransform>().rect.height && (Screen.width - Input.mousePosition.x) <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制上右;
            }
            if (Input.mousePosition.y <= target.transform.GetComponent<RectTransform>().rect.height && Input.mousePosition.x <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制下左;
            }
            if (Input.mousePosition.y <= target.transform.GetComponent<RectTransform>().rect.height && (Screen.width - Input.mousePosition.x) <= target.transform.GetComponent<RectTransform>().rect.width)
            {
                LimitPopUI = LimitPopUI.限制下右;
            }

            //Debug.Log(LimitPopUI);

            switch (LimitPopUI)
            {
                case LimitPopUI.:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(Input.mousePosition.x + target.transform.GetComponent<RectTransform>().rect.width / 2,
                        Input.mousePosition.y - target.transform.GetComponent<RectTransform>().rect.height / 2, 0);
                    break;
                case LimitPopUI.限制上:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(Input.mousePosition.x + target.transform.GetComponent<RectTransform>().rect.width / 2,
                        Screen.height - target.transform.GetComponent<RectTransform>().rect.height / 2, 0);
                    break;
                case LimitPopUI.限制下:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(Input.mousePosition.x + target.transform.GetComponent<RectTransform>().rect.width / 2,
                        target.transform.GetComponent<RectTransform>().rect.height / 2, 0);
                    break;
                case LimitPopUI.限制左:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(target.transform.GetComponent<RectTransform>().rect.width / 2,
                        Input.mousePosition.y - target.transform.GetComponent<RectTransform>().rect.height / 2, 0);
                    break;
                case LimitPopUI.限制右:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(Screen.width - target.transform.GetComponent<RectTransform>().rect.width / 2,
                        Input.mousePosition.y - target.transform.GetComponent<RectTransform>().rect.height / 2, 0);
                    break;
                case LimitPopUI.限制上左:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(target.transform.GetComponent<RectTransform>().rect.width / 2,
                        Screen.height - target.transform.GetComponent<RectTransform>().rect.height / 2,
                        0);
                    break;
                case LimitPopUI.限制上右:
                    targetPanel.GetComponent<RectTransform>().position =
                       new Vector3(Screen.width - target.transform.GetComponent<RectTransform>().rect.width / 2,
                       Screen.height - target.transform.GetComponent<RectTransform>().rect.height / 2,
                       0);
                    break;
                case LimitPopUI.限制下左:
                    targetPanel.GetComponent<RectTransform>().position =
                        new Vector3(target.transform.GetComponent<RectTransform>().rect.width / 2,
                        target.transform.GetComponent<RectTransform>().rect.height / 2,
                        0);
                    break;
                case LimitPopUI.限制下右:
                    targetPanel.GetComponent<RectTransform>().position =
                       new Vector3(Screen.width - target.transform.GetComponent<RectTransform>().rect.width / 2,
                       target.transform.GetComponent<RectTransform>().rect.height / 2,
                       0);
                    break;
                default:
                    break;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值