Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: 10FingerArmy on February 16, 2014, 05:00:09 AM

Title: Box-Collider does not adjust when changing label text programatically
Post by: 10FingerArmy on February 16, 2014, 05:00:09 AM
I do have a UILabel with a box collider and activated auto-adjust in it's widget. This works fine in the editor. If I change the text at runtime though the collider does not change automatically. I have to call

text.GetComponent<UILabel>().text = "New Text";
text.GetComponent<UIWidget>().ResizeCollider();

to make it work which is unfortunate. Is there a way to have it resize automatically?

NGUI 3.4.9
Title: Re: Box-Collider does not adjust when changing label text programatically
Post by: ArenMook on February 17, 2014, 10:40:45 AM
Thanks, I'll fix that.
Title: Re: Box-Collider does not adjust when changing label text programatically
Post by: favoyang on June 10, 2014, 10:35:30 PM
Same issue in v3.6.2, but only happens if changes the UILabel.text while it's inactive.
Title: Re: Box-Collider does not adjust when changing label text programatically
Post by: YukioT on November 20, 2014, 09:16:05 AM
I still have the same issu in v3.7.4. Any workaround?

Edit: tried using an invisible widget as the gameobject for the collider, and anchoring it to the label. The widget resizes correctly but the collider still doesn't.

Edit2: I wrote a little piece of script that calls a resize on the collider if it finds a difference, during Update(). Just attach it to the label that has a collider, works as-is.

Quote from: UpdateLabelCollider.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(Collider), typeof(UILabel))]
  5. public class UpdateLabelCollider : MonoBehaviour
  6. {
  7.         private Collider _col;
  8.         private UILabel _lab;
  9.  
  10.         void OnEnable()
  11.         {
  12.                 _col= gameObject.GetComponent<Collider>();
  13.                 _lab= gameObject.GetComponent<UILabel>();
  14.         }
  15.        
  16.         void Update ()
  17.         {
  18.                 if(_lab.width != _col.bounds.size.x || _lab.height != _col.bounds.size.y)
  19.                         _lab.ResizeCollider();
  20.         }
  21. }
Title: Re: Box-Collider does not adjust when changing label text programatically
Post by: ArenMook on November 20, 2014, 08:40:11 PM
It all works fine with 3.7.6.