GroupWeightManipulator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. [ExecuteAlways]
  11. [HelpURL(Documentation.BaseURL + "api/Cinemachine.GroupWeightManipulator.html")]
  12. public class GroupWeightManipulator : MonoBehaviour
  13. {
  14. /// <summary>The weight of the group member at index 0</summary>
  15. [Tooltip("The weight of the group member at index 0")]
  16. public float m_Weight0 = 1;
  17. /// <summary>The weight of the group member at index 1</summary>
  18. [Tooltip("The weight of the group member at index 1")]
  19. public float m_Weight1 = 1;
  20. /// <summary>The weight of the group member at index 2</summary>
  21. [Tooltip("The weight of the group member at index 2")]
  22. public float m_Weight2 = 1;
  23. /// <summary>The weight of the group member at index 3</summary>
  24. [Tooltip("The weight of the group member at index 3")]
  25. public float m_Weight3 = 1;
  26. /// <summary>The weight of the group member at index 4</summary>
  27. [Tooltip("The weight of the group member at index 4")]
  28. public float m_Weight4 = 1;
  29. /// <summary>The weight of the group member at index 5</summary>
  30. [Tooltip("The weight of the group member at index 5")]
  31. public float m_Weight5 = 1;
  32. /// <summary>The weight of the group member at index 6</summary>
  33. [Tooltip("The weight of the group member at index 6")]
  34. public float m_Weight6 = 1;
  35. /// <summary>The weight of the group member at index 7</summary>
  36. [Tooltip("The weight of the group member at index 7")]
  37. public float m_Weight7 = 1;
  38. CinemachineTargetGroup m_group;
  39. void Start()
  40. {
  41. m_group = GetComponent<CinemachineTargetGroup>();
  42. }
  43. void OnValidate()
  44. {
  45. m_Weight0 = Mathf.Max(0, m_Weight0);
  46. m_Weight1 = Mathf.Max(0, m_Weight1);
  47. m_Weight2 = Mathf.Max(0, m_Weight2);
  48. m_Weight3 = Mathf.Max(0, m_Weight3);
  49. m_Weight4 = Mathf.Max(0, m_Weight4);
  50. m_Weight5 = Mathf.Max(0, m_Weight5);
  51. m_Weight6 = Mathf.Max(0, m_Weight6);
  52. m_Weight7 = Mathf.Max(0, m_Weight7);
  53. }
  54. void Update()
  55. {
  56. if (m_group != null)
  57. UpdateWeights();
  58. }
  59. void UpdateWeights()
  60. {
  61. var targets = m_group.m_Targets;
  62. int last = targets.Length - 1;
  63. if (last < 0) return; targets[0].weight = m_Weight0;
  64. if (last < 1) return; targets[1].weight = m_Weight1;
  65. if (last < 2) return; targets[2].weight = m_Weight2;
  66. if (last < 3) return; targets[3].weight = m_Weight3;
  67. if (last < 4) return; targets[4].weight = m_Weight4;
  68. if (last < 5) return; targets[5].weight = m_Weight5;
  69. if (last < 6) return; targets[6].weight = m_Weight6;
  70. if (last < 7) return; targets[7].weight = m_Weight7;
  71. }
  72. }
  73. }