Esempio n. 1
0
    //lz-2016.07.18 根据鼠标点的位置,将这个Transfrom显示在屏幕的中合适的位置,不超出屏幕
    public static void SetTransInScreenByMousePos(this Transform trans, InScreenOffsetMode offsetMode = InScreenOffsetMode.OffsetBounds)
    {
        Vector3 mousePos = Input.mousePosition;

        mousePos = new Vector3(mousePos.x - Screen.width / 2, mousePos.y - Screen.height / 2, trans.localPosition.z);
        Vector2 pos = GetPosInScreenByPos(trans, mousePos, offsetMode);

        trans.localPosition = new Vector3(pos.x, pos.y, trans.localPosition.z);
    }
Esempio n. 2
0
    //lz-2016.07.18 根据Trans的Bounds和ToPos来计算出合适的位置
    public static Vector2 GetPosInScreenByPos(this Transform trans, Vector3 toPos, InScreenOffsetMode offsetMode)
    {
        Bounds wndBounds = NGUIMath.CalculateRelativeWidgetBounds(trans);
        //优先计算右下角方向
        float screenWidthHalf  = Screen.width / 2;
        float screenHeightHalf = Screen.height / 2;
        float toPosLimitX      = toPos.x + wndBounds.size.x;
        float toPosLimitY      = toPos.y - wndBounds.size.y;

        switch (offsetMode)
        {
        case InScreenOffsetMode.OffsetBounds:
            if (toPosLimitX < -screenWidthHalf)
            {
                toPos.x += wndBounds.size.x;
            }
            else if (toPosLimitX > screenWidthHalf)
            {
                toPos.x -= wndBounds.size.x;
            }
            if (toPosLimitY < -screenHeightHalf)
            {
                toPos.y += wndBounds.size.y;
            }
            else if (toPosLimitY > screenHeightHalf)
            {
                toPos.y -= wndBounds.size.y;
            }
            break;

        case InScreenOffsetMode.OffsetLimit:
            toPos.x = Mathf.Clamp(toPosLimitX, -screenWidthHalf, screenWidthHalf);
            toPos.y = Mathf.Clamp(toPosLimitY, -screenHeightHalf, screenHeightHalf);
            break;
        }
        return(toPos);
    }