docs.unity3d.com
    Show / Hide Table of Contents

    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 Number12 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 Id10 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.

    Signed Bit 1 Bit(64) The signed bit is unused by the ID generator. If you wish to add custom Id values then using the signed bit and adding negative ids will avoid conflicts.
    Inheritance
    Object
    DistributedUIDGenerator
    Namespace: UnityEngine.Localization.Tables
    Syntax
    [Serializable]
    public class DistributedUIDGenerator : IKeyGenerator

    Constructors

    DistributedUIDGenerator()

    Create a default instance which uses the current time as the CustomEpoch the machines physical address as MachineId.

    Declaration
    public DistributedUIDGenerator()

    DistributedUIDGenerator(Int64)

    Creates an instance with a defined CustomEpoch.

    Declaration
    public DistributedUIDGenerator(long customEpoch)
    Parameters
    Type Name Description
    Int64 customEpoch

    The custom epoch is used to calculate the timestamp by taking the current time and subtracting the CustomEpoch. The value is then stored in 41 bits giving it a maximum time of 69 years.

    Fields

    MachineIdPrefKey

    The name of the EditorPrefs that is used to store the machine id.

    Declaration
    public const string MachineIdPrefKey = "KeyGenerator-MachineId"
    Field Value
    Type Description
    String

    Properties

    CustomEpoch

    The custom epoch used to generate the timestamp.

    Declaration
    public long CustomEpoch { get; }
    Property Value
    Type Description
    Int64

    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).

    Declaration
    public int MachineId { get; set; }
    Property Value
    Type Description
    Int32

    Methods

    GetNextKey()

    Returns the next Id using the current time, machine id and sequence number.

    Declaration
    public long GetNextKey()
    Returns
    Type Description
    Int64
    Implements
    IKeyGenerator.GetNextKey()

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Constructors
      • DistributedUIDGenerator()
      • DistributedUIDGenerator(Int64)
    • Fields
      • MachineIdPrefKey
    • Properties
      • CustomEpoch
      • MachineId
    • Methods
      • GetNextKey()
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023