Accessing localized string in editor OnValidate

Hello,

I’m trying to get localized string in OnValidate when the values change in editor but end up with “SendMessage cannot be called during OnValidate” warning.

public AgentData : ScriptableObject
{
    public LocalizedString Name;
}

public class Agent
{
    public string _name;
    public AgentData _presetData;

    public void OnValidate()
    {
        if(_presetData != null)
        {
            _presetData.Name.GetLocalizedString().Completed += op => _name = op.Result;
        }
    }
}

public AgencyData : ScriptableObject
{
    public List<Agent> _agents;

    void OnValidate()
    {
        foreach(Agent agent in _agents)
        {
            _agents.OnValidate();
        }
    }
}

The idea here is to have customizable agent names for realtime new agents and also some predefined localized names for preset agents. Setting up preset data in editor should be automatically setting the name for this agent, but apparently it is not possible to get localized string in editor?

That error is coming from a UnityEvent, not localization.
We do have editor support. You may need to call directly into StringDatabase to avoid triggering the event.

Yes, I see now this warning message was only a distraction. The real problem was “SelectedLocale is null” after inspecting the GetLocalizedString operation.
Now I understand that Localization is not initialized in editor mode by default and wrote this script for it to work:

[InitializeOnLoad]
public class Startup
{
    static Startup()
    {       
        LocalizationSettings.InitializationOperation.Completed += op =>
        {
            var locale = LocalizationSettings.AvailableLocales.Locales.FirstOrDefault();
            LocalizationSettings.Instance.SetSelectedLocale(locale);
        }; 
    }
}

Calling InitializationOperation in Edit mode should complete immediately however calling it from OnValidate may cause issues as there are some restrictions on what can be done inside of OnValidate as you saw earlier.

1 Like