Skip to content

Commit 1458132

Browse files
committed
Save setting to ProjectSettings directory
1 parent 99e93b4 commit 1458132

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

Editor/PreferencesGUI.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class PreferencesGUI
1313
#if UNITY_2018_3_OR_NEWER
1414
private class MyPrefSettingsProvider : SettingsProvider
1515
{
16-
public MyPrefSettingsProvider(string path, SettingsScope scopes = SettingsScope.User)
16+
public MyPrefSettingsProvider(string path, SettingsScope scopes = SettingsScope.Project)
1717
: base(path, scopes)
1818
{ }
1919

@@ -26,24 +26,24 @@ public override void OnGUI(string searchContext)
2626
[SettingsProvider]
2727
static SettingsProvider NewPreferenceItem()
2828
{
29-
return new MyPrefSettingsProvider("Preferences/Lua Interactive");
29+
return new MyPrefSettingsProvider("Project/Lua Interactive");
3030
}
3131
#else
3232
[PreferenceItem("Lua Interactive")]
3333
#endif
3434
public static void OnGUI()
3535
{
36-
Settings.AutoClearLog = (Settings.ClearLog)EditorGUILayout.EnumPopup("Auto Clear Log", Settings.AutoClearLog);
36+
Settings.AutoClearLog = (ClearLogMode)EditorGUILayout.EnumPopup("Auto Clear Log", Settings.AutoClearLog);
3737

3838
EditorGUILayout.BeginHorizontal();
39-
Settings.LuaPath = EditorGUILayout.TextField("Lua Script File", Settings.LuaPath);
39+
Settings.ScriptPath = EditorGUILayout.TextField("Lua Script File", Settings.ScriptPath);
4040
if (GUILayout.Button("Browse", EditorStyles.miniButton, GUILayout.Width(80)))
4141
BrowseScriptFile();
4242
EditorGUILayout.EndHorizontal();
43-
if (!string.IsNullOrEmpty(Settings.LuaPath) && !File.Exists(Settings.LuaPath))
43+
if (!string.IsNullOrEmpty(Settings.ScriptPath) && !File.Exists(Settings.ScriptPath))
4444
EditorGUILayout.HelpBox("The file not exits", MessageType.Warning);
4545

46-
using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(Settings.LuaPath)))
46+
using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(Settings.ScriptPath)))
4747
{
4848
if (GUILayout.Button("Create defualt lua script"))
4949
CreateDefaultScript();
@@ -64,13 +64,13 @@ private static void BrowseScriptFile()
6464
folder.MakeRelativeUri(file)
6565
.ToString()
6666
);
67-
Settings.LuaPath = relativePath;
67+
Settings.ScriptPath = relativePath;
6868
}
6969
}
7070

7171
private static void CreateDefaultScript()
7272
{
73-
var path = Settings.LuaPath;
73+
var path = Settings.ScriptPath;
7474
if (string.IsNullOrEmpty(path)) return;
7575

7676
if (File.Exists(path))

Editor/Settings.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,72 @@
1+
using System.IO;
12
using UnityEditor;
23
using UnityEngine;
34

45
namespace litefeel.LuaInteractive.Editor
56
{
7+
public enum ClearLogMode
8+
{
9+
None,
10+
Previous,
11+
All,
12+
}
13+
public class SettingData
14+
{
15+
public string scriptPath;
16+
public ClearLogMode clearLogMode;
17+
}
618
public static class Settings
719
{
8-
private const string AutoClearLogKey = "litefeel.LuaInteractive.AutoClearLog";
9-
private const string LuaPathKey = "litefeel.LuaInteractive.LuaPath";
10-
11-
public enum ClearLog
12-
{
13-
None,
14-
Previous,
15-
All,
16-
}
17-
20+
const string SettingPath = "ProjectSettings/LuaInteractive.json";
21+
private static SettingData settingData;
1822

1923
[InitializeOnLoadMethod]
2024
private static void Init()
2125
{
22-
_AutoClearLog = (ClearLog)EditorPrefs.GetInt(AutoClearLogKey, 0);
23-
LuaPath = EditorPrefs.GetString(LuaPathKey, "");
26+
settingData = new SettingData();
27+
LoadData();
2428
}
2529

26-
private static ClearLog _AutoClearLog;
27-
public static ClearLog AutoClearLog
30+
public static ClearLogMode AutoClearLog
2831
{
29-
get { return _AutoClearLog; }
32+
get { return settingData.clearLogMode; }
3033
set
3134
{
32-
if (value != _AutoClearLog)
35+
if (value != settingData.clearLogMode)
3336
{
34-
_AutoClearLog = value;
35-
EditorPrefs.SetInt(AutoClearLogKey, (int)value);
37+
settingData.clearLogMode = value;
38+
SaveData();
3639
}
3740
}
3841
}
3942

40-
private static string _LuaPath;
41-
public static string LuaPath
43+
public static string ScriptPath
4244
{
43-
get { return _LuaPath; }
45+
get { return settingData.scriptPath; }
4446
set
4547
{
46-
if (value != _LuaPath)
48+
if (value != settingData.scriptPath)
4749
{
48-
_LuaPath = value;
49-
EditorPrefs.SetString(LuaPathKey, value);
50+
settingData.scriptPath = value;
51+
SaveData();
5052
}
5153
}
5254
}
55+
56+
private static void LoadData()
57+
{
58+
try
59+
{
60+
var data = File.ReadAllText(SettingPath);
61+
JsonUtility.FromJsonOverwrite(data, settingData);
62+
}
63+
catch { }
64+
}
65+
private static void SaveData()
66+
{
67+
var json = JsonUtility.ToJson(settingData);
68+
File.WriteAllText(SettingPath, json);
69+
}
5370
}
5471
}
5572

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Find the manifest.json file in the Packages folder of your project and edit it t
2828

2929
## How to use?
3030

31-
1. Select `Edit > Preferences… > Lua Interactive` from the menu
31+
1. Select `Edit > Project Settings… > Lua Interactive` from the menu
3232
2. Input a lua file path
3333
3. Press `Create defualt lua script`
3434
4. Play game

Runtime/LuaRunner.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ private void Update()
7676
{
7777
if (Input.GetKeyDown(KeyCode.R) && Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.LeftControl))
7878
{
79-
if (Settings.AutoClearLog == Settings.ClearLog.Previous)
79+
if (Settings.AutoClearLog == ClearLogMode.Previous)
8080
ClearLog();
81-
var path = Settings.LuaPath;
81+
var path = Settings.ScriptPath;
8282
if (!string.IsNullOrEmpty(path) && File.Exists(path))
8383
{
8484
var content = File.ReadAllText(path);
8585
DoString?.Invoke(luaState, new object[] { content, null });
8686
}
8787

88-
if (Settings.AutoClearLog == Settings.ClearLog.All)
88+
if (Settings.AutoClearLog == ClearLogMode.All)
8989
ClearLog();
9090
}
9191
}

0 commit comments

Comments
 (0)