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 |
|---|---|
| SharedTableEntry | The new or existing entry, or |
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 when the key is empty or null.
nullAdd 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
Returns
Type | Description |
|---|---|
| SharedTableEntry | The new or existing entry, or |
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 when the key is empty, the id is , or the id is already used by another key.
null0Additional Resources: SharedTableData.AddKey, SharedTableData.GetEntry
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 } }}