SaveSettingsData.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Cristian Pop - https://boxophobic.com/
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Boxophobic.Utils
  6. {
  7. public partial class SettingsUtils
  8. {
  9. public static void SaveSettingsData(string settingsPath, string data)
  10. {
  11. CreateFileIfMissing(settingsPath);
  12. var settings = AssetDatabase.LoadAssetAtPath<SettingsData>(settingsPath);
  13. settings.data = data;
  14. SaveFile(settingsPath);
  15. }
  16. public static void SaveSettingsData(string settingsPath, int data)
  17. {
  18. CreateFileIfMissing(settingsPath);
  19. var settings = AssetDatabase.LoadAssetAtPath<SettingsData>(settingsPath);
  20. settings.data = data.ToString();
  21. SaveFile(settingsPath);
  22. }
  23. public static void SaveSettingsData(string settingsPath, float data)
  24. {
  25. CreateFileIfMissing(settingsPath);
  26. var settings = AssetDatabase.LoadAssetAtPath<SettingsData>(settingsPath);
  27. settings.data = data.ToString();
  28. SaveFile(settingsPath);
  29. }
  30. private static void CreateFileIfMissing(string settingsPath)
  31. {
  32. if (File.Exists(settingsPath) == false)
  33. {
  34. var directory = Path.GetDirectoryName(settingsPath);
  35. if (Directory.Exists(directory) == false)
  36. {
  37. Directory.CreateDirectory(directory);
  38. AssetDatabase.Refresh();
  39. }
  40. AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<SettingsData>(), settingsPath);
  41. AssetDatabase.Refresh();
  42. }
  43. }
  44. private static void SaveFile(string settingsPath)
  45. {
  46. var file = AssetDatabase.LoadAssetAtPath<SettingsData>(settingsPath);
  47. EditorUtility.SetDirty(file);
  48. AssetDatabase.SaveAssets();
  49. AssetDatabase.Refresh();
  50. }
  51. }
  52. }