Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


AddKey

Adds a key and returns its entry.
Read time 2 minutesLast updated 13 days ago

Definition

  • Type: Method
  • Namespace: Unity.Localization
  • Assembly: UnityEngine.LocalizationRuntimeModule

AddKey(string)

Adds a key and returns its entry.
public SharedTableData.SharedTableEntry AddKey(string key)

Parameters

The key to add.

Returns

Type

Description

SharedTableEntryThe new or existing entry, or
null
when the key is empty.

Remarks

When the key already exists, its existing entry is returned rather than a duplicate being created. A new id is drawn from the SharedTableData.KeyGenerator and retried until it is unique. Returns
null
when the key is empty or null.
Add a key and observe that adding it again returns the same entry.

Examples

using Unity.Localization;using UnityEngine;namespace Unity.Localization.Samples{ public class SharedTableDataAddKeyExample { public void Run() { var sharedData = ScriptableObject.CreateInstance<SharedTableData>(); SharedTableData.SharedTableEntry entry = sharedData.AddKey("GREETING"); SharedTableData.SharedTableEntry again = sharedData.AddKey("GREETING"); // same entry, no duplicate Debug.Log(entry.Id == again.Id); // True } }}

AddKey(string, long)

Adds a key with an explicit id and returns its entry.
public SharedTableData.SharedTableEntry AddKey(string key, long id)

Parameters

The key to add.

The stable id to assign.

Returns

Type

Description

SharedTableEntryThe new or existing entry, or
null
when the key or id cannot be used.

Remarks

Use this to preserve ids when rebuilding from serialized data, for example importing or loading from a file. Returns the existing entry when the key is already present, logging a warning if the requested id differs from the stored one. Returns
null
when the key is empty, the id is
0
, or the id is already used by another key.
Add a key with an id preserved from serialized data.

Examples

using Unity.Localization;using UnityEngine;namespace Unity.Localization.Samples{ public class SharedTableDataAddKeyWithIdExample { public void Run() { var sharedData = ScriptableObject.CreateInstance<SharedTableData>(); SharedTableData.SharedTableEntry entry = sharedData.AddKey("GREETING", 1001); Debug.Log(entry.Id); // 1001 } }}