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;
}
相关推荐
风继续吹..1 分钟前
C# 文件 IO 实操练习题 5道
开发语言·c#
苦荞米14 分钟前
C#中,Ticks不能作为时间戳使用。
c#
2501_9400417425 分钟前
探索非主流游戏机制的AI生成指南
人工智能·游戏
吴可可1231 小时前
Teigha处理CAD样条曲线的方法解析
数据库·算法·c#
XS0301061 小时前
并发编程三
开发语言·c#
樱桃花下的小猫3 小时前
游戏云服务器定时任务设置教程
服务器·游戏·新手友好·云鸢互联·零门槛一键开服
魔法阵维护师5 小时前
从零开发游戏需要学习的c#模块,第二十章(2D 敌人与战斗触发)
学习·游戏·c#
我是唐青枫6 小时前
C#.NET YARP + OpenTelemetry:网关链路追踪实战
开发语言·c#·.net
相信神话20217 小时前
第四章:创建《酒魂》项目与场景结构
游戏·游戏引擎·godot·2d游戏开发
mxwin7 小时前
Unity Shader URP 使用模板测试 · 深度测试实现秘境空间效果
unity·游戏引擎·shader