SignalManager.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Timeline.Signals
  8. {
  9. class SignalManager : IDisposable
  10. {
  11. static SignalManager m_Instance;
  12. readonly List<SignalAsset> m_assets = new List<SignalAsset>();
  13. internal static SignalManager instance
  14. {
  15. get
  16. {
  17. if (m_Instance == null)
  18. {
  19. m_Instance = new SignalManager();
  20. m_Instance.Refresh();
  21. }
  22. return m_Instance;
  23. }
  24. set { m_Instance = value; }
  25. }
  26. internal SignalManager()
  27. {
  28. SignalAsset.OnEnableCallback += Register;
  29. }
  30. public static IEnumerable<SignalAsset> assets
  31. {
  32. get
  33. {
  34. foreach (var asset in instance.m_assets)
  35. {
  36. if (asset != null)
  37. yield return asset;
  38. }
  39. }
  40. }
  41. public static SignalAsset CreateSignalAssetInstance(string path)
  42. {
  43. var newSignal = ScriptableObject.CreateInstance<SignalAsset>();
  44. newSignal.name = Path.GetFileNameWithoutExtension(path);
  45. var asset = AssetDatabase.LoadMainAssetAtPath(path) as SignalAsset;
  46. if (asset != null)
  47. {
  48. TimelineUndo.PushUndo(asset, Styles.UndoCreateSignalAsset);
  49. EditorUtility.CopySerialized(newSignal, asset);
  50. Object.DestroyImmediate(newSignal);
  51. return asset;
  52. }
  53. AssetDatabase.CreateAsset(newSignal, path);
  54. return newSignal;
  55. }
  56. public void Dispose()
  57. {
  58. SignalAsset.OnEnableCallback -= Register;
  59. }
  60. void Register(SignalAsset a)
  61. {
  62. m_assets.Add(a);
  63. }
  64. void Refresh()
  65. {
  66. var guids = AssetDatabase.FindAssets("t:SignalAsset");
  67. foreach (var g in guids)
  68. {
  69. var path = AssetDatabase.GUIDToAssetPath(g);
  70. var asset = AssetDatabase.LoadAssetAtPath<SignalAsset>(path);
  71. m_assets.Add(asset);
  72. }
  73. }
  74. }
  75. }