You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
36 lines
1.3 KiB
4 years ago
|
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);
|
||
|
|
||
|
/// <summary>
|
||
|
/// 根据大区号和小区号生成 ID。
|
||
|
/// </summary>
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|