Pass LocalizedString as argument in Unity Event

Hello,

I would like to change the LocalizedString used in the LocalizeStringEvent during run time. And I would like to do this via Unity Events in the inspector.

The setup is the following:

  1. A text mesh pro component.
  2. A Localize String Event component
  3. A custom component for changing the StringReference of the LocalizeStringEvent component with a function:
    code
public class LocalizationExtensions : MonoBehaviour
{
    [Required]
    [SerializeField] private LocalizeStringEvent _localizeStringEvent;
       
    public void SetStringReference(LocalizedString localizedString)
    {
        _localizeStringEvent.SetStringReference(localizedString);
    }
}

SetStringReference is an extension method, which also refreshes the string.

The issue is that I cannot call the SetStringReference method via Unity Events in the inspector because LocalizedString is not a Unity object. It is a shame that 2 Unity systems do not work together nicely. If I didn’t want to create a script purely for this case then what are my options?

Thanks!

You don’t need to change the whole LocalizedString instance, you can just change its references and it will update itself.

E.G

public void ChangeEntry(string entryName)
    {
        _localizeStringEvent.StringReference.TableEntryReference = entryName;
    }

You can now call this with a UnityEvent.

We could add some public methods to LocalizeStringEvent to better support this

Thanks!

Yes, this certainly works. However it is error prone and tedious to use. Would be much simpler to choose a StringReference from a list rather than having to look up the exact key value.

You could do this but it wont work in a UnityEvent as they only support primitives and Object references…
You could make a version of LocalizedString that would work as an object reference.

    [CreateAssetMenu]
    public class LocalizedStringAsset : ScriptableObject
    {
        public LocalizedString localizedString;
    }
    public void SetString(LocalizedStringAsset asset)
    {
        localizedStringEvent.StringReference = asset.localizedString;
    }

You can now create an asset for each string you want to reference.