Unity 设置弹窗Tips位置

根据鼠标位于屏幕的区域,设置弹窗锚点以及位置
csharp 复制代码
public static void TipsPos(Transform tf)
{
	//获取ui相机
    var uiCamera = GetUICamera();
    var popup = tf.GetComponent<RectTransform>();
	//获取鼠标位置
    Vector2 mousePos = Input.mousePosition;

    float screenWidth = Screen.width;
    float screenHeight = Screen.height;

    if (mousePos.x < screenWidth / 2 && mousePos.y > screenHeight / 2)
    {
        // 左上角
        popup.anchorMin = new Vector2(0, 1);
        popup.anchorMax = new Vector2(0, 1);
        popup.pivot = new Vector2(0, 1);
    }
    else if (mousePos.x > screenWidth / 2 && mousePos.y > screenHeight / 2)
    {
        // 右上角
        popup.anchorMin = new Vector2(1, 1);
        popup.anchorMax = new Vector2(1, 1);
        popup.pivot = new Vector2(1, 1);
    }
    else if (mousePos.x < screenWidth / 2 && mousePos.y < screenHeight / 2)
    {
        // 左下角
        popup.anchorMin = new Vector2(0, 0);
        popup.anchorMax = new Vector2(0, 0);
        popup.pivot = new Vector2(0, 0);
    }
    else if (mousePos.x > screenWidth / 2 && mousePos.y < screenHeight / 2)
    {
        // 右下角
        popup.anchorMin = new Vector2(1, 0);
        popup.anchorMax = new Vector2(1, 0);
        popup.pivot = new Vector2(1, 0);
    }
    Vector2 localPoint;
    RectTransformUtility.ScreenPointToLocalPointInRectangle(popup.GetComponent<RectTransform>(), mousePos, uiCamera, out localPoint);
    popup.anchoredPosition = localPoint;
}
相关推荐
死也不注释16 分钟前
【在Unity游戏开发中Dictionary、List介绍】
数据结构·游戏·unity·游戏引擎·list·游戏程序
唐青枫2 小时前
C#.NET 主机详解
c#·.net
R-G-B8 小时前
【28】C# WinForm入门到精通 ——多文档窗体MDI【属性、方法、实例、源码】【多窗口重叠、水平平铺、垂直平铺、窗体传值】
c#·winform·多文档窗体mdi·多窗口重叠·水平平铺·垂直平铺·窗体传值
LZQqqqqo12 小时前
c#_文件的读写 IO
开发语言·c#
沧海归城13 小时前
Unity_XR控制手部动画
unity·游戏引擎·xr
你好!蒋韦杰-(烟雨平生)14 小时前
扫雷游戏C++
c++·单片机·游戏
CodeCraft Studio16 小时前
图像处理控件Aspose.Imaging教程:使用 C# 编程将 CMX 转换为 PNG
图像处理·人工智能·c#·aspose·png·图片格式转换·cmx
csdn_aspnet17 小时前
C# 求梯形面积的程序(Program to find area of a Trapezoid)
c#
FreeBuf_18 小时前
伪装成华硕游戏辅助软件的ArmouryLoader:突破系统安全防护的恶意代码注入器
安全·游戏·系统安全
LZQqqqqo18 小时前
C#_创建自己的MyList列表
java·算法·c#