예제 #1
0
    /// <summary>
    /// With this method you can get the position of the UI object in different kind of coordinate systems.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="coordinateSystem"> The returned position will be from the coordinate system you specify in this parameter.</param>
    /// <param name="rtePivotCentered"> This tool has it's own pivot, the Unity pivot is ignored in all the coordinate systems except "IgnoreAnchors", the pivot of this tool is located at the lower left corner of this object, if this parameter is true then the pivot will be located at the center of this object.</param>
    public static Vector2 GetPosition(this RectTransform tr, CoordinateSystem coordinateSystem = CoordinateSystem.IgnoreAnchorsAndPivot, bool rtePivotCentered = false)
    {
        Vector2 result = default(Vector2);

        switch (coordinateSystem)
        {
        case CoordinateSystem.IgnoreAnchorsAndPivot:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            break;

        case CoordinateSystem.IgnoreAnchors:
            result = RteRectTools.GetPositionIgnoringAnchors(tr);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivotNormalized:
            result = RteRectTools.GetPositionNormalizedIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            break;

        case CoordinateSystem.AsChildOfCanvas:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            result = RteAnchorTools.GetCanvasAnchorCoordinateFromRectCoordinate(tr, result);
            break;

        case CoordinateSystem.AsChildOfCanvasNormalized:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            result = RteAnchorTools.GetCanvasAnchorCoordinateNormalizedFromRectCoordinate(tr, result);
            break;

        case CoordinateSystem.ScreenSpacePixels:
            result = RteAnchorTools.GetScreenSpaceCoordinateFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered));
            break;
        }

        return(result);
    }