using System; using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; public partial class UIdSystem : MonoBehaviour { private static class GUID { private static ulong TimestampMask = 0xffffffff00000000; //时间戳掩码 private static ulong BigMask = 0x00000000f0000000; //大区号掩码 private static ulong SmallMask = 0x000000000ff00000; //小区号掩码 private static ulong BaseMask = 0x00000000000fffff; //本地 ID 编号掩码 private static int BaseId = 0; //本地 ID private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// /// 根据大区号和小区号生成 ID。 /// public static ulong NewGuid(uint big = 0, uint small = 0) { ulong timeStamp = (ulong)(DateTime.UtcNow - Jan1st1970).TotalSeconds; ulong newId = ((timeStamp << 32) & TimestampMask) | ((big << 28) & BigMask) | ((small << 20) & SmallMask) | ((uint)BaseId & BaseMask); Interlocked.Increment(ref BaseId); return newId; } } }