Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

SharedTableData.AddKey

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public SharedTableEntry AddKey(string key);

Parameters

Parameter Description
key The key to add.

Returns

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

Description

Adds a key and returns its entry.

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.

Additional resources: SharedTableData.AddKey, SharedTableData.RemoveKey, SharedTableData.RenameKey

<para>Add a key and observe that adding it again returns the same entry.</para>

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 } } }

Declaration

public SharedTableEntry AddKey(string key, long id);

Parameters

Parameter Description
key The key to add.
id The stable id to assign.

Returns

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

Description

Adds a key with an explicit id and returns its entry.

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.

Additional resources: SharedTableData.AddKey, SharedTableData.GetEntry

<para>Add a key with an id preserved from serialized data.</para>

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 } } }