示例#1
0
        /// <summary>
        /// 스크린의 특정 피봇 위치에 대한 타겟의 위치를 구한다.
        /// </summary>
        /// <param name="targetTransform">위치를 얻고자 하는 타겟의 Transform</param>
        /// <param name="screenPivot">타겟의 스크린에 대한 PivotPresetType</param>
        /// <returns></returns>
        public Vector3 GetWorldPosition(
            Transform targetTransform,
            PivotPresetType screenPivot)
        {
            if (targetTransform is null)
            {
                throw new ArgumentNullException(nameof(targetTransform));
            }

            float2 viewport2;

            switch (screenPivot)
            {
            case PivotPresetType.TopLeft:
                viewport2 = Float2.ZeroOne;
                break;

            case PivotPresetType.TopCenter:
                viewport2 = Float2.HalfOne;
                break;

            case PivotPresetType.TopRight:
                viewport2 = Float2.OneOne;
                break;

            case PivotPresetType.MiddleLeft:
                viewport2 = Float2.ZeroHalf;
                break;

            case PivotPresetType.MiddleCenter:
                viewport2 = Float2.HalfHalf;
                break;

            case PivotPresetType.MiddleRight:
                viewport2 = Float2.OneHalf;
                break;

            case PivotPresetType.BottomLeft:
                viewport2 = Float2.ZeroZero;
                break;

            case PivotPresetType.BottomCenter:
                viewport2 = Float2.HalfZero;
                break;

            case PivotPresetType.BottomRight:
                viewport2 = Float2.OneZero;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(screenPivot), screenPivot, null);
            }

            var viewport3 = new float3(
                viewport2.x,
                viewport2.y,
                (targetTransform.position - Transform.position).magnitude);

            return(Cam.ViewportToWorldPoint(viewport3));
        }
示例#2
0
        public static PivotPresetType ReverseY(this PivotPresetType pivotPresetType)
        {
            switch (pivotPresetType)
            {
            case PivotPresetType.TopLeft:
                return(PivotPresetType.BottomLeft);

            case PivotPresetType.TopCenter:
                return(PivotPresetType.BottomCenter);

            case PivotPresetType.TopRight:
                return(PivotPresetType.BottomRight);

            case PivotPresetType.MiddleLeft:
                return(PivotPresetType.MiddleLeft);

            case PivotPresetType.MiddleCenter:
                return(PivotPresetType.MiddleCenter);

            case PivotPresetType.MiddleRight:
                return(PivotPresetType.MiddleRight);

            case PivotPresetType.BottomLeft:
                return(PivotPresetType.TopLeft);

            case PivotPresetType.BottomCenter:
                return(PivotPresetType.TopCenter);

            case PivotPresetType.BottomRight:
                return(PivotPresetType.TopRight);

            default:
                throw new ArgumentOutOfRangeException(nameof(pivotPresetType), pivotPresetType, null);
            }
        }
        public static void SetPivot(this RectTransform rectTransform, PivotPresetType pivotPresetType)
        {
            switch (pivotPresetType)
            {
            case PivotPresetType.TopLeft:
                rectTransform.pivot = Float2.ZeroOne;
                break;

            case PivotPresetType.TopCenter:
                rectTransform.pivot = Float2.HalfOne;
                break;

            case PivotPresetType.TopRight:
                rectTransform.pivot = Float2.OneOne;
                break;

            case PivotPresetType.MiddleLeft:
                rectTransform.pivot = Float2.ZeroHalf;
                break;

            case PivotPresetType.MiddleCenter:
                rectTransform.pivot = Float2.HalfHalf;
                break;

            case PivotPresetType.MiddleRight:
                rectTransform.pivot = Float2.OneHalf;
                break;

            case PivotPresetType.BottomLeft:
                rectTransform.pivot = Float2.ZeroZero;
                break;

            case PivotPresetType.BottomCenter:
                rectTransform.pivot = Float2.HalfZero;
                break;

            case PivotPresetType.BottomRight:
                rectTransform.pivot = Float2.OneZero;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(pivotPresetType), pivotPresetType, null);
            }
        }
        public static bool TryGetPivotPresetType(this RectTransform rectTransform, out PivotPresetType result)
        {
            int pivotPresetTypeIndex = 0;

            switch (rectTransform.pivot.y)
            {
            case 0:
                pivotPresetTypeIndex += 6;
                break;

            case 0.5f:
                pivotPresetTypeIndex += 3;
                break;

            case 1:
                break;

            default:
                result = default;
                return(false);
            }

            switch (rectTransform.pivot.x)
            {
            case 0:
                break;

            case 0.5f:
                pivotPresetTypeIndex += 1;
                break;

            case 1:
                pivotPresetTypeIndex += 2;
                break;

            default:
                result = default;
                return(false);
            }

            result = (PivotPresetType)pivotPresetTypeIndex;
            return(true);
        }
        public static void MoveToRelatedPosition(this RectTransform rectTransform, RectTransform target, PivotPresetType pivotPresetType,
                                                 float2 offset)
        {
            if (target is null)
            {
                return;
            }

            rectTransform.position = target.position;
            float2 anchoredPosition = rectTransform.anchoredPosition;

            anchoredPosition += target.GetPivotPositionFromAnchor(pivotPresetType) + offset;
            rectTransform.anchoredPosition = anchoredPosition;
        }
        public static float2 GetAnchoredPositionOfPivot(this RectTransform rectTransform, PivotPresetType pivotPresetType)
        {
            float2 anchoredPosition = rectTransform.anchoredPosition;

            return(anchoredPosition + rectTransform.GetPivotPositionFromAnchor(pivotPresetType));
        }
        public static float2 GetPivotPositionFromAnchor(this RectTransform rectTransform, PivotPresetType pivotPresetType)
        {
            var anchoredPosition = new float2();
            var pivot            = rectTransform.pivot;
            var size             = rectTransform.rect.size * rectTransform.transform.localScale;

            switch (pivotPresetType)
            {
            case PivotPresetType.TopLeft:
                anchoredPosition.x -= pivot.x * size.x;
                anchoredPosition.y += (1f - pivot.y) * size.y;
                break;

            case PivotPresetType.TopCenter:
                anchoredPosition.x += (0.5f - pivot.x) * size.x;
                anchoredPosition.y += (1f - pivot.y) * size.y;
                break;

            case PivotPresetType.TopRight:
                anchoredPosition.x += (1f - pivot.x) * size.x;
                anchoredPosition.y += (1f - pivot.y) * size.y;
                break;

            case PivotPresetType.MiddleLeft:
                anchoredPosition.x -= pivot.x * size.x;
                anchoredPosition.y += (0.5f - pivot.y) * size.y;
                break;

            case PivotPresetType.MiddleCenter:
                anchoredPosition.x += (0.5f - pivot.x) * size.x;
                anchoredPosition.y += (0.5f - pivot.y) * size.y;
                break;

            case PivotPresetType.MiddleRight:
                anchoredPosition.x += (1f - pivot.x) * size.x;
                anchoredPosition.y += (0.5f - pivot.y) * size.y;
                break;

            case PivotPresetType.BottomLeft:
                anchoredPosition.x -= pivot.x * size.x;
                anchoredPosition.y -= pivot.y * size.y;
                break;

            case PivotPresetType.BottomCenter:
                anchoredPosition.x += (0.5f - pivot.x) * size.x;
                anchoredPosition.y -= pivot.y * size.y;
                break;

            case PivotPresetType.BottomRight:
                anchoredPosition.x += (1f - pivot.x) * size.x;
                anchoredPosition.y -= pivot.y * size.y;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(pivotPresetType), pivotPresetType, null);
            }

            return(anchoredPosition);
        }
 public static void SetAnchorAndPivot(this RectTransform rectTransform, AnchorPresetType anchorPresetType, PivotPresetType pivotPresetType)
 {
     rectTransform.SetAnchor(anchorPresetType);
     rectTransform.SetPivot(pivotPresetType);
 }
示例#9
0
        /// <summary>
        /// 스크린의 특정 피봇 위치에 대한 타겟의 위치를 구한다. 이때 타겟 스프라이트의 피봇 또한 고려한다.
        /// </summary>
        /// <param name="targetTransform">위치를 얻고자 하는 타겟의 Transform</param>
        /// <param name="screenPivot">타겟 Sprite의 PivotPresetType</param>
        /// <param name="targetSprite">위치를 얻고자자 하는 타겟의 전체 영역을 자치하는 SpriteRenderer</param>
        /// <param name="spritePivot">타겟의 스크린에 대한 PivotPresetType</param>
        /// <returns></returns>
        public Vector3 GetWorldPosition(
            Transform targetTransform,
            PivotPresetType screenPivot,
            Sprite targetSprite,
            PivotPresetType spritePivot
            )
        {
            if (targetTransform is null)
            {
                throw new ArgumentNullException(nameof(targetTransform));
            }

            if (targetSprite is null)
            {
                throw new ArgumentNullException(nameof(targetSprite));
            }

            var position       = GetWorldPosition(targetTransform, screenPivot);
            var lossyScale     = targetTransform.lossyScale;
            var ppu            = targetSprite.pixelsPerUnit;
            var halfSizeAsUnit = new Vector3(
                lossyScale.x * targetSprite.rect.width / ppu / 2f,
                lossyScale.y * targetSprite.rect.height / ppu / 2f);
            Vector3 spritePivotPosition;

            switch (spritePivot)
            {
            case PivotPresetType.TopLeft:
                spritePivotPosition = new Vector3(-halfSizeAsUnit.x, halfSizeAsUnit.y);
                break;

            case PivotPresetType.TopCenter:
                spritePivotPosition = new Vector3(0f, halfSizeAsUnit.y);
                break;

            case PivotPresetType.TopRight:
                spritePivotPosition = new Vector3(halfSizeAsUnit.x, halfSizeAsUnit.y);
                break;

            case PivotPresetType.MiddleLeft:
                spritePivotPosition = new Vector3(-halfSizeAsUnit.x, 0f);
                break;

            case PivotPresetType.MiddleCenter:
                spritePivotPosition = Vector3.zero;
                break;

            case PivotPresetType.MiddleRight:
                spritePivotPosition = new Vector3(halfSizeAsUnit.x, 0f);
                break;

            case PivotPresetType.BottomLeft:
                spritePivotPosition = new Vector3(-halfSizeAsUnit.x, -halfSizeAsUnit.y);
                break;

            case PivotPresetType.BottomCenter:
                spritePivotPosition = new Vector3(0f, -halfSizeAsUnit.y);
                break;

            case PivotPresetType.BottomRight:
                spritePivotPosition = new Vector3(halfSizeAsUnit.x, -halfSizeAsUnit.y);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(spritePivot), spritePivot, null);
            }

            return(position - spritePivotPosition);
        }