Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Uses of Meta Data

Discussion in 'Localization Tools' started by Peanut8, May 18, 2022.

  1. Peanut8

    Peanut8

    Joined:
    May 17, 2018
    Posts:
    10
    Hello fellow unity people.

    I am currently trying to learn meta data. But I dont know if I understand every usecase.
    As far as I understand it. Meta data are like data containers. But I read in the doku that they can be used to run custom code.
    But how can I call it ?
    For example meta data for a tableentry.
    Do I need to find the tableentry , then the metadata and then run a method on the metadata.
    Are there any events I can subsribe to or something.
    Do you have any tipps how to use them properly.

    Thanks for helping me out.
    Have a nice day :)

    Doku I used : https://docs.unity3d.com/Packages/com.unity.localization@1.0/manual/Metadata.html
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    Metadata is just a C# script so it can be used as a data container but can also be used to run the methods on the script. You will need to get the instance of the metadata to run it.

    Code (csharp):
    1. public interface IRunCodeMetadata : IMetadata
    2. {
    3.     void DoSomething();
    4. }
    5.  
    6. [Metadata(AllowedTypes = MetadataType.StringTableEntry)]
    7. [Serializable]
    8. public class RunCodeA : IRunCodeMetadata
    9. {
    10.     public void DoSomething()
    11.     {
    12.         Debug.Log("Doing Something");
    13.     }
    14. }
    15.  
    16. public class MyBehaviour : MonoBehaviour
    17. {
    18.     private void Start()
    19.     {
    20.         LocalizationSettings.SelectedLocaleChanged += LocalizationSettings_SelectedLocaleChanged;
    21.     }
    22.  
    23.     private void LocalizationSettings_SelectedLocaleChanged(Locale obj)
    24.     {
    25.         var entryResult = LocalizationSettings.StringDatabase.GetTableEntry("My Table", "My Entry");
    26.         if (entryResult.Entry != null)
    27.         {
    28.             var code = entryResult.Entry.GetMetadata<IRunCodeMetadata>();
    29.             code?.DoSomething();
    30.         }
    31.     }
    32. }
    Why would you want to do this? I don't know, you just can ;)
    Maybe you want some custom code to be executed based on the language the user selects, so you put metadata on the Locale to do this.

    There are more examples in the scripting docs
    https://docs.unity3d.com/Packages/c...tyEngine.Localization.Metadata.IMetadata.html
    https://docs.unity3d.com/Packages/c....3/api/UnityEngine.Localization.Metadata.html

    One use where we run custom code for metadata is the IEntryOverride. We use it to redirect from one table entry to another, this lets us implement the PlatformOverride metadata.
    We also use Metadata to indicate that an entry uses Smart formatting although will likely change this in the future for performance reasons.
     
  3. Peanut8

    Peanut8

    Joined:
    May 17, 2018
    Posts:
    10
    Ahh I understand. Thank you . You legend ;D
     
    karl_jones likes this.