yum-slop/YOTS

an optimized toggle system for vrchat

git clone https://git.yummers.dev/yum-slop/YOTS

yumBugfix: dragging and dropping new config updates text field015d021

v1.1.0
3.9 KiB142 linesraw
1#if UNITY_EDITOR
2
3using UnityEngine;
4using nadena.dev.ndmf;
5using VRC.SDK3.Avatars.ScriptableObjects;
6using UnityEditor;
7using System.IO;
8
9namespace YOTS
10{
11  [DisallowMultipleComponent]
12  [AddComponentMenu("YOTS NDMF Config")]
13  public class YOTSNDMFConfig : MonoBehaviour {
14    [Tooltip("The JSON configuration file.")]
15    public TextAsset jsonConfig;
16    
17    [TextArea(5, 80)]  // Min 5 lines, max 80 lines
18    public string jsonContent;
19
20    [SerializeField, HideInInspector]
21    private TextAsset lastJsonConfig;
22
23    void OnValidate() {
24      gameObject.tag = "EditorOnly";
25      
26      // Only update jsonContent when jsonConfig actually changes
27      if (jsonConfig != lastJsonConfig) {
28        if (jsonConfig != null) {
29          jsonContent = jsonConfig.text;
30        }
31        lastJsonConfig = jsonConfig;
32      }
33    }
34  }
35
36  [CustomEditor(typeof(YOTSNDMFConfig))]
37  public class YOTSNDMFConfigEditor : Editor
38  {
39    private YOTSNDMFConfig config;
40
41    private void OnEnable()
42    {
43      config = (YOTSNDMFConfig)target;
44    }
45
46    public override void OnInspectorGUI()
47    {
48      EditorGUI.BeginChangeCheck();
49
50      // Draw the default inspector
51      DrawDefaultInspector();
52
53      EditorGUILayout.HelpBox(
54        "You can inspect and edit the JSON config using the textbox above. " +
55        "Changes are saved automatically. Changes from external editors will " +
56        "appear upon tabbing back into Unity.",
57        MessageType.Info);
58
59
60      // If changes were made in the inspector
61      if (EditorGUI.EndChangeCheck())
62      {
63        // Save changes immediately
64        SaveJsonToFile();
65      }
66      // Only check for file changes if we're not currently editing
67      else if (config.jsonConfig != null)
68      {
69        string currentContent = config.jsonConfig.text;
70        if (currentContent != config.jsonContent)
71        {
72          config.jsonContent = currentContent;
73          GUI.changed = true;
74        }
75      }
76
77      // Check for Ctrl+S
78      Event e = Event.current;
79      if (e.type == EventType.KeyDown && e.keyCode == KeyCode.S && e.control)
80      {
81        e.Use();
82        SaveJsonToFile();
83      }
84    }
85
86    private void SaveJsonToFile()
87    {
88      if (config.jsonConfig == null)
89      {
90        Debug.LogWarning("No JSON config file assigned!");
91        return;
92      }
93
94      string assetPath = AssetDatabase.GetAssetPath(config.jsonConfig);
95      if (string.IsNullOrEmpty(assetPath))
96      {
97        Debug.LogError("Could not find asset path!");
98        return;
99      }
100
101      try
102      {
103        // Write the modified content from our component
104        File.WriteAllText(assetPath, config.jsonContent);
105        
106        // Force Unity to reload the file from disk
107        AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
108        
109        // Update the TextAsset to match our changes
110        var serializedObject = new SerializedObject(config.jsonConfig);
111        serializedObject.FindProperty("m_Script").stringValue = config.jsonContent;
112        serializedObject.ApplyModifiedProperties();
113        
114        EditorUtility.SetDirty(config.jsonConfig);
115        AssetDatabase.SaveAssets();
116        Debug.Log($"Successfully saved JSON to {assetPath}");
117      }
118      catch (System.Exception ex)
119      {
120        Debug.LogError($"Error saving JSON: {ex.Message}");
121      }
122    }
123  }
124
125  // Add this new class to handle file modifications
126  public class JsonFileProcessor : UnityEditor.AssetModificationProcessor
127  {
128    private static void OnWillSaveAssets(string[] paths)
129    {
130      foreach (string path in paths)
131      {
132        if (path.EndsWith(".json"))
133        {
134            // Force Unity to reimport the asset
135            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
136        }
137      }
138    }
139  }
140}
141
142#endif  // UNITY_EDITOR