Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IKManager2D.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections.Generic;
  2. using Unity.Profiling;
  3. using UnityEngine.Scripting.APIUpdating;
  4. using UnityEngine.U2D.Common;
  5. namespace UnityEngine.U2D.IK
  6. {
  7. /// <summary>
  8. /// Component responsible for managing and updating 2D IK Solvers.
  9. /// </summary>
  10. [DefaultExecutionOrder(-2)]
  11. [MovedFrom("UnityEngine.Experimental.U2D.IK")]
  12. [IconAttribute(IconUtility.IconPath + "Animation.IKManager.png")]
  13. [ExecuteInEditMode]
  14. public partial class IKManager2D : MonoBehaviour, IPreviewable
  15. {
  16. #if UNITY_EDITOR
  17. internal static event System.Action<IKManager2D> onEnabledEditor;
  18. internal static event System.Action<IKManager2D> onDisabledEditor;
  19. #endif
  20. [SerializeField]
  21. List<Solver2D> m_Solvers = new List<Solver2D>();
  22. [SerializeField]
  23. [Range(0f, 1f)]
  24. float m_Weight = 1f;
  25. /// <summary>
  26. /// Get and set the weight for solvers.
  27. /// </summary>
  28. public float weight
  29. {
  30. get => m_Weight;
  31. set => m_Weight = Mathf.Clamp01(value);
  32. }
  33. /// <summary>
  34. /// Get the Solvers that are managed by this manager.
  35. /// </summary>
  36. public List<Solver2D> solvers => m_Solvers;
  37. void OnValidate()
  38. {
  39. m_Weight = Mathf.Clamp01(m_Weight);
  40. OnEditorDataValidate();
  41. }
  42. void Reset()
  43. {
  44. FindChildSolvers();
  45. OnEditorDataValidate();
  46. }
  47. void FindChildSolvers()
  48. {
  49. m_Solvers.Clear();
  50. var solvers = new List<Solver2D>();
  51. transform.GetComponentsInChildren<Solver2D>(true, solvers);
  52. foreach (var solver in solvers)
  53. {
  54. if (solver.GetComponentInParent<IKManager2D>() == this)
  55. AddSolver(solver);
  56. }
  57. }
  58. /// <summary>
  59. /// Add Solver to the manager.
  60. /// </summary>
  61. /// <param name="solver">Solver to add.</param>
  62. public void AddSolver(Solver2D solver)
  63. {
  64. if (!m_Solvers.Contains(solver))
  65. {
  66. m_Solvers.Add(solver);
  67. AddSolverEditorData();
  68. }
  69. }
  70. /// <summary>
  71. /// Remove Solver from the manager.
  72. /// </summary>
  73. /// <param name="solver">Solver to remove.</param>
  74. public void RemoveSolver(Solver2D solver)
  75. {
  76. RemoveSolverEditorData(solver);
  77. m_Solvers.Remove(solver);
  78. }
  79. /// <summary>
  80. /// Updates the Solvers in this manager.
  81. /// </summary>
  82. public void UpdateManager()
  83. {
  84. var profilerMarker = new ProfilerMarker("IKManager2D.UpdateManager");
  85. profilerMarker.Begin();
  86. foreach (var solver in m_Solvers)
  87. {
  88. if (solver == null || !solver.isActiveAndEnabled)
  89. continue;
  90. if (!solver.isValid)
  91. solver.Initialize();
  92. solver.UpdateIK(weight);
  93. }
  94. profilerMarker.End();
  95. }
  96. /// <summary>
  97. /// Used by the animation clip preview window. Recommended to not use outside of this purpose.
  98. /// </summary>
  99. public void OnPreviewUpdate()
  100. {
  101. #if UNITY_EDITOR
  102. if (IsInGUIUpdateLoop())
  103. UpdateManager();
  104. #endif
  105. }
  106. static bool IsInGUIUpdateLoop() => Event.current != null;
  107. void LateUpdate()
  108. {
  109. UpdateManager();
  110. }
  111. #if UNITY_EDITOR
  112. void OnEnable()
  113. {
  114. onEnabledEditor?.Invoke(this);
  115. }
  116. void OnDisable()
  117. {
  118. onDisabledEditor?.Invoke(this);
  119. }
  120. internal static Events.UnityEvent onDrawGizmos = new Events.UnityEvent();
  121. void OnDrawGizmos()
  122. {
  123. onDrawGizmos.Invoke();
  124. }
  125. #endif
  126. }
  127. }