Skip to content

Commit 1ea3515

Browse files
committed
Add Config
1 parent 5e530a6 commit 1ea3515

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

Editor/PreferencesGUI.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,49 @@ static SettingsProvider NewPreferenceItem()
3333
#endif
3434
public static void OnGUI()
3535
{
36-
Settings.AutoClearLog = EditorGUILayout.ToggleLeft("Auto Clear Log", Settings.AutoClearLog);
36+
Settings.AutoClearLog = (Settings.ClearLog)EditorGUILayout.EnumPopup("Auto Clear Log", Settings.AutoClearLog);
37+
38+
EditorGUILayout.BeginHorizontal();
3739
Settings.LuaPath = EditorGUILayout.TextField("Lua Script File", Settings.LuaPath);
38-
using(new EditorGUI.DisabledScope(string.IsNullOrEmpty(Settings.LuaPath)))
40+
if (GUILayout.Button("Browse", EditorStyles.miniButton, GUILayout.Width(80)))
41+
BrowseScriptFile();
42+
EditorGUILayout.EndHorizontal();
43+
if (!string.IsNullOrEmpty(Settings.LuaPath) && !File.Exists(Settings.LuaPath))
44+
EditorGUILayout.HelpBox("The file not exits", MessageType.Warning);
45+
46+
using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(Settings.LuaPath)))
3947
{
4048
if (GUILayout.Button("Create defualt lua script"))
4149
CreateDefaultScript();
4250
}
4351
}
4452

53+
private static void BrowseScriptFile()
54+
{
55+
var root = Directory.GetCurrentDirectory();
56+
var path = EditorUtility.SaveFilePanel("Save File", root, "", "lua");
57+
58+
if (!string.IsNullOrEmpty(path))
59+
{
60+
Uri file = new Uri(path);
61+
Uri folder = new Uri(root + "/");
62+
string relativePath =
63+
Uri.UnescapeDataString(
64+
folder.MakeRelativeUri(file)
65+
.ToString()
66+
);
67+
Settings.LuaPath = relativePath;
68+
}
69+
}
70+
4571
private static void CreateDefaultScript()
4672
{
4773
var path = Settings.LuaPath;
4874
if (string.IsNullOrEmpty(path)) return;
4975

5076
if (File.Exists(path))
5177
{
52-
if(!EditorUtility.DisplayDialog("Save File",
78+
if (!EditorUtility.DisplayDialog("Save File",
5379
$"the file already exists, do you want to overwrite it?\n{path}",
5480
"Overwrite", "Cancel"))
5581
{

Editor/Settings.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,31 @@ public static class Settings
88
private const string AutoClearLogKey = "litefeel.LuaInteractive.AutoClearLog";
99
private const string LuaPathKey = "litefeel.LuaInteractive.LuaPath";
1010

11+
public enum ClearLog
12+
{
13+
None,
14+
Previous,
15+
All,
16+
}
17+
1118

1219
[InitializeOnLoadMethod]
1320
private static void Init()
1421
{
15-
_AutoClearLog = EditorPrefs.GetBool(AutoClearLogKey, false);
22+
_AutoClearLog = (ClearLog)EditorPrefs.GetInt(AutoClearLogKey, 0);
1623
LuaPath = EditorPrefs.GetString(LuaPathKey, "");
1724
}
1825

19-
private static bool _AutoClearLog;
20-
public static bool AutoClearLog
26+
private static ClearLog _AutoClearLog;
27+
public static ClearLog AutoClearLog
2128
{
2229
get { return _AutoClearLog; }
2330
set
2431
{
2532
if (value != _AutoClearLog)
2633
{
2734
_AutoClearLog = value;
28-
EditorPrefs.SetBool(AutoClearLogKey, value);
35+
EditorPrefs.SetInt(AutoClearLogKey, (int)value);
2936
}
3037
}
3138
}

Runtime/LuaRunner.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#if UNITY_EDITOR
22
using litefeel.LuaInteractive.Editor;
3-
#endif
3+
44
using System;
55
using System.Collections;
66
using System.IO;
@@ -55,7 +55,6 @@ private static MethodInfo GetMethod(Type type, string name, object[] args)
5555
return null;
5656
}
5757

58-
// Start is called before the first frame update
5958
void Start()
6059
{
6160
if (GetLuaState != null && DoString != null)
@@ -73,32 +72,31 @@ private IEnumerator WaitLuaState()
7372
luaState = GetLuaState.Invoke(null, args);
7473
}
7574

76-
#if UNITY_EDITOR
7775
private void Update()
7876
{
7977
if (Input.GetKeyDown(KeyCode.R) && Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.LeftControl))
8078
{
79+
if (Settings.AutoClearLog == Settings.ClearLog.Previous)
80+
ClearLog();
8181
var path = Settings.LuaPath;
82-
if(!string.IsNullOrEmpty(path) && File.Exists(path))
82+
if (!string.IsNullOrEmpty(path) && File.Exists(path))
8383
{
8484
var content = File.ReadAllText(path);
8585
DoString?.Invoke(luaState, new object[] { content, null });
8686
}
87-
// const string chunk =
88-
// @"package.loaded['mydebug'] = nil
89-
//require('mydebug')";
90-
// DoString?.Invoke(luaState, new object[] { chunk, null });
91-
//luaState.DoString(chunk);
9287

93-
if (Settings.AutoClearLog)
94-
{
95-
Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
96-
Type type = assembly.GetType("UnityEditor.LogEntries");
97-
MethodInfo method = type.GetMethod("Clear");
98-
method.Invoke(new object(), null);
99-
}
88+
if (Settings.AutoClearLog == Settings.ClearLog.All)
89+
ClearLog();
10090
}
10191
}
102-
#endif
92+
93+
private void ClearLog()
94+
{
95+
Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
96+
Type type = assembly.GetType("UnityEditor.LogEntries");
97+
MethodInfo method = type.GetMethod("Clear");
98+
method.Invoke(new object(), null);
99+
}
103100
}
104101
}
102+
#endif

0 commit comments

Comments
 (0)