当前位置 : 主页 > 网页制作 > xml >

Util_RecordUtil

来源:互联网 收集:自由互联 发布时间:2021-06-13
Unity中需要记录一些数据,Unity Api PlayerPrefs不好查看,我一般是在编辑中写成xml当发布后在用PlayerPrefs 代码如下 public class RecordUtil{ private static Dictionary string , string keyValuePairs; private stati

Unity中需要记录一些数据,Unity Api PlayerPrefs不好查看,我一般是在编辑中写成xml当发布后在用PlayerPrefs

代码如下

public class RecordUtil
{
    private static Dictionary<string, string> keyValuePairs;
    private static object locker;
    private static string recordPath;
    private static string RecordPath
    {
        get
        {
            if (string.IsNullOrEmpty(recordPath))
                recordPath = Path.Combine(RecordDirectory, "Record.xml");
            return recordPath;
        }
    }
    private static string recordDirectory;
    private static string RecordDirectory
    {
        get
        {
            if (string.IsNullOrEmpty(recordDirectory))
                recordDirectory = Path.Combine(Application.persistentDataPath, "Rescord");
            return recordDirectory;
        }
    }
    public static bool ForceWrite = false;
    public static void Init()
    {
        keyValuePairs = new Dictionary<string, string>();
        recordPath = Path.Combine(RecordDirectory, "Record.xml");
        locker = new object();
    }
    public static void SetValue(string key, string value)
    {
        if (PlatformUtil.IsEditor || ForceWrite)
            keyValuePairs[key] = value;
        else
            PlayerPrefs.SetString(key, value);
    }
    public static string GetValue(string key)
    {
        if (PlatformUtil.IsEditor || ForceWrite)
        {
            lock (locker)
            {
                if (keyValuePairs.ContainsKey(key))
                {
                    return keyValuePairs[key];
                }
                try
                {
                    XDocument recordXml = XDocument.Load(RecordPath);
                    XElement root = recordXml.Root;
                    var temp = root.Elements(key).ToList();
                    if (null != temp && temp.Count > 0)
                    {
                        keyValuePairs[key] = temp[0].Value;
                        return temp[0].Value;
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
                return "";
            }
        }
        else
            return PlayerPrefs.GetString(key);
    }
    public static void Save()
    {
        lock (locker)
        {
            if (PlatformUtil.IsEditor || ForceWrite)
            {
                XDocument recordXml = null;
                XElement root = null;
                if (!Directory.Exists(RecordDirectory))
                    Directory.CreateDirectory(RecordDirectory);
                if (!File.Exists(RecordPath))
                {
                    recordXml = new XDocument();
                    root = new XElement("Record");
                    recordXml.Add(root);
                }
                else
                {
                    recordXml = XDocument.Load(RecordPath);
                    root = recordXml.Root;
                }
                if (null != recordXml && null != root)
                {
                    foreach (var keyValue in keyValuePairs)
                    {
                        var temp = root.Elements(keyValue.Key).ToList();
                        if (null != temp && temp.Count > 0)
                        {
                            temp[0].Value = keyValue.Value;
                        }
                        else
                        {
                            root.Add(new XElement(keyValue.Key, keyValue.Value));
                        }
                    }
                    recordXml.Save(RecordPath);
                    keyValuePairs.Clear();
                }

            }
            else
                PlayerPrefs.Save();
        }
    }
    public static void DeleteAll()
    {
        lock (locker)
        {
            if (PlatformUtil.IsEditor || ForceWrite)
            {
                keyValuePairs.Clear();
                if (File.Exists(RecordPath))
                    File.Delete(RecordPath);
            }
            else
                PlayerPrefs.DeleteAll();
        }
    }
    public static void DeleteKey(string key)
    {
        lock (locker)
        {
            if (PlatformUtil.IsEditor || ForceWrite)
            {
                if (keyValuePairs.ContainsKey(key))
                {
                    keyValuePairs.Remove(key);
                }
                try
                {
                    XDocument recordXml = XDocument.Load(RecordPath);
                    XElement root = recordXml.Root;
                    var temp = root.Elements(key).ToList();
                    if (null != temp && temp.Count > 0)
                    {
                        Debug.LogWarning(temp.Count);
                        foreach (var item in temp)
                        {
                            Debug.LogWarning(item);
                            item.Remove();
                        }
                        recordXml.Save(RecordPath);
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
            }
            else
                PlayerPrefs.DeleteKey(key);
        }

    }
    public static bool HsaKey(string key)
    {
        lock (locker)
        {
            if (PlatformUtil.IsEditor || ForceWrite)
            {
                if (keyValuePairs.ContainsKey(key))
                {
                    return true;
                }
                else
                {
                    try
                    {
                        XDocument recordXml = XDocument.Load(RecordPath);
                        XElement root = recordXml.Root;
                        var temp = root.Elements(key).ToList();
                        if (null != temp && temp.Count > 0)
                        {
                            keyValuePairs[key] = temp[0].Value;
                            return true;
                        }
                        else
                            return false;
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogError(e);
                        return false;
                    }
                }
            }
            else
                return PlayerPrefs.HasKey(key);
        }
    }
}
网友评论