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;
}
相关推荐
future_studio3 分钟前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
helloworddm42 分钟前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
向宇it1 小时前
【unity实战】MapMagic 2实战例子
游戏·3d·unity·c#·游戏引擎
"菠萝"1 小时前
C#知识学习-017(修饰符_6)
学习·c#
VB.Net2 小时前
C#循序渐进
开发语言·c#
SlowFeather3 小时前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
feifeigo1233 小时前
C# WinForms实现模拟叫号系统
c#
helloworddm4 小时前
Orleans 流系统握手机制时序图
后端·c#
William_cl4 小时前
【C# OOP 入门到精通】从基础概念到 MVC 实战(含 SOLID 原则与完整代码)
开发语言·c#·mvc
串流游戏联盟8 小时前
steam新品节游戏推荐!手机怎么玩steam游戏!
游戏·远程工作