CinemachineBlenderSettings.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. using System;
  3. namespace Cinemachine
  4. {
  5. /// <summary>
  6. /// Asset that defines the rules for blending between Virtual Cameras.
  7. /// </summary>
  8. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  9. [Serializable]
  10. [HelpURL(Documentation.BaseURL + "manual/CinemachineBlending.html")]
  11. public sealed class CinemachineBlenderSettings : ScriptableObject
  12. {
  13. /// <summary>
  14. /// Container specifying how two specific Cinemachine Virtual Cameras
  15. /// blend together.
  16. /// </summary>
  17. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  18. [Serializable]
  19. public struct CustomBlend
  20. {
  21. /// <summary>When blending from this camera</summary>
  22. [Tooltip("When blending from this camera")]
  23. public string m_From;
  24. /// <summary>When blending to this camera</summary>
  25. [Tooltip("When blending to this camera")]
  26. public string m_To;
  27. /// <summary>Blend curve definition</summary>
  28. [CinemachineBlendDefinitionProperty]
  29. [Tooltip("Blend curve definition")]
  30. public CinemachineBlendDefinition m_Blend;
  31. }
  32. /// <summary>The array containing explicitly defined blends between two Virtual Cameras</summary>
  33. [Tooltip("The array containing explicitly defined blends between two Virtual Cameras")]
  34. public CustomBlend[] m_CustomBlends = null;
  35. /// <summary>Internal API for the inspector editopr: a label to represent any camera</summary>
  36. public const string kBlendFromAnyCameraLabel = "**ANY CAMERA**";
  37. /// <summary>
  38. /// Attempts to find a blend definition which matches the to and from cameras as specified.
  39. /// If no match is found, the function returns the supplied default blend.
  40. /// </summary>
  41. /// <param name="fromCameraName">The game object name of the from camera</param>
  42. /// <param name="toCameraName">The game object name of the to camera</param>
  43. /// <param name="defaultBlend">Blend to return if no custom blend found.</param>
  44. /// <returns></returns>
  45. public CinemachineBlendDefinition GetBlendForVirtualCameras(
  46. string fromCameraName, string toCameraName, CinemachineBlendDefinition defaultBlend)
  47. {
  48. bool gotAnyToMe = false;
  49. bool gotMeToAny = false;
  50. CinemachineBlendDefinition anyToMe = defaultBlend;
  51. CinemachineBlendDefinition meToAny = defaultBlend;
  52. if (m_CustomBlends != null)
  53. {
  54. for (int i = 0; i < m_CustomBlends.Length; ++i)
  55. {
  56. // Attempt to find direct name first
  57. CustomBlend blendParams = m_CustomBlends[i];
  58. if ((blendParams.m_From == fromCameraName)
  59. && (blendParams.m_To == toCameraName))
  60. {
  61. return blendParams.m_Blend;
  62. }
  63. // If we come across applicable wildcards, remember them
  64. if (blendParams.m_From == kBlendFromAnyCameraLabel)
  65. {
  66. if (!string.IsNullOrEmpty(toCameraName)
  67. && blendParams.m_To == toCameraName)
  68. {
  69. if (!gotAnyToMe)
  70. anyToMe = blendParams.m_Blend;
  71. gotAnyToMe = true;
  72. }
  73. else if (blendParams.m_To == kBlendFromAnyCameraLabel)
  74. defaultBlend = blendParams.m_Blend;
  75. }
  76. else if (blendParams.m_To == kBlendFromAnyCameraLabel
  77. && !string.IsNullOrEmpty(fromCameraName)
  78. && blendParams.m_From == fromCameraName)
  79. {
  80. if (!gotMeToAny)
  81. meToAny = blendParams.m_Blend;
  82. gotMeToAny = true;
  83. }
  84. }
  85. }
  86. // If nothing is found try to find wild card blends from any
  87. // camera to our new one
  88. if (gotAnyToMe)
  89. return anyToMe;
  90. // Still have nothing? Try from our camera to any camera
  91. if (gotMeToAny)
  92. return meToAny;
  93. return defaultBlend;
  94. }
  95. }
  96. }