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#List
开发语言·c#·list
mxwin2 分钟前
Unity URP 下 Shader 变体 (Variants):multi_compile 与 shader_feature的关键字管理及变体爆炸防控策略
unity·游戏引擎
howlet21 小时前
AI生成cocos-creator打砖块游戏-跑通第1关(CodeBuddy)
人工智能·游戏·cocos2d
独特的螺狮粉1 小时前
开源鸿蒙跨平台Flutter开发:室内探险游戏应用
开发语言·flutter·游戏·华为·开源·harmonyos·鸿蒙
RReality2 小时前
【Unity Shader URP】全息扫描线(Hologram Scanline)源码+脚本控制
ui·unity·游戏引擎·图形渲染
独特的螺狮粉2 小时前
开源鸿蒙跨平台Flutter开发:手机清理小助手应用
开发语言·flutter·游戏·智能手机·开源·harmonyos·鸿蒙
Paine Zeng2 小时前
C# + SolidWorks 二次开发 -监听退出草图事件并自动执行逻辑
c#·solidworks二次开发·solidworks api
游乐码3 小时前
C#Dicitionary
算法·c#
SunnyDays10113 小时前
C# 实战:如何高效地将 HTML 转换为可编辑 Word 文档
c#·html转word
渔民小镇3 小时前
一次编写到处对接 —— 为 Godot/Unity/React 生成统一交互接口
java·分布式·游戏·unity·godot