ScriptableObjectViewPrefs.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.IO;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. using UnityObject = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. class ScriptableObjectViewPrefs<TViewModel> : IDisposable where TViewModel : ScriptableObject
  9. {
  10. const string k_DefaultFilePath = "Library/";
  11. const string k_Extension = ".pref";
  12. readonly string m_RelativePath;
  13. readonly string m_AbsolutePath;
  14. readonly string m_FileName;
  15. ScriptableObject m_Asset;
  16. TViewModel m_ViewModel;
  17. bool isSavable
  18. {
  19. get
  20. {
  21. return m_Asset != null &&
  22. m_ViewModel != null &&
  23. !string.IsNullOrEmpty(m_FileName);
  24. }
  25. }
  26. public ScriptableObjectViewPrefs(ScriptableObject asset, string relativeSavePath)
  27. {
  28. m_Asset = asset;
  29. m_RelativePath = string.IsNullOrEmpty(relativeSavePath) ? k_DefaultFilePath : relativeSavePath;
  30. if (!m_RelativePath.EndsWith("/", StringComparison.Ordinal))
  31. m_RelativePath += "/";
  32. m_AbsolutePath = Application.dataPath + "/../" + m_RelativePath;
  33. var assetKey = GetAssetKey(asset);
  34. m_FileName = string.IsNullOrEmpty(assetKey) ? string.Empty : assetKey + k_Extension;
  35. }
  36. public TViewModel viewModel
  37. {
  38. get
  39. {
  40. if (m_ViewModel == null)
  41. {
  42. if (m_Asset == null)
  43. m_ViewModel = CreateViewModel();
  44. else
  45. m_ViewModel = LoadViewModel() ?? CreateViewModel();
  46. }
  47. return m_ViewModel;
  48. }
  49. }
  50. public void Save()
  51. {
  52. if (!isSavable)
  53. return;
  54. // make sure the path exists or file write will fail
  55. if (!Directory.Exists(m_AbsolutePath))
  56. Directory.CreateDirectory(m_AbsolutePath);
  57. const bool saveAsText = true;
  58. InternalEditorUtility.SaveToSerializedFileAndForget(new UnityObject[] { m_ViewModel }, m_RelativePath + m_FileName, saveAsText);
  59. }
  60. public void DeleteFile()
  61. {
  62. if (!isSavable)
  63. return;
  64. var path = m_AbsolutePath + m_FileName;
  65. if (!File.Exists(path))
  66. return;
  67. File.Delete(path);
  68. }
  69. public void Dispose()
  70. {
  71. if (m_ViewModel != null)
  72. UnityObject.DestroyImmediate(m_ViewModel);
  73. m_Asset = null;
  74. }
  75. public static TViewModel CreateViewModel()
  76. {
  77. var model = ScriptableObject.CreateInstance<TViewModel>();
  78. model.hideFlags |= HideFlags.HideAndDontSave;
  79. return model;
  80. }
  81. TViewModel LoadViewModel()
  82. {
  83. if (string.IsNullOrEmpty(m_FileName))
  84. return null;
  85. var objects = InternalEditorUtility.LoadSerializedFileAndForget(m_RelativePath + m_FileName);
  86. if (objects.Length <= 0 || objects[0] == null)
  87. return null;
  88. var model = (TViewModel)objects[0];
  89. model.hideFlags |= HideFlags.HideAndDontSave;
  90. return model;
  91. }
  92. static string GetAssetKey(UnityObject asset)
  93. {
  94. return asset == null ? string.Empty : AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset));
  95. }
  96. }
  97. }