// Update is called once per frame void Update() { // updateSize(1f); bounds = tm.GetRenderedValues(); if (Mathf.Abs(bounds.x) < 100f) { wTar = bounds.x / 2; } else { wTar = 1f; } if (Mathf.Abs(bounds.y) < 100f) { h = bounds.y; } else { h = 2f; } float moreW = w + deltaW; if (moreW < wTar) { w = moreW; } else { float lessW = w - deltaW; if (lessW > wTar) { w = lessW; } } // Debug.Log("w: " + w + ", h: " + h); float moreS = fSize + deltaFSize; if (moreS < fSizeTar) { fSize = moreS; tm.fontSize = fSize; } else { float lessS = fSize - deltaFSize; if (lessS > fSizeTar) { fSize = lessS; tm.fontSize = fSize; } } }
public void SetTooltipDescription(Item item) { if (sb.Length > 0) { sb.Clear(); } itemTitleRect.sizeDelta = new Vector2(maxTooltipWidth, itemTitleRect.sizeDelta.y); itemDescriptionRect.sizeDelta = new Vector2(maxTooltipWidth, itemTitleRect.sizeDelta.y); itemTitle.SetText(item.itemName); itemTitle.ForceMeshUpdate(); Vector2 titleSize = itemTitle.GetRenderedValues(false); float effectiveItemTitleWidth = titleSize.x <= minTooltipWidth ? minTooltipWidth : titleSize.x < maxTooltipWidth ? titleSize.x : maxTooltipWidth; itemTitleRect.sizeDelta = new Vector2(effectiveItemTitleWidth, titleSize.y); if (item is ItemWeapon) { SetStatsForWeaponItem(item as ItemWeapon); } else if (item is ItemShield) { SetStatsForShieldItem(item as ItemShield); } else if (item is ItemArmor) { SetStatsForArmorItem(item as ItemArmor); } itemDescription.SetText(sb.ToString()); itemDescription.ForceMeshUpdate(); Vector2 descriptionSize = itemDescription.GetRenderedValues(false); float maxTextBoxLength = descriptionSize.x > titleSize.x ? descriptionSize.x : titleSize.x; float effectiveTooltipWidth = maxTextBoxLength <minTooltipWidth?minTooltipWidth : maxTextBoxLength> maxTooltipWidth ? maxTooltipWidth : maxTextBoxLength; itemDescriptionRect.sizeDelta = new Vector2(effectiveTooltipWidth, descriptionSize.y); Vector2 effectiveSize = new Vector2(effectiveTooltipWidth + horizontalPadding, descriptionSize.y + titleSize.y + verticalPadding); background.sizeDelta = effectiveSize; }
/// <summary> /// Chat bubble setup /// </summary> /// <param name="text"></param> public void Setup(string text) { //Dummy Text (Used to space all bubbles depending on text height) dummyText.text = text; //Message message.text = text; //Background message.ForceMeshUpdate(); //Assures that the "GetRenderedValues" allways gets the latest values Vector2 messageSize = message.GetRenderedValues(false); Vector2 padding = new Vector2(10, 10); background.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, messageSize.x + padding.x); //Resizes the text depending on message size (X) background.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, messageSize.y + padding.y); //Resizes the text depending on message size (Y) }
// Start is called before the first frame update void Start() { //find the text asset in children TMP_Text LowerThirdText = gameObject.GetComponentInChildren <TMP_Text>(); //set the text to the value stored in player prefs LowerThirdText.text = PlayerPrefs.GetString("lowerThird"); //force a update so that we can get the size LowerThirdText.ForceMeshUpdate(); //Grab the length of the text from text element Vector2 textBoxSize = LowerThirdText.GetRenderedValues(); //find the image asset - should be the first child GameObject imageObject = transform.GetChild(0).gameObject; //set the target width of the image - account for 20 margin left and right float targetBoxWidth = textBoxSize[0] + 40f; //set the size of the box imageObject.GetComponent <RectTransform>().sizeDelta = new Vector2(targetBoxWidth, 60); }