Author Topic: best way to handle text input and backspace with UILabel  (Read 2176 times)

Fractalbase

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
best way to handle text input and backspace with UILabel
« on: April 28, 2014, 11:00:31 PM »
I'm attempting to create a text-input panel for my game, and I implemented one solution with UILabel.  I'm still learning NGUI, but the issue I'm running into is that backspace is only recognized one time after each press of return.  My code is included below.  Any help?

thanks,
fb

----

using System;
using System.Collections.Generic;
using UnityEngine;



public class DialogController : MonoBehaviour
{
    public int MaxLines;
    public int MaxLength;

    private List<String> textFrame;

    public DialogController()
    {
    }

    // Use this for initialization
    void Start()
    {
        if (textFrame == null)
        {
            textFrame = new List<string>();
            for (int index = 0; index < MaxLines; ++index)
            {
                textFrame.Add(String.Empty);
            }
        }
    }

    // Update is called once per frame
    private void Update()
    {
        String entered = Input.inputString;
        int last = (textFrame.Count - 1);

        textFrame[last] += entered;
        if (textFrame[last].Length > MaxLength)
        {
            textFrame[last] = textFrame[last].Substring(0, MaxLength);
        }

        if (Input.GetKeyDown(KeyCode.Return))
        {
            textFrame.RemoveAt(0);
            textFrame.Add(entered);
        }

        //TODO: only works for one backspace per line
        if (Input.GetKeyDown(KeyCode.Backspace))
        {
            int currentLength = textFrame[last].Length;
            textFrame[last] = textFrame[last].Substring(0, (currentLength - 1));
        }

        PopulateContents();
    }

    public void PopulateContents()
    {
        String content = String.Empty;
        foreach (String line in textFrame)
        {
            content += (line + '\n');
        }
        GetComponent<UILabel>().text = content;
    }

}


apparenthorizondev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: best way to handle text input and backspace with UILabel
« Reply #1 on: April 29, 2014, 12:21:46 AM »
What about the input field that NGUI has?

Fractalbase

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: best way to handle text input and backspace with UILabel
« Reply #2 on: April 30, 2014, 08:54:42 PM »
I haven't looked into NGUI's input yet.  I've somewhat written my input code already using Unity input -- will have to investigate.