Пример #1
0
    public void Setup(string word, WordManager manager)
    {
        this.word    = word;
        this.manager = manager;

        text = GetComponent <TMP_Text>();
        text.autoSizeTextContainer = true;
        text.text = word;

        backgroundMesh        = GetComponentInChildren <SlicedMesh>();
        backgroundMesh.Width  = text.GetPreferredValues().x + 0.4f;
        backgroundMesh.Height = text.GetPreferredValues().y + 0.2f;
        backgroundMesh.Margin = 0.25f;
        backgroundMesh.transform.Translate(new Vector3(-text.GetPreferredValues().x / 2 - 0.2f, -text.GetPreferredValues().y / 2 - 0.1f, 0));
    }
Пример #2
0
        void Awake()
        {
            // Get a reference to the TMP text component if one already exists otherwise add one.
            // This example show the convenience of having both TMP components derive from TMP_Text.
            if (ObjectType == 0)
            {
                m_text = GetComponent <TextMeshPro>() ?? gameObject.AddComponent <TextMeshPro>();
            }
            else
            {
                m_text = GetComponent <TextMeshProUGUI>() ?? gameObject.AddComponent <TextMeshProUGUI>();
            }

            // Load a new font asset and assign it to the text object.
            m_text.font = Resources.Load <TMP_FontAsset>("Fonts & Materials/Anton SDF");

            // Load a new material preset which was created with the context menu duplicate.
            m_text.fontSharedMaterial = Resources.Load <Material>("Fonts & Materials/Anton SDF - Drop Shadow");

            // Set the size of the font.
            m_text.fontSize = 120;

            // Set the text
            m_text.text = "A <#0080ff>simple</color> line of text.";

            // Get the preferred width and height based on the supplied width and height as opposed to the actual size of the current text container.
            Vector2 size = m_text.GetPreferredValues(Mathf.Infinity, Mathf.Infinity);

            // Set the size of the RectTransform based on the new calculated values.
            m_text.rectTransform.sizeDelta = new Vector2(size.x, size.y);
        }
Пример #3
0
    // Start is called before the first frame update
    void Start()
    {
        text.text = folderName;

        textRectTransform               = text.gameObject.GetComponent <RectTransform>();
        textRectTransform.sizeDelta     = new Vector2(textRectTransform.sizeDelta.x, text.GetPreferredValues().y);
        textRectTransform.localPosition = new Vector3(0, baseTextPos - textRectTransform.rect.height * textRectTransform.localScale.y / 2);

        iconSpriteRenderer = icon.GetComponent <MeshCollider>();
        // topLeft calculated from the width of the text
        Vector3 topLeft     = icon.transform.localPosition - new Vector3(textRectTransform.rect.width * textRectTransform.localScale.x / 2, -iconSpriteRenderer.bounds.size.y * iconSpriteRenderer.transform.localScale.y / 2);
        Vector3 bottomRight = textRectTransform.transform.localPosition + new Vector3(textRectTransform.rect.width * textRectTransform.localScale.x / 2, -textRectTransform.rect.height * textRectTransform.localScale.y / 2);

        boxCollider        = GetComponent <BoxCollider>();
        boxCollider.center = (topLeft + bottomRight) / 2;
        Vector3 size = topLeft - bottomRight;

        size.y           = size.y + boxColliderPaddingY;
        size.z           = boxColliderSizeZ;
        boxCollider.size = new Vector3(Mathf.Abs(size.x), Mathf.Abs(size.y), Mathf.Abs(size.z));

        hoverSpriteRenderer.transform.localPosition = new Vector3(boxCollider.center.x, boxCollider.center.y, hoverSpriteRenderer.transform.localPosition.z);
        hoverSpriteRenderer.size    = new Vector2(boxCollider.size.x / hoverSpriteRenderer.transform.localScale.x, boxCollider.size.y / hoverSpriteRenderer.transform.localScale.y);
        hoverSpriteRenderer.enabled = false;

        selectSpriteRenderer.transform.localPosition = hoverSpriteRenderer.transform.localPosition;
        selectSpriteRenderer.size    = hoverSpriteRenderer.size;
        selectSpriteRenderer.enabled = hoverSpriteRenderer.enabled;
    }
Пример #4
0
        protected virtual void Update()
        {
            // if (text.text == lastText &&
            //     lastSize == rectTransform.rect.size &&
            //     ControlAxes == lastControlAxes)
            // {
            //     return;
            // }

            var preferredSize = text.GetPreferredValues(MaxX, MaxY);

            preferredSize.x = Mathf.Clamp(preferredSize.x, MinX, MaxX);
            preferredSize.y = Mathf.Clamp(preferredSize.y, MinY, MaxY);
            preferredSize  += Padding;

            if ((ControlAxes & Mode.Horizontal) != 0)
            {
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
                parentRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
            }

            if ((ControlAxes & Mode.Vertical) != 0)
            {
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
                parentRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
            }

            lastText        = text.text;
            lastSize        = rectTransform.rect.size;
            lastControlAxes = ControlAxes;
        }
Пример #5
0
    protected virtual void Update()
    {
        if (!_isTextNull && (Text.text != _lastText || _lastSize != _selfRectTransform.rect.size || _forceRefresh || ControlAxes != _lastControlAxes))
        {
            var preferredSize = Text.GetPreferredValues(MaxX, MaxY);
            preferredSize.x = Mathf.Clamp(preferredSize.x, MinX, MaxX);
            preferredSize.y = Mathf.Clamp(preferredSize.y, MinY, MaxY);
            preferredSize  += Padding;

            if ((ControlAxes & Mode.Horizontal) != 0)
            {
                _selfRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
                if (ResizeTextObject)
                {
                    _textRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
                }
            }
            if ((ControlAxes & Mode.Vertical) != 0)
            {
                _selfRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
                if (ResizeTextObject)
                {
                    _textRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
                }
            }

            _lastText        = Text.text;
            _lastSize        = _selfRectTransform.rect.size;
            _lastControlAxes = ControlAxes;
            _forceRefresh    = false;
        }
    }
Пример #6
0
    private void Resize()
    {
        var size         = _textElement.GetPreferredValues(_textElement.text);
        var currentDelta = _textElement.rectTransform.sizeDelta;
        var newSizeDelta = new Vector2(Width ? size.x : currentDelta.x, Height ? size.y : currentDelta.y);

        _textElement.rectTransform.sizeDelta = newSizeDelta;
    }
Пример #7
0
        public void ShowTooltip(string text)
        {
            m_Tooltip.SetActive(true);
            m_TooltipText.text = text;


            (m_Tooltip.transform as RectTransform).sizeDelta = m_TooltipText.GetPreferredValues(text);

            m_CursorUpdate = StartCoroutine(CursorUpdate());
        }
Пример #8
0
    public void ShowTooltip(string tooltipString)
    {
        gameObject.SetActive(true);
        tooltipText.text = tooltipString;

        var     preferredSize  = tooltipText.GetPreferredValues();
        Vector2 backgroundSize = new Vector2(tooltipText.GetComponent <RectTransform>().rect.width + textPadding * 2f, preferredSize.y + textPadding * 2f);

        tooltipBackgroundRect.sizeDelta = backgroundSize;
        SetTooltipPos();
    }
Пример #9
0
        public void Update()
        {
            Vector2 size = label.GetPreferredValues(label.text);

            rt.sizeDelta = size + margin;

            if (follow != null)
            {
                Vector2 screenPoint = cam.WorldToViewportPoint(follow.position);
                Vector2 WorldObject_ScreenPosition = new Vector2(
                    ((screenPoint.x * canvasRect.sizeDelta.x) - (canvasRect.sizeDelta.x * 0.5f)),
                    ((screenPoint.y * canvasRect.sizeDelta.y) - (canvasRect.sizeDelta.y * 0.5f)));
                rt.anchoredPosition = WorldObject_ScreenPosition;
            }
        }
Пример #10
0
        void Start()
        {
            // Get a reference to the TMP text component.
            m_text = GetComponent <TextMeshProUGUI>();

            // Set the size of the font.
            m_text.fontSize = 18;

            // Set the text
            m_text.text = "A <#0080ff>simple</color> line of text.";

            // Get the preferred width and height based on the supplied width and height as opposed to the actual size of the current text container.
            Vector2 size = m_text.GetPreferredValues(Mathf.Infinity, Mathf.Infinity);

            // Set the size of the RectTransform based on the new calculated values.
            m_text.rectTransform.sizeDelta = new Vector2(size.x, size.y);
        }
Пример #11
0
        public void Refresh()
        {
            if (m_Lock)
            {
                return;
            }

            m_Lock = true;

            if (enabled == false)
            {
                return;
            }

            if (m_Text == null)
            {
                return;
            }

            var preferredSize = m_Text.GetPreferredValues(MaxX, MaxY);

            preferredSize.x = Mathf.Clamp(preferredSize.x, MinX, MaxX);
            preferredSize.y = Mathf.Clamp(preferredSize.y, MinY, MaxY);
            preferredSize  += m_Padding;

            if ((m_ControlAxes & Mode.X) != 0)
            {
                m_SelfRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
                if (m_ResizeTextObject)
                {
                    m_TextRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
                }
            }
            if ((m_ControlAxes & Mode.Y) != 0)
            {
                m_SelfRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
                if (m_ResizeTextObject)
                {
                    m_TextRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
                }
            }

            m_Lock = false;
        }
Пример #12
0
        void Awake()
        {
            TIME      = 0;
            TruLength = 100000;
            n         = -1;
            if (ObjectType == 0)
            {
                m_text = GetComponent <TextMeshPro>() ?? gameObject.AddComponent <TextMeshPro>();
            }
            else
            {
                m_text = GetComponent <TextMeshProUGUI>() ?? gameObject.AddComponent <TextMeshProUGUI>();
            }
            m_text.font = Resources.Load <TMP_FontAsset>("Fonts & Materials/Anton SDF");
            m_text.fontSharedMaterial = Resources.Load <Material>("Fonts & Materials/Anton SDF - Outline");
            m_text.color     = new Color32(255, 255, 255, 255);
            m_text.fontSize  = 5;
            m_text.alignment = TextAlignmentOptions.Center;
            m_text.text      = ".                                                                                                                                                          .";                //A <#0080ff>simple</color> line of text.";
            Vector2 size = m_text.GetPreferredValues(Mathf.Infinity, Mathf.Infinity);

            m_text.rectTransform.sizeDelta = new Vector2(size.x, size.y);
        }
Пример #13
0
    protected virtual void Update()
    {
        RectTransform rt = GetComponent <RectTransform>();

        if (text != null && (text.text != lastText || lastSize != rt.rect.size || forceRefresh))
        {
            lastText = text.text;
            Vector2 preferredSize = text.GetPreferredValues(MaxX, MaxY);
            preferredSize.x = Mathf.Clamp(preferredSize.x, MinX, MaxX);
            preferredSize.y = Mathf.Clamp(preferredSize.y, MinY, MaxY);
            preferredSize  += padding;

            if ((controlAxes & Mode.Horizontal) != 0)
            {
                rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, preferredSize.x);
            }
            if ((controlAxes & Mode.Vertical) != 0)
            {
                rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, preferredSize.y);
            }
            lastSize     = rt.rect.size;
            forceRefresh = false;
        }
    }
Пример #14
0
            public KEY(KEYBOARD kb, Vector2 position, string text, float width, float height, Color color)
            {
                value   = text;
                this.kb = kb;

                name = text;

                mybutton = Button.Instantiate(kb.BaseButton, kb.container, false);

                (mybutton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
                (mybutton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);

                TMP_Text txt = mybutton.GetComponentInChildren <TMP_Text>();

                mybutton.ToggleWordWrapping(false);

                mybutton.transform.localScale = new Vector3(kb.scale, kb.scale, 1.0f);
                mybutton.SetButtonTextSize(5f);
                mybutton.SetButtonText(text);
                mybutton.name = $"SRM_{text}";
                mybutton.GetComponentInChildren <Image>().color = color;

                if (width == 0)
                {
                    Vector2 v = txt.GetPreferredValues(text);
                    v.x  += 10f;
                    v.y  += 2f;
                    width = v.x;
                }

                // Adjust starting position so button aligns to upper left of current drawing position

                position.x += kb.scale * width / 2;
                position.y -= kb.scale * height / 2;
                (mybutton.transform as RectTransform).anchoredPosition = position;

                (mybutton.transform as RectTransform).sizeDelta = new Vector2(width, height);

                kb.currentposition.x += width * kb.scale + kb.padding;

                mybutton.onClick.RemoveAllListeners();

                mybutton.onClick.AddListener(delegate()
                {
                    if (keyaction != null)
                    {
                        keyaction(this);
                        kb.DrawCursor();
                        return;
                    }

                    if (value.EndsWith("%CR%"))
                    {
                        kb.KeyboardText.text += value.Substring(0, value.Length - 4);
                        kb.Enter(this);
                        kb.DrawCursor();

                        return;
                    }

                    {
                        string x = kb.Shift ? shifted : value;
                        if (x == "")
                        {
                            x = value;
                        }
                        if (kb.Caps)
                        {
                            x = value.ToUpper();
                        }
                        kb.KeyboardText.text += x;
                        kb.DrawCursor();
                    }
                });
                HoverHint _MyHintText = UIHelper.AddHintText(mybutton.transform as RectTransform, value);
            }
            public KEY(KEYBOARD kb, Vector2 position, string text, float width, float height, Color color)
            {
                value   = text;
                this.kb = kb;

                name     = text;
                mybutton = Button.Instantiate(kb.BaseButton, kb.container, false);
                GameObject.Destroy(mybutton.GetComponent <UIKeyboardKey>());

                Polyglot.LocalizedTextMeshProUGUI localizer = mybutton.GetComponentInChildren <Polyglot.LocalizedTextMeshProUGUI>(true);
                if (localizer != null)
                {
                    GameObject.Destroy(localizer);
                }
                ExternalComponents externalComponents = mybutton.gameObject.AddComponent <ExternalComponents>();
                TextMeshProUGUI    textMesh           = mybutton.GetComponentInChildren <TextMeshProUGUI>();

                textMesh.richText = true;
                externalComponents.components.Add(textMesh);

                (mybutton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f);
                (mybutton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f);

                TMP_Text txt = mybutton.GetComponentInChildren <TMP_Text>();

                mybutton.ToggleWordWrapping(false);
                mybutton.transform.localScale = new Vector3(kb.scale, kb.scale, 1.0f);
                mybutton.SetButtonTextSize(5f);
                mybutton.SetButtonText(text);
                mybutton.GetComponentInChildren <Image>().color = color;

                if (width == 0)
                {
                    Vector2 v = txt.GetPreferredValues(text);
                    v.x  += 10f;
                    v.y  += 2f;
                    width = v.x;
                }

                // Adjust starting position so button aligns to upper left of current drawing position
                position.x += kb.scale * width / 2;
                position.y -= kb.scale * height / 2;
                (mybutton.transform as RectTransform).anchoredPosition = position;

                (mybutton.transform as RectTransform).sizeDelta = new Vector2(width, height);

                kb.currentposition.x += width * kb.scale + kb.padding;

                mybutton.onClick.RemoveAllListeners();

                mybutton.onClick.AddListener(delegate()
                {
                    if (keyaction != null)
                    {
                        keyaction(this);
                        kb.DrawCursor();
                        return;
                    }

                    if (value.EndsWith("%CR%"))
                    {
                        kb.KeyboardText.text += value.Substring(0, value.Length - 4);
                        kb.Enter(this);
                        kb.DrawCursor();
                        return;
                    }

                    string x = kb.Shift ? shifted : value;
                    if (x == "")
                    {
                        x = value;
                    }

                    if (kb.Caps)
                    {
                        x = value.ToUpper();
                    }

                    kb.KeyboardText.text += x;
                    kb.DrawCursor();
                });

                HoverHint _MyHintText = mybutton.gameObject.AddComponent <HoverHint>();

                _MyHintText.text = value;
                _MyHintText.SetField("_hoverHintController", Resources.FindObjectsOfTypeAll <HoverHintController>().First());
            }
Пример #16
0
        public override void SetQuestion(Question q, UnityAction <Question> answeredEvent, UISkinData skinData = null)
        {
            base.SetQuestion(q, answeredEvent, skinData);

            radioGridQuestion     = question as RadioGridQuestion;
            instructionsText.text = question.instructions;
            idText.text           = question.id;


            questionItems = new List <List <Toggle> >();
            VerticalLayoutGroup panelLayout = itemsUI.GetComponent <VerticalLayoutGroup>();

            panelLayout.spacing        = 0.0f;
            panelLayout.padding.left   = 0;
            panelLayout.padding.right  = 0;
            panelLayout.padding.top    = 0;
            panelLayout.padding.bottom = 1;
            for (int i = 0; i < radioGridQuestion.q_text.Length; i++)
            {
                GameObject    textObj = Instantiate(labelPrefab);
                LayoutElement layout  = textObj.GetComponent <LayoutElement>();
                layout.enabled         = true;
                layout.minHeight       = 1;
                layout.minWidth        = 1;
                layout.preferredWidth  = 2;
                layout.preferredHeight = 1.5f;


                TMP_Text questionText = textObj.GetComponent <TMP_Text>();

                questionText.text      = radioGridQuestion.q_text[i].text;
                questionText.alignment = TextAlignmentOptions.Bottom;
                Vector2 textSize = questionText.GetPreferredValues(radioGridQuestion.q_text[i].text);
                //questionText.margin = new Vector4(0,0,1,-0.75f);

                textObj.transform.parent        = itemsUI;
                textObj.transform.localPosition = Vector3.zero;
                textObj.transform.localRotation = Quaternion.identity;
                textObj.transform.localScale    = textObj.transform.parent.localScale;
                //RadioGroup radioGroup = new RadioGroup(radioGridQuestion.q_text[i].id);
                List <Toggle> toggles = new List <Toggle>();
                //radioGroup.OnGroupSelected += OnItemSelected;
                GameObject itemsContainer = new GameObject("itemsContainer", typeof(RectTransform));
                itemsContainer.transform.parent        = itemsUI;
                itemsContainer.transform.localPosition = Vector3.zero;
                itemsContainer.transform.localRotation = Quaternion.identity;
                itemsContainer.transform.localScale    = itemsContainer.transform.parent.localScale;
                HorizontalLayoutGroup horizontalLayoutGroup = itemsContainer.AddComponent <HorizontalLayoutGroup>();
                horizontalLayoutGroup.childControlHeight = true;
                horizontalLayoutGroup.childControlWidth  = true;
                //horizontalLayoutGroup.spacing = -2;


                for (int j = 0; j < radioGridQuestion.labels.Length; j++)
                {
                    GameObject          container           = new GameObject("container", typeof(RectTransform));
                    VerticalLayoutGroup verticalLayoutGroup = container.AddComponent <VerticalLayoutGroup>();
                    verticalLayoutGroup.childControlWidth  = true;
                    verticalLayoutGroup.childControlHeight = true;
                    verticalLayoutGroup.spacing            = 0.2f;

                    container.transform.parent        = itemsContainer.transform;
                    container.transform.parent        = container.transform;
                    container.transform.localPosition = Vector3.zero;
                    container.transform.localRotation = Quaternion.identity;
                    container.transform.localScale    = container.transform.parent.localScale;

                    GameObject label     = Instantiate(labelPrefab);
                    TMP_Text   labelText = label.GetComponent <TMP_Text>();
                    labelText.GetComponent <SkinText>().textFormat = TextFormat.Small;
                    labelText.text                = radioGridQuestion.labels[j];
                    labelText.alignment           = TextAlignmentOptions.Center;
                    labelText.fontStyle           = FontStyles.Italic;
                    label.transform.parent        = container.transform;
                    label.transform.localPosition = Vector3.zero;
                    label.transform.localRotation = Quaternion.identity;
                    label.transform.localScale    = Vector3.one;


                    LayoutElement labelLayout = labelText.GetComponent <LayoutElement>();
                    labelLayout.enabled         = true;
                    labelLayout.minWidth        = skinData.toggleSize.x;
                    labelLayout.minHeight       = 0.5f;              // skinData.toggleSize.y;
                    labelLayout.preferredWidth  = skinData.toggleSize.x;
                    labelLayout.preferredHeight = 1.0f;              // skinData.toggleSize.y;


                    GameObject item   = Instantiate(radioItemPrefab);
                    Toggle     toggle = item.GetComponent <Toggle>();
                    //LayoutElement toggleLayout = textObj.GetComponent<LayoutElement>();
                    //toggleLayout.enabled = true;
                    //toggleLayout.minHeight = 1;
                    //toggleLayout.minWidth = 1;
                    //toggleLayout.preferredWidth = 1;
                    //toggleLayout.preferredHeight = 1;
                    //radioGroup.AddToggle(toggle);
                    toggle.SetIsOnWithoutNotify(false);
                    toggle.onValueChanged.AddListener((val) => {
                        OnItemSelected(toggle, radioGridQuestion.id, val);
                    });

                    toggles.Add(toggle);


                    item.transform.parent        = container.transform;
                    item.transform.localPosition = new Vector3(0, 0, skinData.radioZOffset);
                    //item.transform.localPosition = Vector3.zero;
                    item.transform.localRotation = Quaternion.identity;
                    item.transform.localScale    = Vector3.one;
                }
                //radioGroup.Init();
                //questionItems.Add(radioGroup);
                questionItems.Add(toggles);
            }

            //Canvas.ForceUpdateCanvases();
            //LayoutGridElements(); //Invoke("LayoutGridElements",0.16f);
            //Canvas.ForceUpdateCanvases();
        }