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;
}
相关推荐
weixin_4239950021 小时前
unity 处理图片:截图,下载,保存
java·unity·游戏引擎
故事不长丨1 天前
C#进制转换:从基础原理到实战应用
开发语言·c#·进制转换·16进制·2进制·10进制
liulilittle1 天前
VEthernet 框架实现 tun2socks 的技术原理
网络·windows·c#·信息与通信·通信
云草桑1 天前
.net AI API应用 客户发的信息提取对接上下游系统报价
ai·c#·.net·semantickernel·sk
故事不长丨1 天前
C#File文件操作全解析:从基础用法到异常处理
服务器·开发语言·visualstudio·c#·文件操作·io流·file
呆呆敲代码的小Y1 天前
【Unity实战篇】| 游戏轮播图效果,多种实现思路及完整教程
游戏·unity·游戏引擎·实战·游戏开发·轮播图·u3d
工程师0071 天前
C# 动态编程(基于 dynamic 类型)
开发语言·c#·dynamic·动态编程
用户298698530141 天前
C#: 在Word文档中添加或移除可编辑区域
后端·c#
爱说实话1 天前
C# 2026
c#
FairGuard手游加固1 天前
FairGuard游戏安全2025年度报告
安全·游戏