Author Topic: Box-Collider does not adjust when changing label text programatically  (Read 4144 times)

10FingerArmy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Box-Collider does not adjust when changing label text programatically
« Reply #1 on: February 17, 2014, 10:40:45 AM »
Thanks, I'll fix that.

favoyang

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Same issue in v3.6.2, but only happens if changes the UILabel.text while it's inactive.

YukioT

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Box-Collider does not adjust when changing label text programatically
« Reply #3 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. }
« Last Edit: November 20, 2014, 09:50:55 AM by YukioT »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Box-Collider does not adjust when changing label text programatically
« Reply #4 on: November 20, 2014, 08:40:11 PM »
It all works fine with 3.7.6.