当前位置: 首页 > news >正文

Unity 设置弹窗Tips位置

根据鼠标位于屏幕的区域,设置弹窗锚点以及位置
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;
}

相关文章:

  • LLaMA-Factory从安装到微调全流程
  • Linux上搭建NFS共享存储
  • SpringBoot项目集成Seata 2.0.0
  • Kubernetes核心架构:从组件协同到工作原理
  • LED恒流驱动驱动电路原理图 LM3406HV-Q1
  • SpringBoot 为何启动慢
  • 第1课:MCP服务协议核心架构解析
  • Cursor中rules配置参考-202504版(含前后端Golang/TypeScript/Kotlin等)
  • Android 自己的智能指针
  • leetcode:905. 按奇偶排序数组(python3解法)
  • 解读json.loads函数参数
  • 【免费公测】可遇AI直播/无人直播/矩阵直播/AI场控
  • 微信小程序跳6
  • CondaError: Run ‘conda init‘ before ‘conda activate‘
  • 大数据(7.1)Kafka实时数据采集与分发的企业级实践:从架构设计到性能调优
  • 蓝桥杯基础数论入门
  • freecad 窗口控件层级关系
  • DIA——边缘检测
  • 腾讯后端 C++一面:recv 返回值,什么错误是可接受的?
  • vue3+vite+ts使用daisyui/tailwindcss
  • wordpress 大型站/搜索引擎优化的定义是什么
  • 国外购物独立网站建设/网络安全
  • 如何在app上做网站/网站搜索优化
  • 如何注册免费企业邮箱/济南seo优化外包
  • 做游戏制作 网站/今日新闻头条
  • 摄影网站源代码/百度总部地址