blob: a6a3f9c53845c64248e3fd2742a8afb5958be8ae (
plain)
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
|
#if UNITY_EDITOR
using UnityEngine;
using VRC.SDK3.Avatars.ScriptableObjects;
using UnityEditor;
using System.IO;
namespace YOTS
{
[DisallowMultipleComponent]
[AddComponentMenu("YOTS Config")]
public class YOTSConfig : MonoBehaviour {
[Tooltip("The JSON configuration file.")]
public TextAsset jsonConfig;
[TextArea(5, 30)]
public string jsonContent;
[SerializeField, HideInInspector]
private TextAsset lastJsonConfig;
void OnValidate() {
gameObject.tag = "EditorOnly";
// Only update jsonContent when jsonConfig actually changes
if (jsonConfig != lastJsonConfig) {
if (jsonConfig != null) {
jsonContent = jsonConfig.text;
}
lastJsonConfig = jsonConfig;
}
}
}
[CustomEditor(typeof(YOTSConfig))]
public class YOTSConfigEditor : Editor
{
private YOTSConfig config;
private void OnEnable()
{
config = (YOTSConfig)target;
}
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
// Draw the default inspector
DrawDefaultInspector();
EditorGUILayout.HelpBox(
"You can inspect and edit the JSON config using the textbox above. " +
"Changes are saved automatically. Changes from external editors will " +
"appear upon tabbing back into Unity.",
MessageType.Info);
// If changes were made in the inspector
if (EditorGUI.EndChangeCheck())
{
// Save changes immediately
SaveJsonToFile();
}
// Only check for file changes if we're not currently editing
else if (config.jsonConfig != null)
{
string currentContent = config.jsonConfig.text;
if (currentContent != config.jsonContent)
{
config.jsonContent = currentContent;
GUI.changed = true;
}
}
// Check for Ctrl+S
Event e = Event.current;
if (e.type == EventType.KeyDown && e.keyCode == KeyCode.S && e.control)
{
e.Use();
SaveJsonToFile();
}
}
private void SaveJsonToFile()
{
if (config.jsonConfig == null)
{
Debug.LogWarning("No JSON config file assigned!");
return;
}
string assetPath = AssetDatabase.GetAssetPath(config.jsonConfig);
if (string.IsNullOrEmpty(assetPath))
{
Debug.LogError("Could not find asset path!");
return;
}
try
{
// Write the modified content from our component
File.WriteAllText(assetPath, config.jsonContent);
// Force Unity to reload the file from disk
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
// Update the TextAsset to match our changes
var serializedObject = new SerializedObject(config.jsonConfig);
serializedObject.FindProperty("m_Script").stringValue = config.jsonContent;
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(config.jsonConfig);
AssetDatabase.SaveAssets();
Debug.Log($"Successfully saved JSON to {assetPath}");
}
catch (System.Exception ex)
{
Debug.LogError($"Error saving JSON: {ex.Message}");
}
}
}
// Add this new class to handle file modifications
public class JsonFileProcessor : UnityEditor.AssetModificationProcessor
{
private static void OnWillSaveAssets(string[] paths)
{
foreach (string path in paths)
{
if (path.EndsWith(".json"))
{
// Force Unity to reimport the asset
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
}
}
}
#endif // UNITY_EDITOR
|