using System;
using UnityEngine.Serialization;
namespace UnityEngine.Animations.Rigging
{
///
/// The RigLayer is used by the RigBuilder to control in which order rigs will be
/// evaluated and whether they are active or not.
///
[Serializable]
public class RigLayer : IRigLayer
{
[SerializeField][FormerlySerializedAs("rig")] private Rig m_Rig;
[SerializeField][FormerlySerializedAs("active")] private bool m_Active = true;
private IRigConstraint[] m_Constraints;
private IAnimationJob[] m_Jobs;
/// The Rig associated to the RigLayer
public Rig rig { get => m_Rig; private set => m_Rig = value; }
/// The active state. True if the RigLayer is active, false otherwise.
public bool active { get => m_Active; set => m_Active = value; }
/// The RigLayer name.
public string name { get => (rig != null ? rig.gameObject.name : "no-name"); }
/// The list of constraints associated with the RigLayer.
public IRigConstraint[] constraints { get => isInitialized ? m_Constraints : null; }
/// The list of jobs built from constraints associated with the RigLayer.
public IAnimationJob[] jobs { get => isInitialized ? m_Jobs : null; }
/// Returns true if RigLayer was initialized or false otherwise.
///
public bool isInitialized { get; private set; }
///
/// Constructor.
///
/// The rig represented by this rig layer.
/// The active state of the rig layer.
public RigLayer(Rig rig, bool active = true)
{
this.rig = rig;
this.active = active;
}
///
/// Initializes the RigLayer. This will retrieve the constraints associated with the Rig
/// and create the animation jobs required by the PlayableGraph.
///
/// The Animator used to animate the RigLayer constraints.
/// True if RigLayer was initialized properly, false otherwise.
public bool Initialize(Animator animator)
{
if (isInitialized)
return true;
if (rig != null)
{
m_Constraints = RigUtils.GetConstraints(rig);
if (m_Constraints == null || m_Constraints.Length == 0)
return false;
m_Jobs = RigUtils.CreateAnimationJobs(animator, m_Constraints);
return (isInitialized = true);
}
return false;
}
///
/// Updates the RigLayer jobs. This is called during the Update loop before
/// the Animator evaluates the PlayableGraph.
///
public void Update()
{
if (!isInitialized)
return;
for (int i = 0, count = m_Constraints.Length; i < count; ++i)
m_Constraints[i].UpdateJob(m_Jobs[i]);
}
///
/// Resets the RigLayer. This will destroy the animation jobs and
/// free up memory.
///
public void Reset()
{
if (!isInitialized)
return;
RigUtils.DestroyAnimationJobs(m_Constraints, m_Jobs);
m_Constraints = null;
m_Jobs = null;
isInitialized = false;
}
///
/// Queries whether the RigLayer is valid.
///
/// True if RigLayer is valid, false otherwise.
public bool IsValid() => rig != null && isInitialized;
}
}