Пример #1
0
        public void SetAnchorPreset(AnchorPreset preset)
        {
            int num = (int)preset;

            int[] vals = new int[2];
            vals[0] = num % 4;
            vals[1] = (int)Math.Floor(num / 4.0f);
            for (int i = 0; i < 2; i++)
            {
                double min = 0;
                double max = 0;

                switch (vals[i])
                {
                case 0:
                {
                    min = 0.5;
                    max = 0.5;
                    break;
                }

                case 1:
                {
                    min = 0;
                    max = 0;
                    break;
                }

                case 2:
                {
                    min = 1;
                    max = 1;
                    break;
                }

                case 3:
                {
                    min = 0;
                    max = 1;
                    break;
                }
                }
                if (i == 0)
                {
                    anchorMax.X = max;
                    anchorMin.X = min;
                }
                else
                {
                    anchorMax.Y = max;
                    anchorMin.Y = min;
                }
            }
        }
Пример #2
0
    public void Init(int index, AnchorPreset anchor, RectTransform parent)
    {
        rectTrans.SetParent(parent);

        if (anchor == AnchorPreset.TopLeft)
        {
            rectTrans.anchorMin = new Vector2(0, 1);
            rectTrans.anchorMax = new Vector2(0, 1);

            rectTrans.anchoredPosition = new Vector2(xPos + (index * xOffset), yPos);
            rectTrans.localScale       = new Vector2(1, 1);
        }

        if (anchor == AnchorPreset.TopRight)
        {
            rectTrans.anchorMin = new Vector2(1, 1);
            rectTrans.anchorMax = new Vector2(1, 1);

            rectTrans.anchoredPosition = new Vector2(-1 * (xPos + (index * xOffset)), yPos);
            rectTrans.localScale       = new Vector2(1, 1);
        }
    }
Пример #3
0
    public static void AddCaption(string content, Vector2 size, float fontSize, Color txtColor, bool fade, bool revertFade, float fadeTime, AnchorPreset preset = AnchorPreset.CENTER)
    {
        EnsureInterfacePresence();

        GameObject TxtObject = new GameObject("Caption");

        TxtObject.transform.SetParent(CanvasInstance.transform);
        RectTransform rect      = TxtObject.AddComponent <RectTransform>();
        Vector2       Placement = new Vector2(0.0f, 0.0f);

        TextMeshProUGUI tmp = TxtObject.AddComponent <TextMeshProUGUI>();

        tmp.text             = content;
        tmp.fontSize         = fontSize;
        tmp.enableAutoSizing = true;
        tmp.color            = txtColor;
        rect.sizeDelta       = size;

        //Adapt anchor for the given preset
        switch (preset)
        {
        case AnchorPreset.TOPLEFT:
        {
            rect.anchorMin = new Vector2(0.0f, 1.0f);
            rect.anchorMax = new Vector2(0.0f, 1.0f);
            Placement      = new Vector2(rect.sizeDelta.x * 0.55f, -(rect.sizeDelta.y * 1.05f));
            break;
        }

        case AnchorPreset.TOP:
        {
            rect.anchorMin = new Vector2(0.5f, 1.0f);
            rect.anchorMax = new Vector2(0.5f, 1.0f);
            Placement      = new Vector2(0.0f, -(rect.sizeDelta.y * 1.05f));
            break;
        }

        case AnchorPreset.TOPRIGHT:
        {
            rect.anchorMin = new Vector2(1.0f, 1.0f);
            rect.anchorMax = new Vector2(1.0f, 1.0f);
            Placement      = new Vector2(-(rect.sizeDelta.x * 0.55f), -(rect.sizeDelta.y * 1.05f));
            break;
        }

        case AnchorPreset.LEFT:
        {
            rect.anchorMin = new Vector2(0.0f, 0.5f);
            rect.anchorMax = new Vector2(0.0f, 0.5f);
            Placement      = new Vector2(rect.sizeDelta.x * 0.55f, 0.0f);
            break;
        }

        case AnchorPreset.CENTER:
        {
            rect.anchorMin = new Vector2(0.5f, 0.5f);
            rect.anchorMax = new Vector2(0.5f, 0.5f);
            Placement      = new Vector2(0.0f, 0.0f);
            break;
        }

        case AnchorPreset.RIGHT:
        {
            rect.anchorMin = new Vector2(1.0f, 0.5f);
            rect.anchorMax = new Vector2(1.0f, 0.5f);
            Placement      = new Vector2(-(rect.sizeDelta.x * 0.55f), 0.0f);
            break;
        }

        case AnchorPreset.BOTTOMLEFT:
        {
            rect.anchorMin = new Vector2(0.0f, 0.0f);
            rect.anchorMax = new Vector2(0.0f, 0.0f);
            Placement      = new Vector2(rect.sizeDelta.x * 0.55f, rect.sizeDelta.y * 0.55f);
            break;
        }

        case AnchorPreset.BOTTOM:
        {
            rect.anchorMin = new Vector2(0.5f, 0.0f);
            rect.anchorMax = new Vector2(0.5f, 0.0f);
            Placement      = new Vector2(0.0f, rect.sizeDelta.y * 0.55f);
            break;
        }

        case AnchorPreset.BOTTOMRIGHT:
        {
            rect.anchorMin = new Vector2(1.0f, 0.0f);
            rect.anchorMax = new Vector2(1.0f, 0.0f);
            Placement      = new Vector2(-(rect.sizeDelta.x * 0.55f), rect.sizeDelta.y * 0.55f);
            break;
        }

        default: break;
        }

        rect.anchoredPosition = Placement;

        if (fade)
        {
            Core.instance.StartCoroutine(InitiateFade(tmp, revertFade, fadeTime));
        }
    }
Пример #4
0
        public static void GetAnchor(AnchorPreset anchorPreset, out Vector2 anchorMin, out Vector2 anchorMax)
        {
            anchorMin = new Vector2(0.0f, 0.0f);
            anchorMax = new Vector2(1.0f, 1.0f);

            if (anchorPreset == AnchorPreset.TopLeft ||
                anchorPreset == AnchorPreset.MiddleLeft ||
                anchorPreset == AnchorPreset.BottomLeft ||
                anchorPreset == AnchorPreset.VertStretchLeft)
            {
                anchorMin.x = 0.0f;
                anchorMax.x = 0.0f;
            }

            if (anchorPreset == AnchorPreset.TopRight ||
                anchorPreset == AnchorPreset.MiddleRight ||
                anchorPreset == AnchorPreset.BottomRight ||
                anchorPreset == AnchorPreset.VertStretchRight)
            {
                anchorMin.x = 1.0f;
                anchorMax.x = 1.0f;
            }

            if (anchorPreset == AnchorPreset.TopCenter ||
                anchorPreset == AnchorPreset.MiddleCenter ||
                anchorPreset == AnchorPreset.BottonCenter ||
                anchorPreset == AnchorPreset.VertStretchCenter)
            {
                anchorMin.x = 0.5f;
                anchorMax.x = 0.5f;
            }

            if (anchorPreset == AnchorPreset.TopLeft ||
                anchorPreset == AnchorPreset.TopCenter ||
                anchorPreset == AnchorPreset.TopRight ||
                anchorPreset == AnchorPreset.HorStretchTop)
            {
                anchorMin.y = 1.0f;
                anchorMax.y = 1.0f;
            }

            if (anchorPreset == AnchorPreset.BottomLeft ||
                anchorPreset == AnchorPreset.BottonCenter ||
                anchorPreset == AnchorPreset.BottomRight ||
                anchorPreset == AnchorPreset.HorStretchBottom)
            {
                anchorMin.y = 0.0f;
                anchorMax.y = 0.0f;
            }

            if (anchorPreset == AnchorPreset.MiddleLeft ||
                anchorPreset == AnchorPreset.MiddleCenter ||
                anchorPreset == AnchorPreset.MiddleRight ||
                anchorPreset == AnchorPreset.HorStretchMiddle)
            {
                anchorMin.y = 0.5f;
                anchorMax.y = 0.5f;
            }

            if (anchorPreset == AnchorPreset.VertStretchLeft ||
                anchorPreset == AnchorPreset.VertStretchCenter ||
                anchorPreset == AnchorPreset.VertStretchRight ||
                anchorPreset == AnchorPreset.StretchAll)
            {
                anchorMin.y = 0.0f;
                anchorMax.y = 1.0f;
            }

            if (anchorPreset == AnchorPreset.HorStretchTop ||
                anchorPreset == AnchorPreset.HorStretchMiddle ||
                anchorPreset == AnchorPreset.HorStretchBottom ||
                anchorPreset == AnchorPreset.StretchAll)
            {
                anchorMin.x = 0.0f;
                anchorMax.x = 1.0f;
            }
        }
Пример #5
0
 public static void Set(this RectTransform thisTrans, float anchorX, float anchorY, float offsetX, float offsetY, AnchorPreset anchor)
 {
     thisTrans.anchoredPosition = new Vector2(anchorX, anchorY);
     thisTrans.offsetMin        = new Vector2(offsetX, offsetY);
     thisTrans.offsetMax        = new Vector2(offsetX, offsetY);
     GetAnchor(anchor, out Vector2 anchorMin, out Vector2 anchorMax);
     SetAnchor(thisTrans, anchorMin, anchorMax);
 }
Пример #6
0
 public static void Set(this RectTransform thisTrans, float x, float y, AnchorPreset anchor)
 {
     thisTrans.anchoredPosition = new Vector2(x, y);
     GetAnchor(anchor, out Vector2 anchorMin, out Vector2 anchorMax);
     SetAnchor(thisTrans, anchorMin, anchorMax);
 }
Пример #7
0
    public static void SetAnchor(this RectTransform rectTransform, AnchorPreset anchorPreset, float offsetX, float offsetY)
    {
        rectTransform.anchoredPosition = new Vector2(offsetX, offsetY);

        switch (anchorPreset)
        {
        case AnchorPreset.TopLeft:
            rectTransform.anchorMin = new Vector2(0, 1);
            rectTransform.anchorMax = new Vector2(0, 1);
            break;

        case AnchorPreset.TopCenter:
            rectTransform.anchorMin = new Vector2(0.5f, 1);
            rectTransform.anchorMax = new Vector2(0.5f, 1);
            break;

        case AnchorPreset.TopRight:
            rectTransform.anchorMin = new Vector2(1, 1);
            rectTransform.anchorMax = new Vector2(1, 1);
            break;

        case AnchorPreset.MiddleLeft:
            rectTransform.anchorMin = new Vector2(0, 0.5f);
            rectTransform.anchorMax = new Vector2(0, 0.5f);
            break;

        case AnchorPreset.MiddleCenter:
            rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
            rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
            break;

        case AnchorPreset.MiddleRight:
            rectTransform.anchorMin = new Vector2(1, 0.5f);
            rectTransform.anchorMax = new Vector2(1, 0.5f);
            break;

        case AnchorPreset.BottomLeft:
            rectTransform.anchorMin = new Vector2(0, 0);
            rectTransform.anchorMax = new Vector2(0, 0);
            break;

        case AnchorPreset.BottomCenter:
            rectTransform.anchorMin = new Vector2(0.5f, 0);
            rectTransform.anchorMax = new Vector2(0.5f, 0);
            break;

        case AnchorPreset.BottomRight:
            rectTransform.anchorMin = new Vector2(1, 0);
            rectTransform.anchorMax = new Vector2(1, 0);
            break;

        case AnchorPreset.HorStretchTop:
            rectTransform.anchorMin = new Vector2(0, 1);
            rectTransform.anchorMax = new Vector2(1, 1);
            break;

        case AnchorPreset.HorStretchMiddle:
            rectTransform.anchorMin = new Vector2(0, 0.5f);
            rectTransform.anchorMax = new Vector2(1, 0.5f);
            break;

        case AnchorPreset.HorStretchBottom:
            rectTransform.anchorMin = new Vector2(0, 0);
            rectTransform.anchorMax = new Vector2(1, 0);
            break;

        case AnchorPreset.VertStretchLeft:
            rectTransform.anchorMin = new Vector2(0, 0);
            rectTransform.anchorMax = new Vector2(0, 1);
            break;

        case AnchorPreset.VertStretchCenter:
            rectTransform.anchorMin = new Vector2(0.5f, 0);
            rectTransform.anchorMax = new Vector2(0.5f, 1);
            break;

        case AnchorPreset.VertStretchRight:
            rectTransform.anchorMin = new Vector2(1, 0);
            rectTransform.anchorMax = new Vector2(1, 1);
            break;

        case AnchorPreset.StretchAll:
            rectTransform.anchorMin = new Vector2(0, 0);
            rectTransform.anchorMax = new Vector2(1, 1);
            break;

        default:
            Debug.LogErrorFormat("Not supported anchor preset {0}", anchorPreset);
            break;
        }
    }
Пример #8
0
 public static void SetAnchor(this RectTransform rectTransform, AnchorPreset anchorPreset)
 {
     rectTransform.SetAnchor(anchorPreset, 0, 0);
 }
 public static void SetAnchor(this RectTransform rectTransform, AnchorPreset anchorPresetX, AnchorPreset anchorPresetY, (float x, float y) pivot)