I’ve spent most of my weekend trying to sort out a system for parsing text from an Ink script to display paragraph-by-paragraph when the user presses a button. Easy enough in itself as the Unity integration allows you to call each individual line that makes up the body of the current ‘continue’ (i.e. all text before the start of the next choice list). However, it becomes more problematic if the text is longer than the text view area.
What I essentially wanted was the following:
- If the line of text is shorter than a defined length (a variable found through trial and error) display that
- If not then split it into smaller chunks…
- If the text has a defined punctuation (. ? or !) then split it there
- If not…
- If there’s a comma roughly halfway through replace the comma with ellipses and split there
- If there’s not, do the same but with a space
Again, doesn’t sound particularly problematic but had issues with trying to get it to work. Such as:
- For some reason I went through this whole process of taking the text Unity/Ink automatically supplies and split it up myself initially, which took a fair while figuring that out. It’s only when I started writing this I realised I could take the Unity/Ink currentText call and dump that into the string list instead.
- It took a while to figure out how to break up longer text based on punctuation and keep that punctuation in place (the Split function I was using was removing the character it was splitting on).
- Likewise, figuring out how to break long strings into shorter ones using ellipses took a while
- Had to figure out how to work around ‘unwanted’ punctuation. The one that cropped up in my example text (entirely coincidental it was in there in the first place) was an e.g. which it was reading as being a full stop. Similarly, don’t want it picking up duplicate or mixed punctuation (??, !! or !?!?). Turns out the easiest way is to temporarily replace it with some dummy text and replace it back again.
When I write it down like that it makes me realise how little I know as that seems simple yet took me a fair while!
There’s still more to do though:
- Get the flow working as per the user input. At the moment any non-broken line is printing when you hit enter, but anything broken up prints all the parts at once (mostly for me to test it is breaking up correctly)
- Currently doesn’t account for if a broken piece of text using ellipses is longer than two screens (for the purpose of testing I’ve just split it into two chunks, but in reality it could be more if its a very long piece of text with no sentences).
- Need to test with different text. I set up a test text script, but want to check it is working properly using some more test scripts.
- Allow flexibility of the line length value depending on language
- Need to allow for a dictionary of sorts for the temporary text replacements and have Unity run through that and replace as/when it finds them, rather than just a series of hand-written replace functions at the moment.
- Ideally also have some way of knowing if punctuation is strung together (!?!?!) without having to write each possible variation
I’m sure there’s probably an asset or something I could have downloaded to do all this, although having a quick look most of the things I could find seem to predate Unity’s new(ish) UI system. Although I didn’t look too hard. Plus they’re not designed for Ink (although undoubtedly I would have been able to middleware it myself).
However, it was educational (if at times painful) so I’m happy I stuck with it. Just need to finish it off, preferably before I go back home for Christmas.