Search Unity

Improving the Input Field, or an alternative?

Discussion in 'UGUI & TextMesh Pro' started by shoffing, Apr 14, 2015.

  1. shoffing

    shoffing

    Joined:
    Oct 31, 2013
    Posts:
    9
    Hey, I'm trying to implement an in-game Lua script editor so players can write their own scripts. Right now, I just have it implemented as a vanilla multiline Input Field with the new UI...and it's complete trash. Pasting text ignores newlines, there's no undo, clicking on the end of a line sends the caret to the beginning of the previous line, pressing up when the caret is on the last character sends the caret to the beginning of the file, it doesn't play well with Scroll Rect, and (somewhat understandably) there's no easy support for things like syntax highlighting and line numbering.

    My entire project is built around the concept of players writing Lua scripts. Right now, I have a message telling them how to use an external text editor like Sublime/Notepad++ because the in-game Input Field is so bad. I've seen ScintillaNET, which looks pretty cool, but I have no idea how I would even start implementing it in Unity (or if it even supports it) and it looks like it would only work with Windows builds.

    If anyone has any tips for improving the functionality of the Input Field, they would be really appreciated. I'm also willing to spend a decent amount of money on an asset store alternative if such a thing exists.

    Thank you!
     
    JayDeveloper likes this.
  2. joshskelton

    joshskelton

    Joined:
    Aug 30, 2013
    Posts:
    22
    Did you ever find a solution? I'm working with input fields right now, but am running into the same problems.
     
  3. shoffing

    shoffing

    Joined:
    Oct 31, 2013
    Posts:
    9
    I did eventually find an extremely hacky way around a few of the problems...but your mileage may vary.

    I created my own ScriptEditor script, and (using the InputField source as a guide) directly inherited from InputField. I identified the sections of the InputField where the problems were occurring, and just overrided methods until things got fixed.

    Here's what I came up with: https://gist.github.com/shoffing/5d304ff858d25546e7e7

    The code is ugly at best, and downright shameful at worst. See line 102, for example. I needed to set the content of the script editor to load a ship's script, but there was a problem - doing scriptEditor.text = "some text" would throw some internal Unity error on the first attempt to set the text, then it would work fine on the second attempt. So I created my own SetText() method, that wraps the text setting code in a try/catch. When the exception occurs after the first attempt, another method is called that simply tries again. And this worked.

    Some fixes were very straight-forward, and could be easily fixed by Unity if they wanted to. For example, the issue where clicking on the end of a line would bring the cursor to the beginning of the next line...that was fixed by simply adding a "- 1" to the end of a line in GetLineEndPosition() (see InputField method vs ScriptEditor method).

    So, where do I go from here? There still exist many other annoyances with the InputField - newlines are removed after copy-paste, there's no undo functionality, and syntax highlighting is virtually impossible. I could continue to hack together my extension of the InputField, but I fear the sanity cost may just be too high. So I'm considering a more drastic approach sometime in the future. I might just take the entire InputField source and copy it to create my own InputField. Or I might try and figure out how Unity plugins work, and create my own InputField from scratch to suit my needs.

    Either way, it's not a high enough priority to bother with right now. I just make sure to include instructions on how to use an external text editor in the README, because Unity's InputField is currently a bit of a joke.