Hello! I have a problem with LocalizationStringEvent. When I change one scene to another this component stops see any changings in global variables group(sometimes I see message that some of localizations don’t exist). I made a special static class that loads and provides this group(Loading of variables group is done by Addressables) and I use it in my scripts to change global variables. In this time every element that was created from code is working properly. But when I startup second scene in editor, everything works fine.
Is it bug or I am doing something wrong?
Thank you
Hey. Thanks for the project it helped a lot.
Theres 2 problems I spotted.
You marked the global variables group as addressable but it’s also included in the Localization settings.
Localization settings are not addressable, it’s included in unity during the build so you will end up with 2 versions of the global variables group, 1 in the addressable asset bundle and 1 built into the player. This will likely cause issues when you change 1 but the other one is being listened to etc. This is not the reason for the above issue but it will be the reason it has issues in the player. If you want to keep it addressable then I would assign it to the source after it has loaded so that you have the same version.
Your script GlobalValueChanger is adding a new IntVariable each time. So it looks like when the 2nd scene starts, the LocalizedText uses the old variable, you then add the new variable and they ignore it as they are already using the old version. If you disable and enable them you will see that they start working again because they refreshed and found the new variable. The simplest thing to do here is to not add a new variable if one already exists.
Here is my changed version:
using UnityEngine;
using UnityEngine.Localization.SmartFormat.PersistentVariables;
public class GlobalValueChanger : MonoBehaviour
{
private IntVariable globalCounter;
private float timer = 0f;
private void Start()
{
globalCounter = LocalizationHelper.GetGlobalVariable<IntVariable>("counter");
}
private void Update()
{
timer += Time.deltaTime;
if (timer > 1f)
{
timer = 0f;
globalCounter.Value = Random.Range(0, 100000);
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.SmartFormat.Extensions;
using UnityEngine.Localization.SmartFormat.PersistentVariables;
using UnityEngine.ResourceManagement.AsyncOperations;
public class LocalizationHelper
{
private const string GlobalVariablesGroupAssetName = "global";
public static VariablesGroupAsset GlobalVariables
{
get
{
if (!wasInitialized)
{
Initialize();
}
return _globalVariablesGroup;
}
}
private static VariablesGroupAsset _globalVariablesGroup;
private static bool wasInitialized = false;
public static void Initialize()
{
if (wasInitialized)
return;
wasInitialized = true;
var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
_globalVariablesGroup = source[GlobalVariablesGroupAssetName];
}
public static string GetLocalizedString(string key)
{
return LocalizationSettings.StringDatabase.GetLocalizedString(key);
}
// Метод получения строки с данными локальными переменными. Использовать только один раз для каждой строки,
// так как медленно выполняется
public static string GetLocalizedString(string key, Dictionary<string, IVariable> variables)
{
var locale = new LocalizedString(LocalizationSettings.StringDatabase.DefaultTable, key);
foreach (var kp in variables)
locale.Add(kp);
return locale.GetLocalizedString();
}
public static LocalizedString MakeStringClone(LocalizedString localizedString)
{
return new LocalizedString(localizedString.TableReference, localizedString.TableEntryReference);
}
public static T GetGlobalVariable<T>(string name) where T : class, IVariable, new()
{
if (GlobalVariables.TryGetValue(name, out var value))
{
return value as T;
}
var v = new T();
GlobalVariables.Add(name, v);
return v;
}
// Метод добавляет новую глобальную переменную с проверкой на существование таковой.
public static void SetGlobalVariable(string name, IVariable value)
{
if (GlobalVariables.ContainsKey(name))
GlobalVariables.Remove(name);
GlobalVariables.Add(name, value);
}
}