Skip to content

Unique ID

To generate unique identifiers, it is usually used the System.Guid class, because it provides a fast and reliable mechanism to generate long enough IDs that the collision chance is almost zero.

However, this class is not serializable. That's why Game Creator comes with the UniqueID class, which serves two purposes:

  • Serializable: This means that any changes made to this ID will be kept between editor sessions.
  • Custom UI: When showing this ID in a Unity Window, it automatically displays a nice and handy box with buttons that allow to easily modify this ID or even regenerate it, in case that's necessary.

Initialization

To initialize a class instance of UniqueID is as easy as calling the constructor class. For example, let's say we want to add a unique ID to a MonoBehaviour class:

public class MyComponent : MonoBehaviour
{
    public UniqueID myID = new UniqueID();
}

This will automagically assign a unique ID to the myID field. If we drag and drop this component onto a scene game object, we'll see this field with its associated ID.

Accessing ID

Accessing the ID value can be performed getting the IdString struct, which contains a string based ID and its hash value. This last one is recommended when comparing to ids:

To get the hash value:

int hash = this.myID.Get.Hash;

To get the string value:

string id = this.myID.Get.String;

Best Practices

Accessing the string value of the UniqueID should only be done if you plan on serializing this value somewhere. For comparing two IDs, it is best if you simply compare their hash value, as the probablity that two strings have the same hash value its very, very very low. On the other hand, comparing two int values is extremely fast and performant.

Back to top