Search Unity

Question Localize Scriptable Objects

Discussion in 'Localization Tools' started by joss51, Feb 5, 2023.

  1. joss51

    joss51

    Joined:
    May 6, 2016
    Posts:
    18
    Hello,

    I want to use localization system with Scriptable Objects but it doesn't work.
    As this tool is made to be simple, is there a simple way to do it ?
    All I have seen on the web is old and not simple at all.
    Thanks a lot !
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    The scripting guide has some examples.
    https://docs.unity3d.com/Packages/com.unity.localization@1.4/manual/Scripting.html#localizedasset

    Use LocalizedAsset.

    You could localize any scriptable object:

    Code (csharp):
    1. [Serializable]
    2. public class LocalizedScriptableObject : LocalizedAsset<ScriptableObject> { }
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     public LocalizedScriptableObject localizedScriptableObject;
    7.  
    8.     void OnEnable()
    9.     {
    10.         localizedScriptableObject.AssetChanged += OnAssetChanged;
    11.     }
    12.  
    13.     void OnDisable()
    14.     {
    15.         localizedScriptableObject.AssetChanged -= OnAssetChanged;
    16.     }
    17.  
    18.     void OnAssetChanged(ScriptableObject value)
    19.     {
    20.         Debug.Log(value.ToString());
    21.     }
    22. }
    or a specific type

    Code (csharp):
    1. [CreateAssetMenu]
    2. public class MyScriptableObject : ScriptableObject
    3. {
    4. }
    5.  
    6. [Serializable]
    7. public class LocalizedMyScriptableObject : LocalizedAsset<MyScriptableObject> { }
    8.  
    9. public class Example : MonoBehaviour
    10. {
    11.     public LocalizedMyScriptableObject localizedScriptableObject;
    12.  
    13.     void OnEnable()
    14.     {
    15.         localizedScriptableObject.AssetChanged += OnAssetChanged;
    16.     }
    17.  
    18.     void OnDisable()
    19.     {
    20.         localizedScriptableObject.AssetChanged -= OnAssetChanged;
    21.     }
    22.  
    23.     void OnAssetChanged(MyScriptableObject value)
    24.     {
    25.         Debug.Log(value.ToString());
    26.     }
    27. }
     
  3. joss51

    joss51

    Joined:
    May 6, 2016
    Posts:
    18
    Thank you. I can change the scriptable object variable of a Monobehaviour with the good local version.
    But how can I simply & dynamicly change the scriptable object variable of a MonoBehaviour with the localization system which select the good local version?
    For example, I want to use scriptable objects for an item inventory. Item list is a scriptable object list. It always changes. How can I localize all scriptable objects ?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
  5. joss51

    joss51

    Joined:
    May 6, 2016
    Posts:
    18
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Which solution?
    Try right clicking the TMP text and choosing the localize menu option.
     
  7. joss51

    joss51

    Joined:
    May 6, 2016
    Posts:
    18
    It works, thanks a lot.
    I was speaking about the solution proposed with the use of LocalizedString in the Scriptable Objects at the beginning of the thread.
    I also need to localize lists of strings in a Scriptable Object so I'm gonna try solutions in the thread too.
    Thanks Karl and sorry for these basic questions.
     
    karl_jones likes this.