85 lines
2.1 KiB
C#
85 lines
2.1 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using System;
|
|||
|
|
|||
|
namespace ZXL.ID
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 生成全局唯一ID(设备的唯一识别符+通过系统时间+int的最大值和最小值随机取值)
|
|||
|
/// </summary>
|
|||
|
public static class GenerateGlobalID
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// ID存储
|
|||
|
/// </summary>
|
|||
|
private static List<string> list_ID = new List<string>();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 当前最新的ID
|
|||
|
/// </summary>
|
|||
|
private static string id;
|
|||
|
|
|||
|
private static List<int> list_Int_ID = new List<int>();
|
|||
|
private static int int_Id;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 生成ID逻辑(使用递归生成全局唯一ID)
|
|||
|
/// </summary>
|
|||
|
private static void Generate()
|
|||
|
{
|
|||
|
id = SystemInfo.deviceUniqueIdentifier + DateTime.Now + UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
|||
|
while (list_ID.Contains(id))
|
|||
|
{
|
|||
|
Generate();
|
|||
|
}
|
|||
|
|
|||
|
list_ID.Add(id);
|
|||
|
}
|
|||
|
|
|||
|
private static void GenerateInt()
|
|||
|
{
|
|||
|
int_Id = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
|||
|
|
|||
|
while (list_Int_ID.Contains(int_Id))
|
|||
|
{
|
|||
|
GenerateInt();
|
|||
|
}
|
|||
|
|
|||
|
list_Int_ID.Add(int_Id);
|
|||
|
}
|
|||
|
|
|||
|
public static int GenerateIntID()
|
|||
|
{
|
|||
|
int id = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
|||
|
while (list_Int_ID.Contains(id))
|
|||
|
{
|
|||
|
Generate();
|
|||
|
}
|
|||
|
|
|||
|
return id;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 生成ID
|
|||
|
/// </summary>
|
|||
|
/// <returns>返回生成好的ID</returns>
|
|||
|
public static string GenerateID()
|
|||
|
{
|
|||
|
Generate();
|
|||
|
return id;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 移除ID(假回收ID重复使用)
|
|||
|
/// </summary>
|
|||
|
/// <param name="id">需要移除的ID</param>
|
|||
|
public static void RemoveID(string id)
|
|||
|
{
|
|||
|
if (list_ID.Contains(id))
|
|||
|
{
|
|||
|
list_ID.Remove(id);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|