In my application, there is a need for all of my strings to be multilingual. My first plan was to import key-value pairs as text assets and parse these per individual scene. Being rather new to Unity, however, I couldn't figure out how to access this data from other classes. I was told that this could be easily achieved using static member variables and functions. I figured that, considering I am not very familiar with Unity yet, as opposed to the background I came from where the contents of static variables are lost across scripts, things might be handled differently here.
So as to not make my script too complicated on the get-go, I have currently come to (and have removed most of the irrelevant code):
using UnityEngine;
using System.Collections.Generic;
public class LanguageManager : MonoBehaviour
{
public static string SelectedLanguage { get; set; }
public static Dictionary> Strings { get; set; }
void Start()
{
LanguageManager.SelectedLanguage = "en";
LanguageManager.Strings = new Dictionary>();
LanguageManager.Strings.Add("en", new Dictionary() {
{ "test_string_01", "This is a test sentence." },
{ "test_string_02", "This is another test sentence." },
{ "test_string_03", "Formatting? {0}!" }
});
LanguageManager.Strings.Add("nl", new Dictionary() {
{ "test_string_01", "Dit is een testzin." },
{ "test_string_02", "Dit is een andere testzin." },
{ "test_string_03", "Formatteren? {0}!" }
});
}
}
In this script, the values are fine and everything works as I want it to. In any other script, referring to any member variable returns null. I know there are alternatives to my issue, but I'm wondering if static variables being lost across scripts still holds true here as expected, or if I'm just doing something wrong.
I apologise if it still turns out I didn't look hard enough or am missing something incredibly obvious.
Thanks in advance!
↧