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;
}
相关推荐
钢铁男儿41 分钟前
C# 方法(栈帧)
开发语言·c#
码小跳7 小时前
Halcon案例(一):C#联合Halcon识别路由器上的散热孔
图像处理·c#
神仙别闹10 小时前
基于C#+MySQL实现(WinForm)企业设备使用信息管理系统
开发语言·mysql·c#
czhaii10 小时前
PLC脉冲位置 单片机跟踪读取记录显示
开发语言·c#
神仙别闹12 小时前
基于C#+SQL Server开发(WinForm)租房管理系统
数据库·oracle·c#
bicijinlian13 小时前
.Net HttpClient 管理客户端(初始化与生命周期管理)
c#·.net·httpclient·.net httpclient·c# http
一个人的博客@你13 小时前
C# 通用OCR识别
图像处理·c#·ocr·图像识别·文字提取
老胖闲聊15 小时前
C# 注册表操作类
开发语言·c#
液态不合群17 小时前
理解 C# 中的各类指针
java·开发语言·c#
可喜~可乐17 小时前
C# SQLite高级功能示例
数据库·sql·sqlite·c#