CinemachineBlenderSettings.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. [Tooltip("When blending from this camera")]
  22. public string m_From;
  23. [Tooltip("When blending to this camera")]
  24. public string m_To;
  25. [CinemachineBlendDefinitionProperty]
  26. [Tooltip("Blend curve definition")]
  27. public CinemachineBlendDefinition m_Blend;
  28. }
  29. /// <summary>The array containing explicitly defined blends between two Virtual Cameras</summary>
  30. [Tooltip("The array containing explicitly defined blends between two Virtual Cameras")]
  31. public CustomBlend[] m_CustomBlends = null;
  32. /// <summary>Internal API for the inspector editopr: a label to represent any camera</summary>
  33. public const string kBlendFromAnyCameraLabel = "**ANY CAMERA**";
  34. /// <summary>
  35. /// Attempts to find a blend definition which matches the to and from cameras as specified.
  36. /// If no match is found, the function returns the supplied default blend.
  37. /// </summary>
  38. /// <param name="fromCameraName">The game object name of the from camera</param>
  39. /// <param name="toCameraName">The game object name of the to camera</param>
  40. /// <param name="defaultBlend">Blend to return if no custom blend found.</param>
  41. /// <returns></returns>
  42. public CinemachineBlendDefinition GetBlendForVirtualCameras(
  43. string fromCameraName, string toCameraName, CinemachineBlendDefinition defaultBlend)
  44. {
  45. bool gotAnyToMe = false;
  46. bool gotMeToAny = false;
  47. CinemachineBlendDefinition anyToMe = defaultBlend;
  48. CinemachineBlendDefinition meToAny = defaultBlend;
  49. if (m_CustomBlends != null)
  50. {
  51. for (int i = 0; i < m_CustomBlends.Length; ++i)
  52. {
  53. // Attempt to find direct name first
  54. CustomBlend blendParams = m_CustomBlends[i];
  55. if ((blendParams.m_From == fromCameraName)
  56. && (blendParams.m_To == toCameraName))
  57. {
  58. return blendParams.m_Blend;
  59. }
  60. // If we come across applicable wildcards, remember them
  61. if (blendParams.m_From == kBlendFromAnyCameraLabel)
  62. {
  63. if (!string.IsNullOrEmpty(toCameraName)
  64. && blendParams.m_To == toCameraName)
  65. {
  66. if (!gotAnyToMe)
  67. anyToMe = blendParams.m_Blend;
  68. gotAnyToMe = true;
  69. }
  70. else if (blendParams.m_To == kBlendFromAnyCameraLabel)
  71. defaultBlend = blendParams.m_Blend;
  72. }
  73. else if (blendParams.m_To == kBlendFromAnyCameraLabel
  74. && !string.IsNullOrEmpty(fromCameraName)
  75. && blendParams.m_From == fromCameraName)
  76. {
  77. if (!gotMeToAny)
  78. meToAny = blendParams.m_Blend;
  79. gotMeToAny = true;
  80. }
  81. }
  82. }
  83. // If nothing is found try to find wild card blends from any
  84. // camera to our new one
  85. if (gotAnyToMe)
  86. return anyToMe;
  87. // Still have nothing? Try from our camera to any camera
  88. if (gotMeToAny)
  89. return meToAny;
  90. return defaultBlend;
  91. }
  92. }
  93. }