using System; using System.Collections.Generic; using UnityEngine.Scripting.APIUpdating; namespace UnityEngine.U2D.IK { /// /// Component to manager 2D IK Solvers. /// [DefaultExecutionOrder(-2)] [MovedFrom("UnityEngine.Experimental.U2D.IK")] public partial class IKManager2D : MonoBehaviour { [SerializeField] private List m_Solvers = new List(); [SerializeField][Range(0f, 1f)] private float m_Weight = 1f; /// /// Get and Set the weight for solvers. /// public float weight { get { return m_Weight; } set { m_Weight = Mathf.Clamp01(value); } } /// /// Get the Solvers that are managed by this manager. /// public List solvers { get { return m_Solvers; } } private void OnValidate() { m_Weight = Mathf.Clamp01(m_Weight); OnEditorDataValidate(); } private void OnEnable() { } private void Reset() { FindChildSolvers(); OnEditorDataValidate(); } private void FindChildSolvers() { m_Solvers.Clear(); List solvers = new List(); transform.GetComponentsInChildren(true, solvers); foreach (Solver2D solver in solvers) { if (solver.GetComponentInParent() == this) AddSolver(solver); } } /// /// Add Solver to the manager. /// /// Solver to add. public void AddSolver(Solver2D solver) { if (!m_Solvers.Contains(solver)) { m_Solvers.Add(solver); AddSolverEditorData(); } } /// /// Remove Solver from the manager. /// /// Solver to remove. public void RemoveSolver(Solver2D solver) { RemoveSolverEditorData(solver); m_Solvers.Remove(solver); } /// /// Updates the Solvers in this manager. /// public void UpdateManager() { foreach (var solver in m_Solvers) { if (solver == null || !solver.isActiveAndEnabled) continue; if (!solver.isValid) solver.Initialize(); solver.UpdateIK(weight); } } private void LateUpdate() { UpdateManager(); } #if UNITY_EDITOR internal static Events.UnityEvent onDrawGizmos = new Events.UnityEvent(); private void OnDrawGizmos() { onDrawGizmos.Invoke(); } #endif } }