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