123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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<string> m_AvailableProfiles;
- public ReadOnlyCollection<string> 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<string>();
- 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);
- }
- }
|