コード例 #1
0
    public void DoAlignToCanvas()
    {
        if (UseCamera.orthographic &&
            Mathfx.Approximately(UseCamera.orthographicSize, 0))
        {
            return;
        }

        var screenSize = GetGameViewSize();
        var bottomLeft = UseCamera.ScreenToWorldPoint(new Vector3(0, 0, UseCamera.nearClipPlane));
        var topRight   = UseCamera.ScreenToWorldPoint(new Vector3(screenSize.x, screenSize.y, UseCamera.farClipPlane));

        if (Boundaries == null)
        {
            return;
        }

        switch (HorizontalAlign)
        {
        case HorizontalAlignType.Left:
            _offsetX = bottomLeft.x + transform.lossyScale.x * (-Boundaries.center.x + Boundaries.size.x / 2);
            break;

        case HorizontalAlignType.Right:
            _offsetX = topRight.x + transform.lossyScale.x * (-Boundaries.center.x - Boundaries.size.x / 2);
            break;

        case HorizontalAlignType.Center:
            _offsetX = (topRight.x + bottomLeft.x) / 2 + transform.lossyScale.x * -Boundaries.center.x;
            break;
        }

        switch (VerticalAlign)
        {
        case VerticalAlignType.Bottom:
            _offsetY = bottomLeft.y + transform.lossyScale.x * (-Boundaries.center.y + (Boundaries.size.y / 2));
            break;

        case VerticalAlignType.Top:
            _offsetY = topRight.y + transform.lossyScale.x * (-Boundaries.center.y - (Boundaries.size.y / 2));
            break;

        case VerticalAlignType.Center:
            _offsetY = (topRight.y + bottomLeft.y) / 2 + transform.lossyScale.x * -Boundaries.center.y;
            break;
        }

        transform.position = new Vector3(_offsetX, _offsetY, transform.position.z);
    }