Author Topic: Altering ui lable from a different ui script  (Read 3440 times)

dRosser

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Altering ui lable from a different ui script
« on: November 20, 2013, 01:40:05 PM »
This I'm sure is something easy that I'm missing, but I've been trying to get the color of text in a UILabel to update and change based on the setting of a color field on it's parent object. I've tried to manipulate the color of the UIWidget that is attached to the label, but it never forwards the color to the text itself.

Is there an easy process to have the color choice forwarded from a different editor script?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Altering ui lable from a different ui script
« Reply #1 on: November 20, 2013, 03:25:08 PM »
UIWidget that's attached to a label? I'm not sure what you mean. UILabel derives from UIWidget. You don't need a UIWidget there.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Altering ui lable from a different ui script
« Reply #2 on: November 20, 2013, 05:44:07 PM »
You reference the UILabel in your other script and then set the values when you want.

  1. //other script
  2. [SerializeField] UILabel myLabel;
  3.  
  4. void MyUpdateMethod(){
  5. myLabel.text="New text";
  6. myLabel.color = Color.blue;
  7. }
  8.  

dRosser

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Altering ui lable from a different ui script
« Reply #3 on: November 21, 2013, 01:04:08 PM »
Sorry I wasn't specific enough Aren, I was referring to the color field in the inspector of the UILabel under the widget header.

So the problem I have with altering the color variable of the UILabel is that it doesn't effect the actual color of the text, just the color field on the inspector; at least not when I'm attempting to do it from a different editor script. Some example code:

public class TextObject : MonoBehaviour
{
     public UILabel label;
}

[CustomEditor(typeof(TextObject))]
public class TextFieldInspector : Editor
{
   public override void OnInspectorGUI ()
   {
      
      TextObject _target = (TextObject)target;
      _target.label = _target.gameObject.GetComponentInChildren<UILabel>();

      _target.TextColor = EditorGUILayout.ColorField("Text Color", _target.TextColor);
      
      if (GUI.changed)
      {
         _target.label.color = _target.TextColor;
      }
   }
}

Where the UILabel i'm attempting to effect is a child of a panel in the top level object's children. This code reproduces my problem that it will update the color swatch in the inspector of the UILabel, but it doesn't impact the actual color of the text, and when the game is started all the information is reset to it's previous value before the color field in the editor script was altered.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Altering ui lable from a different ui script
« Reply #4 on: November 21, 2013, 01:47:39 PM »
Consider switching to SerializedProperties:
  1. UILabel lbl = _target.gameObject.GetComponentInChildren<UILabel>();
  2. SerializedObject so = new SerializedObject(lbl);
  3. so.Update();
  4. NGUIEditorTools.DrawProperty("Color", so, "mColor");
  5. so.ApplyModifiedProperties();
It's a more up-to-date way of doing things, and works correctly with Undo, and it actually marks the widget as dirty correctly. You're basically never calling UnityEditor.EditorUtility.SetDirty(lbl), which is why it never updates. Edit time is different than run-time.

dRosser

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Altering ui lable from a different ui script
« Reply #5 on: November 21, 2013, 02:29:52 PM »
Ah, I see. The serialized property works much better too. Thanks for your help!