- Home /
Loaclization in WebGL?
Hi,
I would like to implement multiple languages into my WebGL build. I already did this tutorial: https://youtu.be/5Kt9jbnqzKA to get used to the general idea. But sadly the "StreamingAssets" folder can't be accessed directly when building for WebGL.
Has anyone experience with this and could point me in the right direction on how to accomplish localization in WebGL?
Thanks in advance :)
Answer by Getsumi3 · Jan 30, 2020 at 07:06 PM
Hey @MSachs !
It's actually pretty easy to do it.
First. You can use your own localization but take a look at Lean Localization (it's free). https://assetstore.unity.com/packages/tools/localization/lean-localization-28504
It will save you some precious time, Easy to implement and almost no codding required.
Second. You can access almost any file/folder. For this you can use UnityWebRequest like it's said in the docs:
Use the UnityWebRequest class to access the Assets
Here is an example using UnityWebRequest
public IEnumerator GetFileCo(string _filename)
{
string fullPath = Path.Combine(Application.streamingAssetsPath, _filename);
var uwrLocal = UnityWebRequest.Get("file:///" + fullPath);
yield return uwrLocal.SendWebRequest();
if (uwrLocal.isHttpError || uwrLocal.isNetworkError)
{
Debug.Log("Error while getting the file. Code: " + uwrLocal.error);
}
else
{
Debug.Log("File content: " + uwrLocal.downloadHandler.text);
}
}
Third Let's say that you want to have your webgl language same as website language. For this you'll need to send message from webpage and then make an externall call from webgl to receive data.
Here is the trick. In your webgl index.html create a js function after unityInstance:
function OnUnityReady() {
unityInstance.SendMessage("WebRequestHandler", "SetLanguage", "en");
};
The name of the function is the name that will be called in unity.
The first param of SendMessage is the script class name in Unity. Let's say it's WebRequestHandler.cs
The second param is the name of your method that will be executed. Lets say it's void SetLanguage(string _langID).
And the third param is the method's param. In our case it is _langID (You'll need to make a js function that will get the current language of your page and return a string with language code).
Now lets look what will happen inside WebGL. First of all create a new scene that will act as a splashScreen/loadingScreen and a script that will be called WebRequestHandler.cs. This script will be the bridge between website and unity.
Here how script will look inside:
public string languageID;
private void Start()
{
//calling OnUnityReady from JS script
Application.ExternalCall("OnUnityReady");
LoadScene();
}
//this is the method that is using in index.html to set language
public void SetLanguage(string _langID)
{
languageID= _langID;
}
public void LoadScene()
{
if(languageID != "")
{
StartCoroutine(PrepareMainMenu());
}
else
{
languageID = defaultLanguageID;
StartCoroutine(PrepareMainMenu());
}
}
public Ienumerator PrepareMainMenu()
{
AsyncOperation loadAsync = SceneManager.LoadSceneAsync(yourSceneIndex);
while (!loadAsync.isDone)
{
progressText.text = "INITIALIZING: " + (loadAsync.progress * 100) + "%";
if (loadAsync.progress >= 0.9f)
{
loadAsync.allowSceneActivation = true;
}
yield return null;
}
}
}
And basicaly that's all. All that remains is to tell to your localizationManager that language = languageID from WebRequestHandler