using System; using System.Collections.Generic; using System.Collections.ObjectModel; #if UNITY_EDITOR using System.Security.Cryptography; using System.Text; #endif using UnityEngine; //Profile数据管理,一般是一些String数据,通过ClientPrefs进行本地保存 public class ProfileManager { public const string AuthProfileCommandLineArg = "-AuthProfile"; string m_Profile; public string Profile { get { return m_Profile ??= GetProfile(); } set { m_Profile = value; onProfileChanged?.Invoke(); } } public event Action onProfileChanged; List m_AvailableProfiles; public ReadOnlyCollection AvailableProfiles { get { if(m_AvailableProfiles == null) { LoadProfiles(); } return m_AvailableProfiles.AsReadOnly(); } } public void CreateProfile(string profile) { m_AvailableProfiles.Add(profile); SaveProfiles(); } public void DeleteProfile(string profile) { m_AvailableProfiles.Remove(profile); SaveProfiles(); } static string GetProfile() { var arguments = Environment.GetCommandLineArgs(); for(int i = 0;i < arguments.Length;i++) { if(arguments[i] == AuthProfileCommandLineArg) { var profileId = arguments[i + 1]; return profileId; } } #if UNITY_EDITOR var hashedBytes = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(Application.dataPath)); Array.Resize(ref hashedBytes, 16); return new Guid(hashedBytes).ToString("N"); #else return ""; #endif } void LoadProfiles() { m_AvailableProfiles = new List(); var loadedProfiles = ClientPrefs.GetAvailableProfiles(); foreach(var profile in loadedProfiles.Split(',')) { if(profile.Length > 0) { m_AvailableProfiles.Add(profile); } } } void SaveProfiles() { var profilesToSave = ""; foreach(var profile in m_AvailableProfiles) { profilesToSave += profile + ","; } ClientPrefs.SetAvailableProfiles(profilesToSave); } }