1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace TEngine { public class SaveModule : Module, ISaveModule { private SQLiteHelper sql; public override void OnInit() { string dbPath = "URI=file:" + Application.persistentDataPath + "/save.db"; sql = new SQLiteHelper(dbPath);
var reader = sql.ExecuteQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='SaveTable';"); if (!reader.HasRows) { sql.CreateTable( "SaveTable", new string[] { "Key", "Value", "Type" }, new string[] { "TEXT PRIMARY KEY", "TEXT", "TEXT" } ); Debug.Log("✅ 创建数据表 SaveTable 成功"); } else { Debug.Log("✅ 数据表 SaveTable 已存在,跳过创建"); } reader.Close(); }
public override void Shutdown() { if (sql != null) { sql.CloseConnection(); sql = null; } } private void SetValue(string key, string value, string type) { var reader = sql.ExecuteQuery($"SELECT key FROM SaveTable WHERE key='{key}'"); bool exists = reader.Read(); reader.Close();
if (exists) { sql.ExecuteQuery($"UPDATE SaveTable SET value='{value}', type='{type}' WHERE key='{key}'"); } else { sql.ExecuteQuery($"INSERT INTO SaveTable (key, value, type) VALUES ('{key}', '{value}', '{type}')"); } }
private string GetValue(string key) { var reader = sql.ExecuteQuery($"SELECT value FROM SaveTable WHERE key='{key}'"); string result = null; if (reader.Read()) { result = reader["value"].ToString(); } reader.Close(); return result; }
public void RemoveAllKey() { sql.ExecuteQuery("DELETE FROM SaveTable");
PlayerPrefs.DeleteAll(); PlayerPrefs.Save(); } public int GetInt(string key, int defaultValue = 0) { string v = GetValue(key); if (int.TryParse(v, out int result)) return result; return defaultValue; }
public float GetFloat(string key, float defaultValue = 0) { string v = GetValue(key); if (float.TryParse(v, out float result)) return result; return defaultValue; }
public string GetString(string key, string defaultValue = null) { string v = GetValue(key); return string.IsNullOrEmpty(v) ? defaultValue : v; }
public bool GetBool(string key, bool defaultValue = false) { string v = GetValue(key); if (v == null) return defaultValue; return v == "1" || v.ToLower() == "true"; }
public void SetInt(string key, int value) { SetValue(key, value.ToString(), "int"); }
public void SetFloat(string key, float value) { SetValue(key, value.ToString(), "float"); }
public void SetString(string key, string value) { SetValue(key, value, "string"); }
public void SetBool(string key, bool value) { SetValue(key, value ? "1" : "0", "bool"); }
public void RemoveKey(string key) { sql.ExecuteQuery($"DELETE FROM SaveTable WHERE key='{key}'"); }
public int GetIntLocal(string key, int defaultValue = 0) { return PlayerPrefs.GetInt(key, defaultValue); }
public float GetFloatLocal(string key, float defaultValue = 0) { return PlayerPrefs.GetFloat(key, defaultValue); }
public string GetStringLocal(string key, string defaultValue = null) { return PlayerPrefs.GetString(key, defaultValue); }
public bool GetBoolLocal(string key, bool defaultValue = false) { return PlayerPrefs.GetInt(key, defaultValue ? 1 : 0) == 1; }
public void SetIntLocal(string key, int value = 0) { PlayerPrefs.SetInt(key, value); }
public void SetFloatLocal(string key, float value = 0) { PlayerPrefs.SetFloat(key, value); }
public void SetStringLocal(string key, string value = null) { PlayerPrefs.SetString(key, value); }
public void SetBoolLocal(string key, bool value = false) { PlayerPrefs.SetInt(key, value ? 1 : 0); }
public void RemoveKeyLocal(string key) { PlayerPrefs.DeleteKey(key); } } }
|