简体   繁体   English

如何在屏幕上保持弹出 window。 (Unity 2D、UI 图像、屏幕空间叠加)

[英]How to keep a pop up window on the screen. (Unity 2D, UI Image, Screen space overlay)

How to keep a pop up window on the screen?如何在屏幕上弹出 window? (Unity 2D, UI Image, Screen space overlay) (Unity 2D、UI 图像、屏幕空间叠加)

I have a pop up window 225x172 that I enable and change its transform to the mouse position as a pop up window.我有一个弹出窗口 window 225x172,我启用并将其转换为鼠标 position 作为弹出窗口 window。

The problem I have is that if a user clicks near the screen edge, the pop up window isn't fully visible on the screen, as parts of its length and width will be out of the visible area.我遇到的问题是,如果用户在屏幕边缘附近单击,弹出的 window 在屏幕上并不完全可见,因为它的部分长度和宽度将超出可见区域。

Solution is to check for the borders and adjust the pop up windows location to ensure for its length and width it appears as close to the mouse as possible but always fully visible.解决方案是检查边框并调整弹出的 windows 位置,以确保其长度和宽度尽可能靠近鼠标,但始终完全可见。

I wrote a function to return the adjusted Vector3, and it works ok until I adjust the screen resolution, and then it starts placing it in different non optimal positions.我写了一个 function 来返回调整后的 Vector3,它可以正常工作,直到我调整屏幕分辨率,然后它开始将它放置在不同的非最佳位置。

I guess I have to put some sort of scaling in?我想我必须进行某种缩放? I imagine this is fairly common problem, but googling for a few hours hasn't given me any solutions...我想这是一个相当普遍的问题,但谷歌搜索了几个小时并没有给我任何解决方案......

Anyone got a solution?有人有解决方案吗? Here is my method:这是我的方法:

   private Vector3 KeepOnScreen(Vector3 MyInputPos, int width, int Height)   // returns vector3 always on screen
{
    var corners = new Vector3[4];
    var RetRec = new Vector3();
    RetRec = MyInputPos;

    GetComponent<RectTransform>().GetWorldCorners(corners);
    // This returns the world space positions of the corners in the order
    // [0] bottom left, [1] top left [2] top right [3] bottom right

    int xmin = Mathf.RoundToInt(corners[0].x);
    int xmax = Mathf.RoundToInt(corners[2].x);
    int ymin = Mathf.RoundToInt(corners[2].y);
    int ymax = Mathf.RoundToInt(corners[3].y);

    //Debug.Log($" MINS {xmin} {ymin}");
    //Debug.Log($" MAXS {xmax} {ymax}");
    //Debug.Log($" Mouse {MyInputPos.x} {MyInputPos.y}");

    RetRec.y = RetRec.y-20 - Height*2;  // move a bit to the right and down so the pop up window is not under mouse
    RetRec.x = RetRec.x + 40;
    if (MyInputPos.x + width*2 > xmax) { RetRec.x = xmax - width * 2 - 40;} // adjust to keep whole window on screen
    if (MyInputPos.y -Height < ymax) { RetRec.y = MyInputPos.y;}

    return RetRec;

}

Ok after may hours of searching I ended up with the following method that pops up the UI element near the mouse but keeps it on the screen.好的,经过数小时的搜索,我最终得到了以下方法,该方法在鼠标附近弹出 UI 元素,但将其保留在屏幕上。

Pass it your mouse position (Input.mousePosition), the popup windows RectTransform, and the RectTransform of the canvas your panel is on.将鼠标 position (Input.mousePosition)、弹出窗口 windows RectTransform 和 canvas 的 RectTransform 传递给您的面板。

void ClampToWindow( Vector3 MyMouse,  RectTransform panelRectTransform, RectTransform parentRectTransform)
{

    panelRectTransform.transform.position  = MyMouse;

    Vector3 pos = panelRectTransform.localPosition;

    Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
    Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;

    pos.x = Mathf.Clamp(panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
    pos.y = Mathf.Clamp(panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);

    panelRectTransform.localPosition = pos;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM