public static void SaveAccountMainData(CTestAccountMainData mainData)
    {
        if (mainData == null)
        {
            return;
        }

        string dataStr = "";

        if (mainData.accountDataList != null)
        {
            for (int i = 0; i < mainData.accountDataList.Count; i++)
            {
                dataStr += JsonUtility.ToJson(mainData.accountDataList[i]);
                if (i != mainData.accountDataList.Count - 1)
                {
                    dataStr += "|";
                }
            }
        }

        CTestAccountSaveData saveData = new CTestAccountSaveData(mainData.accountName, dataStr);

        PlayerPrefs.SetString(CTestAccountMainKey, JsonUtility.ToJson(saveData));
    }
    public static CTestAccountMainData LoadAccountMainData()
    {
        string str = PlayerPrefs.GetString(CTestAccountMainKey);

        if (string.IsNullOrEmpty(str))
        {
            return(new CTestAccountMainData("CTest"));
        }

        CTestAccountSaveData saveData = JsonUtility.FromJson <CTestAccountSaveData>(str);

        CTestAccountMainData mainData = new CTestAccountMainData(saveData.accountName);

        string[] dataStrList = saveData.accountDataStr.Split(new char[] { '|' });

        for (int i = 0; i < dataStrList.Length; i++)
        {
            CTestAccountData data = JsonUtility.FromJson <CTestAccountData>(dataStrList[i]);
            if (data != null)
            {
                mainData.accountDataList.Add(data);
            }
        }

        return(mainData);
    }
    private void OnGUI()
    {
        CTestAccountMainData mainData = CTestAccountManager.LoadAccountMainData();

        bool   hasLoad = false;
        string oldName = mainData.accountName;

        GUILayout.BeginHorizontal();
        GUILayout.TextArea("用户名");
        mainData.accountName = GUILayout.TextField(mainData.accountName);
        GUILayout.EndHorizontal();

        GUILayout.Space(2);

        GUILayout.BeginHorizontal();
        GUILayout.TextArea("账号");
        GUILayout.TextArea("注册时间");
        GUILayout.TextArea("最后使用时间");
        GUILayout.TextArea("");
        GUILayout.EndHorizontal();

        scrollVec2 = GUILayout.BeginScrollView(scrollVec2);
        GUILayout.BeginVertical(GUILayout.Width(position.width - 20));

        if (mainData.accountDataList != null)
        {
            mainData.accountDataList.Sort((a, b) => - a.lastLoginTime.CompareTo(b.lastLoginTime));

            for (int i = 0; i < mainData.accountDataList.Count; i++)
            {
                GUILayout.BeginHorizontal();
                GUILayout.TextArea(CTestAccountManager.GetAccountStrByData(mainData.accountName, mainData.accountDataList[i]));
                GUILayout.TextArea(CTestAccountManager.GetSystemTimeFromStamp(mainData.accountDataList[i].registTime).ToString());
                GUILayout.TextArea(CTestAccountManager.GetSystemTimeFromStamp(mainData.accountDataList[i].lastLoginTime).ToString());
                if (GUILayout.Button("使用账号"))
                {
                    mainData.accountDataList[i].lastLoginTime = CTestAccountManager.GetCurrentTimeStamp();
                    hasLoad = true;
                    CTestAccountManager.CopyAccount(mainData.accountName, mainData.accountDataList[i]);
                }

                GUILayout.EndHorizontal();
                GUILayout.Space(1);
            }
        }

        GUILayout.EndVertical();
        GUILayout.EndScrollView();

        if (hasLoad || oldName != mainData.accountName)
        {
            CTestAccountManager.SaveAccountMainData(mainData);
            if (hasLoad)
            {
                Close();
            }
        }
    }
    /// <summary>
    /// 清理废弃账号
    /// </summary>
    /// <param name="mainData"></param>
    public static void ClearOldAccount(CTestAccountMainData mainData)
    {
        if (mainData != null && mainData.accountDataList != null && mainData.accountDataList.Count != 0)
        {
            int currentStamp = GetCurrentTimeStamp();
            int checkTime    = 86400 * 14;

            mainData.accountDataList = mainData.accountDataList.FindAll(x => currentStamp - x.lastLoginTime < checkTime);
        }
    }
    /// <summary>
    /// 获取并复制最后使用账号
    /// </summary>
    public static void CopyLastLoginAccount()
    {
        CTestAccountMainData accountMainData = LoadAccountMainData();

        CTestAccountData accountData = GetLastLoginAccount(accountMainData);

        if (accountData != null)
        {
            accountData.lastLoginTime = GetCurrentTimeStamp();
            SaveAccountMainData(accountMainData);
        }

        CopyAccount(accountMainData.accountName, accountData);
    }
    /// <summary>
    /// 注册新账号
    /// </summary>
    public static void RegistNewAccount()
    {
        CTestAccountMainData accountmainData = LoadAccountMainData();

        ClearOldAccount(accountmainData);

        int currentStampTime = GetCurrentTimeStamp();

        CTestAccountData accountData = new CTestAccountData(GetNewAccountIndex(accountmainData), currentStampTime, currentStampTime);

        accountmainData.accountDataList.Add(accountData);

        SaveAccountMainData(accountmainData);

        CopyAccount(accountmainData.accountName, accountData);
    }
    /// <summary>
    /// 获取新账号index
    /// </summary>
    /// <param name="mainData"></param>
    /// <returns></returns>
    private static int GetNewAccountIndex(CTestAccountMainData mainData)
    {
        CTestAccountData lastAccount = GetLastLoginAccount(mainData);

        if (lastAccount == null)
        {
            return(1);
        }

        DateTime lastDateTime = GetSystemTimeFromStamp(lastAccount.registTime);

        if (lastDateTime.Year == DateTime.UtcNow.Year && lastDateTime.Month == DateTime.UtcNow.Month && lastDateTime.Day == DateTime.UtcNow.Day)
        {
            return(lastAccount.index + 1);
        }
        else
        {
            return(1);
        }
    }
    /// <summary>
    /// 获取最后使用账号
    /// </summary>
    /// <returns></returns>
    private static CTestAccountData GetLastLoginAccount(CTestAccountMainData accountMainData)
    {
        if (accountMainData == null)
        {
            return(null);
        }

        if (accountMainData.accountDataList == null || accountMainData.accountDataList.Count == 0)
        {
            return(null);
        }

        CTestAccountData accountData = accountMainData.accountDataList[0];

        accountMainData.accountDataList.ForEach(x =>
        {
            //不能改成大于 防止同一秒点击多次
            if (x.lastLoginTime >= accountData.lastLoginTime)
            {
                accountData = x;
            }
        });
        return(accountData);
    }