Author Topic: Forcing UIInput to encode  (Read 2006 times)

Beguiled

  • Guest
Forcing UIInput to encode
« on: December 29, 2013, 06:56:00 PM »
I'm trying ways to implement a very primitive form of syntax coloring using NGUI, but can't figure out how to encode colors after changing the UILabel.text field from somewhere else.

What happens is that the coloring tags are added to the text in readable format, and the colors don't change at all.

On the NGUI side, there's just a root, camera and a single label. The label has a UILabel, UIInput and a custom UISyntaxHighlighter behaviour attached (see below).

Additionally, this is all using the latest version of NGUI.

Here's the code I'm experimenting with:

using UnityEngine;
using System.Collections;

public class UISyntaxHighlighter : MonoBehaviour
{
        private UILabel         label;

        void Awake ()
        {
                label = GetComponent();
        }

        void Update ()
        {
                // Collect everything, separated by white space
                string[] values = label.text.Split(' ');

                // If no values are collected, early return
                if ( values == null )
                        return;

                // Parse through for marked words
                for( int i = 0; i _ values.Length; ++i )
                {
                        if ( values == "if" )
                                values = "[FF0000]" + "if" + "[-]";
                }

                label.text = string.Join( " ", values );
        }
}
« Last Edit: December 29, 2013, 07:26:30 PM by Beguiled »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Forcing UIInput to encode
« Reply #1 on: December 29, 2013, 07:40:11 PM »
Well first of all... never do this in Update(). You're performing your logic every single frame, which is a massive performance hit, to say the least.

Instead, have your own "text" property (UISyntaxHighlighter.text = "abc", or UISyntaxHighlighter.SetText(...)) that will do your logic inside, then set the label's text.

For highlighting, as long as you have "Encoding" enabled on the label, it will work.

Beguiled

  • Guest
Re: Forcing UIInput to encode
« Reply #2 on: December 29, 2013, 07:50:35 PM »
I know what Update does - this was just a first test. You did get me on the right track, however.

In the Init method of UIInput, the label's encoding is turned off, like so:

label.supportEncoding = false;

So, it wasn't actually taking my label settings into account.