Loading Text strings from XML/JSON etc in Unity - best practice?

Hey, I’m working on a game that displays modal windows now and then to the player, with various warning texts. The basic XML structure is here (Github Gist).

So for example I’d like to be able to instantiate a new modal panel with the header and body text from the Category:Default Item, or from a random Category:Advisory Item.

I’m targeting mobile, so I’m not sure if the better option would be to lazy load them from XML as needed, or initialise Collections on start in a Singleton GameManager, or something else. Resources? TextAssets? Not sure, any advice appreciated.

Out of curiosity, have you seen the modal panel tutorial on the Unity site? It sets it up as a method that you can pass all the fields into (Title, message, 3 buttons that adjust based on how many you need, etc) and you can make any button run any public void method in the script.

I hadn’t, but thanks as that code takes a slightly different approach to how I’m doing it, and has a nice alpha blend so it might be useful. The question is more about sourcing the text that goes into the modals, in a performance efficient way - though I appreciate the tipoff.

I’ll admit that I’m an idiot when it comes to deserializing json or xml. I did have a script that dynamically created a card deck based on a TSV file, but that method probably wouldn’t work well here. Only thing I could think of would be to make lots of static variables that you set to call at the proper time, but that would probably be quite dumb.

Looks like I’ll go with a combination of this Asset to handle the i18n https://www.assetstore.unity3d.com/en/#!/content/7543 along with a small engine of my own design to handle the randomising and duplicate prevention. At least Smart Localization takes a lot of the complexity away.

Yep, Smart Localization works great. i18n with three different languages in a couple of hours:

Hi @ninjachimp it seems you’ve had a similar problem to me, and found the solution. My issue is a little different, but I’m hoping you can help anyway.
I did the Text101 adventure unit, and then just kind of got carried away with a text adventure game I’ve been wanting to make for awhile. Someone recently pointed out that hard-coding the text limits my ability to move things around easily so I should consider using JSON to handle the actual text and just handle the progression through C# code. I’ve been searching, but can’t still figure out how to do that. I managed to get Unity to read the file (after ages!) but it reads the entire file at once.
Any idea how I could solve this problem? Solution doesn’t have to be using JSON, it just needs to be elegant.

I’m still struggling with the performance aspect and have been really happy with the Smart Localization plugin. I’ll admit I still don’t fully understand what it’s doing under the hood, but it seems to be working efficiently so I’m happy with that :slight_smile:

Privacy & Terms