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;
}
相关推荐
驱动小百科12 小时前
国产3A游戏《影之刃零》正式发售:售价及电脑配置要求介绍
游戏·影之刃零·影之刃零配置要求·影之刃零下载·影之刃零售价
吃一碗粉只付一碗钱13 小时前
10-令牌里的用户主键不要明文
服务器·数据库·sql·sqlserver·c#
信奥卷王14 小时前
[GESP202312 六级] 闯关游戏
游戏
胖纸不争16 小时前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
德迅云安全-上官16 小时前
德迅游戏盾:面向 TCP 业务的分布式云安全防护方案
分布式·tcp/ip·游戏
科技那些事儿17 小时前
旗舰游戏本 CPU 对决:锐龙 9 9955HX 对比酷睿 Ultra 9 275HX
游戏·智能手机·智能电视
曹牧17 小时前
C#:The operation has timed out
开发语言·c#
He BianGu17 小时前
【WPF-Control教程】IOC 依赖注入
c#·wpf·wpf-control
He BianGu17 小时前
【WPF-Control教程】H.Extensions.Behvaiors 开发文档
c#·wpf·wpf-control
njsgcs17 小时前
老滚5 文本替换中文简体 仓库加经验glm5.3f
游戏