Class DistributedUIDGenerator
A Key generator that uses the current time in ms with the machine id to generate a unique value every time. This means it is safe for multiple users to add entries to the same table without suffering from conflicts due to the entries using the same key but having different values.
The implementation is based on this article https://www.callicoder.com/distributed-unique-id-sequence-number-generator/
The Key is made up of the following components:
Sequence Number | 12 Bits(0 - 11) | A local counter per machine that starts at 0 and is incremented by 1 for each new id request that is made during the same millisecond. The value is limited to 12 bytes so can contain 4095 items before the ids for this millisecond are exhausted and the id generator must wait until the next millisecond before it can continue. |
Machine Id | 10 Bits(12-21) | The Id of the machine. By default, in the Editor, this value is generated from the machines network interface physical address however it can also be set to a user provided value. There is enough space for 1024 machines. |
Epoch Timestamp. | 41 Bits(22-63) | A timestamp using a custom epoch which is the time the class was created. The maximum timestamp that can be represented is 69 years from the custom epoch, at this point the Key generator will have exhausted its possible unique Ids. |
Implements
Namespace: UnityEngine.Localization.Tables
Assembly: Unity.Localization.dll
Syntax
[Serializable]
public class DistributedUIDGenerator : IKeyGenerator
Constructors
Name | Description |
---|---|
DistributedUIDGenerator() | Create a default instance which uses the current time as the CustomEpoch the machines physical address as MachineId. |
DistributedUIDGenerator(long) | Creates an instance with a defined CustomEpoch. |
Fields
Name | Description |
---|---|
MachineIdPrefKey | The name of the EditorPrefs that is used to store the machine id. |
Properties
Name | Description |
---|---|
CustomEpoch | The custom epoch used to generate the timestamp. |
MachineId | The Id of the current machine. By default, in the Editor, this value is generated from the machines network interface physical address however it can also be set to a user provided value. There is enough space for 1024 unique machines. Set value will be clamped in the range 1-1023. The value is not serialized into the asset but stored into the EditorPrefs(Editor only). |
Methods
Name | Description |
---|---|
GetNextKey() | Returns the next Id using the current time, machine id and sequence number. |