雪花算法(Snowflake)是一种生成唯一ID的算法。在游戏开发过程中会为玩家生成唯一的id,或者玩家获得一件装备,为这件装备生成唯一的Id,将此角色和装备的Id保存于数据库当中。

这里以ET框架中的 ActorId 举例子,id 总长度为 64位,其中总共分为了四个部分,分别占用:
以上的各个占位数可以根据项目中的需求自行调整,并非说只能这么表示。
Debug.Log(12 | 5); //输出 13

可以看出两个二进制没一位之间进行“|”运算 只要有一个为 1,那么结果就为 “1”
Debug.Log(5 << 2); //返回值3
“<<”运算符执行左移位运算。在移位运算过程中,符号位始终保持不变。如果右侧空出位置,则自动填充为 0;超出 32 位的值,则自动丢弃。

可以看出5右移2位,则在 5的二进制 右边补充两个00,左边则去掉2位
那么左移就正好相反 那 1000 >> 8 举例,在左边补充8个0,右边去掉8位
Debug.Log(1000 >> 8); //返回值3

介绍完上面需要使用的运算符,我们就可以很简单的实现代码:
- public struct UnitIdStruct
- {
- public uint Time; // 30bit 34年
- public ushort Zone; // 10bit 1024个区
- public byte Process; // 8bit Process % 256 一个区最多256个进程
- public ushort Value; // 16bit 每秒每个进程最大16K个Unit
-
- public long ToLong()
- {
- ulong result = 0;
- result |= this.Value;
- result |= (ulong)this.Zone << 16;
- result |= (ulong)this.Process << (16 + 8);
- result |= (ulong)this.Time << (16 + 8 + 10);
- return (long)result;
- }
-
- public UnitIdStruct(int zone, int process, uint time, ushort value)
- {
- this.Zone = (ushort)zone;
- this.Process = (byte)(process % 256);
- this.Time = time;
- this.Value = value;
- }
-
- public UnitIdStruct(long id)
- {
- ulong result = (ulong)id;
- this.Value = (ushort)(result & ushort.MaxValue);
- result >>= 16;
- this.Process = (byte)(result & byte.MaxValue);
- result >>= 8;
- this.Zone = (ushort) (result & 0x03ff);
- result >>= 10;
- this.Time = (uint)result;
- }
- }