CinemachineImpulseChannels.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using UnityEngine;
  2. using System;
  3. using UnityEditor;
  4. namespace Cinemachine.Editor
  5. {
  6. /// <summary>
  7. /// This class contains setting for the Impulse system. Specifically, it holds
  8. /// the Impulse Channel definitions. These work like Unity Layers, you can
  9. /// define and name them, and create masks to filter only the layers you want.
  10. /// </summary>
  11. [Serializable]
  12. public class CinemachineImpulseChannels : ScriptableObject
  13. {
  14. static CinemachineImpulseChannels sInstance = null;
  15. private static bool alreadySearched = false;
  16. /// <summary>Get the singleton instance of this object, or null if it doesn't exist</summary>
  17. public static CinemachineImpulseChannels InstanceIfExists
  18. {
  19. get
  20. {
  21. if (!alreadySearched)
  22. {
  23. alreadySearched = true;
  24. var guids = AssetDatabase.FindAssets("t:CinemachineImpulseChannels");
  25. for (int i = 0; i < guids.Length && sInstance == null; ++i)
  26. sInstance = AssetDatabase.LoadAssetAtPath<CinemachineImpulseChannels>(
  27. AssetDatabase.GUIDToAssetPath(guids[i]));
  28. }
  29. if (sInstance != null)
  30. sInstance.EnsureDefaultLayer();
  31. return sInstance;
  32. }
  33. }
  34. /// <summary>Get the singleton instance of this object. Creates asset if nonexistant</summary>
  35. public static CinemachineImpulseChannels Instance
  36. {
  37. get
  38. {
  39. if (InstanceIfExists == null)
  40. {
  41. string newAssetPath = EditorUtility.SaveFilePanelInProject(
  42. "Create Impulse Channel Definition asset", "CinemachineImpulseChannels", "asset",
  43. "This editor-only file will contain the Impulse channels for this project");
  44. if (!string.IsNullOrEmpty(newAssetPath))
  45. {
  46. sInstance = CreateInstance<CinemachineImpulseChannels>();
  47. AssetDatabase.CreateAsset(sInstance, newAssetPath);
  48. AssetDatabase.SaveAssets();
  49. AssetDatabase.Refresh();
  50. }
  51. }
  52. if (sInstance != null)
  53. {
  54. sInstance.EnsureDefaultLayer();
  55. }
  56. return sInstance;
  57. }
  58. }
  59. // Make sure it has at least one layer in it
  60. void EnsureDefaultLayer()
  61. {
  62. if (ImpulseChannels == null || ImpulseChannels.Length == 0)
  63. ImpulseChannels = new string[] { "default" };
  64. }
  65. /// <summary>The currently-defined Impulse channel names</summary>
  66. public string[] ImpulseChannels;
  67. }
  68. }