获取唯一ID

版权声明:未经允许不可转载 https://blog.csdn.net/Edision_li/article/details/89403780

将用过的ID存到list里面。 

 private int tid;
 private static readonly string obj = "lock";
 private List<int> m_TidList = new List<int>();

 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
        {
            tid = GetTid();
            m_TidList.Add(tid);
        }
        
 }
  /// <summary>
    /// 获取唯一ID
    /// </summary>
    /// <returns></returns>
    private int GetTid()
    {
        lock (obj)
        {
            tid++;

            //安全校验
            while (true)
            {
                if (tid == int.MaxValue)
                {
                    tid = 0;
                }
                bool used = false;
                for (int i = 0; i < m_TidList.Count; i++)
                {
                    if (tid == m_TidList[i])
                    {
                        used = true;
                        break;
                    }
                }
                if (!used)
                {
                    break;
                }
                else
                {
                    tid++;
                }
            }
        }
        return tid;
    }

猜你喜欢

转载自blog.csdn.net/Edision_li/article/details/89403780