public void SetText(string s) { textField.text = s; Vector2 newScale = textField.GetPreferredValues(s) + Vector2.one * padding; backPanel.localScale = new Vector3(newScale.x, newScale.y, 1); textField.rectTransform.sizeDelta = newScale; }
void Update() { var size = text.GetPreferredValues(); var oldStartPos = start.localPosition; oldStartPos.x = size.x * 0.5f + startOffset; start.localPosition = oldStartPos; var oldEndPos = endCompleted.localPosition; oldEndPos.x = -size.x * 0.5f - endOffset; endCompleted.localPosition = oldEndPos; // set target position var targetLocalPosition = Vector3.Lerp(start.localPosition, endCompleted.localPosition, currentTarget); target.localPosition = Vector3.Lerp(target.localPosition, targetLocalPosition, Time.deltaTime * 20); // set glass position glass.transform.position = Vector3.Lerp(glass.transform.position, GetGlassWorldPosition(), Time.deltaTime * 20); // Set Back Sprite float glassPercPos = Vector3.Distance(glass.transform.position, start.position) / Vector3.Distance(start.position, endCompleted.position); var oldPos = backSprite.transform.localPosition; oldPos.x = (start.localPosition.x + endCompleted.localPosition.x) * 0.5f; backSprite.transform.localPosition = oldPos; backSprite.donePercentage = 1 - glassPercPos; var oldScale = backSprite.transform.localScale; oldScale.x = (start.localPosition.x - endCompleted.localPosition.x) * 0.25f; backSprite.transform.localScale = oldScale; const float ALPHA_LERP_SPEED = 5.0f; var textColor = text.color; textColor.a = Mathf.Lerp(textColor.a, startTextColor.a * alpha, ALPHA_LERP_SPEED * Time.deltaTime); text.color = textColor; if (shineWhenNearTarget) { var distance = GetDistanceFromTarget(); glass.Shining = 1.0f - Mathf.SmoothStep(0, 1, (Mathf.Abs(distance) - minShineDistance) / (maxShineDistance - minShineDistance)); glass.Bad = Mathf.Lerp(glass.Bad, 1 - glass.Shining, Time.deltaTime * 5); glass.DistanceFactor = distance / 100; } else { glass.Shining = 0; glass.Bad = 0; glass.DistanceFactor = 0; } }
protected override void Awake() { //Debug.Log("TextContainer Awake() called."); m_rectTransform = this.rectTransform; if (m_rectTransform == null) { Vector2 oldPivot = m_pivot; m_rectTransform = gameObject.AddComponent<RectTransform>(); m_pivot = oldPivot; } m_textMeshPro = GetComponent(typeof(TextMeshPro)) as TextMeshPro; if (m_rect.width == 0 || m_rect.height == 0) { // Handling of Legacy lineLength property if (m_textMeshPro != null && m_textMeshPro.anchor != TMP_Compatibility.AnchorPositions.None) { Debug.LogWarning("Converting from using anchor and lineLength properties to Text Container.", this); m_isDefaultHeight = true; int anchor = (int)m_textMeshPro.anchor; // Special Handling if Baseline Anchor was used on the old object. if (anchor == 9) { switch (m_textMeshPro.alignment) { case TextAlignmentOptions.TopLeft: // Left m_textMeshPro.alignment = TextAlignmentOptions.BaselineLeft; break; case TextAlignmentOptions.Top: // Center m_textMeshPro.alignment = TextAlignmentOptions.Baseline; break; case TextAlignmentOptions.TopRight: // Right m_textMeshPro.alignment = TextAlignmentOptions.BaselineRight; break; case TextAlignmentOptions.TopJustified: // Left m_textMeshPro.alignment = TextAlignmentOptions.BaselineJustified; break; } anchor = 3; // left } m_anchorPosition = (TextContainerAnchors)anchor; m_pivot = GetPivot(m_anchorPosition); if (m_textMeshPro.lineLength == 72) { m_rect.size = m_textMeshPro.GetPreferredValues(m_textMeshPro.text); } else { m_rect.width = m_textMeshPro.lineLength; m_rect.height = m_textMeshPro.GetPreferredValues(m_rect.width, Mathf.Infinity).y; } } else // if (m_rectTransform.sizeDelta == new Vector2(100, 100)) { m_isDefaultWidth = true; m_isDefaultHeight = true; m_pivot = GetPivot(m_anchorPosition); m_rect.width = 20; m_rect.height = 5; m_rectTransform.sizeDelta = this.size; } m_margins = new Vector4(0, 0, 0, 0); UpdateCorners(); } }
protected void FitBGForText() { float width = label.GetPreferredValues(0, label.rectTransform.sizeDelta.y).x + offset; bgImage.size = new Vector2(width / 0.3f, bgImage.size.y); }