Search Unity

Get the GUID of a StringReference

Discussion in 'Localization Tools' started by xrdnk, Apr 3, 2022.

  1. xrdnk

    xrdnk

    Joined:
    Jan 31, 2018
    Posts:
    4
    I would like to be able to get the GUID of a StringReference if I know the name of the StringTable and the key name. Does anyone know how to do this in a script?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Is this for an editor script or player?
    Whats the use case here, what do you need the GUID for if you have the table name? Can you use the table name instead?
     
  3. xrdnk

    xrdnk

    Joined:
    Jan 31, 2018
    Posts:
    4
    Thanks for Reply!

    I am trying to create a ScriptableObject with LocalizedString. The sample code is as follows.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Localization;
    3.  
    4. namespace Sample
    5. {
    6.     [CreateAssetMenu (fileName = "FILENAME", menuName = "MENUNAME", order = 0)]
    7.     public class SampleLocalizedSO : ScriptableObject
    8.     {
    9.         [SerializeField] private LocalizedString m_localizedString;
    10.     }
    11. }
    12.  
    I have a lot of SampleLocalizedSO data, and it is troublesome to manage them, so I want to make the data itself easily editable on the CSV side, and convert the CSV file to a ScriptableObject file.

    Here is the problem.
    On the CSV side, the data is registered by the key name of StringTable, and
    When converting to a ScriptableObject, I want to be able to register it as a StringReference.
    StringReference seems to use the GUID of the StringTable and the KeyId of the StringTable, which is why I asked this question.
     
  4. xrdnk

    xrdnk

    Joined:
    Jan 31, 2018
    Posts:
    4
    I'm sorry ... I tried it and found the following constructor in LocalizedString.

    Code (CSharp):
    1.         /// <summary>
    2.         /// Initializes and returns an instance of a <see cref="LocalizedString"/>.
    3.         /// </summary>
    4.         /// <param name="tableReference">Reference to the String Table Collection.
    5.         /// This can either be the name of the collection as a <c>string</c> or the Collection Guid as a [System.Guid](https://docs.microsoft.com/en-us/dotnet/api/system.guid).</param>
    6.         /// <param name="entryReference">Reference to the String Table Collection entry.
    7.         /// This can either be the name of the Key as a <c>string</c> or the <c>long</c> Key Id.</param>
    8.         /// <example>
    9.         /// This example shows the different ways to construct a LocalizedString.
    10.         /// <code source="../../DocCodeSamples.Tests/LocalizedStringSamples.cs" region="localized-string-constructor"/>
    11.         /// </example>
    12.         /// <example>
    13.         /// This example shows how a LocalizedString could be set up in the Editor.
    14.         /// By using the Guid and Id the table and entry references will not be lost if the table collection name or entry name was to be changed.
    15.         /// Note: The performance benefits to using a Guid and Id are negligible.
    16.         /// <code source="../../DocCodeSamples.Tests/LocalizedStringSamples.cs" region="localized-string-constructor-editor"/>
    17.         /// </example>
    18.         public LocalizedString(TableReference tableReference, TableEntryReference entryReference)
    19.         {
    20.             TableReference = tableReference;
    21.             TableEntryReference = entryReference;
    22.         }
    Using this, the TableReference can be set to the Name of the StringTable, and the
    TableEntryReference is the Key Name, and I was able to apply the reference I wanted to the StringReference, so I can use this to convert the KeyName to StringReference.
     
    karl_jones likes this.
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Yes a TableReference can either be a GUID or a table name and a TableEntryReference can be a Key Id or Key name so it lets you use whatever you need with the same API.