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;
}
}
}
}