DistributedUIDGenerator
The default key generator. It produces a distributed unique id, Snowflake-style, that combines a timestamp, a machine id, and a per-millisecond sequence.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Class
- Namespace: Unity.Localization
- Assembly: UnityEngine.LocalizationRuntimeModule
- Implements: IKeyGenerator, ISerializationCallbackReceiver
public class DistributedUIDGenerator : IKeyGenerator, ISerializationCallbackReceiver
Remarks
The generated ids are non-sequential, so several users can add keys to the same table without id collisions. The id packs a per-millisecond sequence (12 bits), a machine id (10 bits), and a timestamp measured from DistributedUIDGenerator.CustomEpoch (41 bits). Timestamps count from a fixed default epoch of 2020-01-01T00:00:00Z, and every generator in the process draws from one sequence counter, so two default generators created at the same moment still hand out different ids. The machine id is derived from SystemInfo.deviceUniqueIdentifier so it is stable per machine. Assign an instance to SharedTableData.KeyGenerator to control how a collection assigns key ids, or implement IKeyGenerator for a different scheme.
Additional Resources: IKeyGenerator, SharedTableData
Generate two ids and confirm they differ.
Examples
using Unity.Localization;using UnityEngine;namespace Unity.Localization.Samples{ public class DistributedUIDGeneratorOverviewExample { public void Run() { var generator = new DistributedUIDGenerator(); var first = generator.GetNextKey(); var second = generator.GetNextKey(); Debug.Log(first != second); // True: ids are unique } }}
Constructors
Constructor | Description |
|---|---|
| DistributedUIDGenerator() | Creates a generator that measures timestamps from the default epoch. |
| DistributedUIDGenerator(System.Int64) | Creates a generator that measures timestamps from a specific epoch. |
Properties
Property | Description |
|---|---|
| CustomEpoch | The epoch, in Unix milliseconds, that timestamps are measured from. |
| MachineId | The machine id folded into each id. |
Methods
Method | Description |
|---|---|
| GetNextKey | Returns the next distributed unique id. |