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