GroupWeightManipulator.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. namespace Cinemachine
  3. {
  4. /// <summary>
  5. /// A class to get around the limitation in timeline that array members can't be animated.
  6. /// A fixed number of slots are made available, rather than a dynamic array.
  7. /// If you want to add more slots, just modify this code.
  8. /// </summary>
  9. [RequireComponent(typeof(CinemachineTargetGroup))]
  10. #if UNITY_2018_3_OR_NEWER
  11. [ExecuteAlways]
  12. #else
  13. [ExecuteInEditMode]
  14. #endif
  15. [HelpURL(Documentation.BaseURL + "api/Cinemachine.GroupWeightManipulator.html")]
  16. public class GroupWeightManipulator : MonoBehaviour
  17. {
  18. /// <summary>The weight of the group member at index 0</summary>
  19. [Tooltip("The weight of the group member at index 0")]
  20. public float m_Weight0 = 1;
  21. /// <summary>The weight of the group member at index 1</summary>
  22. [Tooltip("The weight of the group member at index 1")]
  23. public float m_Weight1 = 1;
  24. /// <summary>The weight of the group member at index 2</summary>
  25. [Tooltip("The weight of the group member at index 2")]
  26. public float m_Weight2 = 1;
  27. /// <summary>The weight of the group member at index 3</summary>
  28. [Tooltip("The weight of the group member at index 3")]
  29. public float m_Weight3 = 1;
  30. /// <summary>The weight of the group member at index 4</summary>
  31. [Tooltip("The weight of the group member at index 4")]
  32. public float m_Weight4 = 1;
  33. /// <summary>The weight of the group member at index 5</summary>
  34. [Tooltip("The weight of the group member at index 5")]
  35. public float m_Weight5 = 1;
  36. /// <summary>The weight of the group member at index 6</summary>
  37. [Tooltip("The weight of the group member at index 6")]
  38. public float m_Weight6 = 1;
  39. /// <summary>The weight of the group member at index 7</summary>
  40. [Tooltip("The weight of the group member at index 7")]
  41. public float m_Weight7 = 1;
  42. CinemachineTargetGroup m_group;
  43. void Start()
  44. {
  45. m_group = GetComponent<CinemachineTargetGroup>();
  46. }
  47. void OnValidate()
  48. {
  49. m_Weight0 = Mathf.Max(0, m_Weight0);
  50. m_Weight1 = Mathf.Max(0, m_Weight1);
  51. m_Weight2 = Mathf.Max(0, m_Weight2);
  52. m_Weight3 = Mathf.Max(0, m_Weight3);
  53. m_Weight4 = Mathf.Max(0, m_Weight4);
  54. m_Weight5 = Mathf.Max(0, m_Weight5);
  55. m_Weight6 = Mathf.Max(0, m_Weight6);
  56. m_Weight7 = Mathf.Max(0, m_Weight7);
  57. }
  58. void Update()
  59. {
  60. if (m_group != null)
  61. UpdateWeights();
  62. }
  63. void UpdateWeights()
  64. {
  65. var targets = m_group.m_Targets;
  66. int last = targets.Length - 1;
  67. if (last < 0) return; targets[0].weight = m_Weight0;
  68. if (last < 1) return; targets[1].weight = m_Weight1;
  69. if (last < 2) return; targets[2].weight = m_Weight2;
  70. if (last < 3) return; targets[3].weight = m_Weight3;
  71. if (last < 4) return; targets[4].weight = m_Weight4;
  72. if (last < 5) return; targets[5].weight = m_Weight5;
  73. if (last < 6) return; targets[6].weight = m_Weight6;
  74. if (last < 7) return; targets[7].weight = m_Weight7;
  75. }
  76. }
  77. }