// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik //
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Audio;
using UnityEngine.Playables;
using Object = UnityEngine.Object;
namespace Animancer
{
/// [Pro-Only] An which plays a .
///
/// Documentation: Timeline
///
/// https://kybernetik.com.au/animancer/api/Animancer/PlayableAssetState
///
public class PlayableAssetState : AnimancerState, ICopyable
{
/************************************************************************************************************************/
/// An that creates a .
public interface ITransition : ITransition { }
/************************************************************************************************************************/
#region Fields and Properties
/************************************************************************************************************************/
/// The which this state plays.
private PlayableAsset _Asset;
/// The which this state plays.
public PlayableAsset Asset
{
get => _Asset;
set => ChangeMainObject(ref _Asset, value);
}
/// The which this state plays.
public override Object MainObject
{
get => _Asset;
set => _Asset = (PlayableAsset)value;
}
/************************************************************************************************************************/
private float _Length;
/// The .
public override float Length => _Length;
/************************************************************************************************************************/
///
protected override void OnSetIsPlaying()
{
var inputCount = _Playable.GetInputCount();
for (int i = 0; i < inputCount; i++)
{
var playable = _Playable.GetInput(i);
if (!playable.IsValid())
continue;
if (IsPlaying)
playable.Play();
else
playable.Pause();
}
}
/************************************************************************************************************************/
/// IK cannot be dynamically enabled on a .
public override void CopyIKFlags(AnimancerNode copyFrom) { }
/************************************************************************************************************************/
/// IK cannot be dynamically enabled on a .
public override bool ApplyAnimatorIK
{
get => false;
set
{
#if UNITY_ASSERTIONS
if (value)
OptionalWarning.UnsupportedIK.Log(
$"IK cannot be dynamically enabled on a {nameof(PlayableAssetState)}.", Root?.Component);
#endif
}
}
/************************************************************************************************************************/
/// IK cannot be dynamically enabled on a .
public override bool ApplyFootIK
{
get => false;
set
{
#if UNITY_ASSERTIONS
if (value)
OptionalWarning.UnsupportedIK.Log(
$"IK cannot be dynamically enabled on a {nameof(PlayableAssetState)}.", Root?.Component);
#endif
}
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Methods
/************************************************************************************************************************/
/// Creates a new to play the `asset`.
/// The `asset` is null.
public PlayableAssetState(PlayableAsset asset)
{
if (asset == null)
throw new ArgumentNullException(nameof(asset));
_Asset = asset;
}
/************************************************************************************************************************/
///
protected override void CreatePlayable(out Playable playable)
{
playable = _Asset.CreatePlayable(Root._Graph, Root.Component.gameObject);
playable.SetDuration(9223372.03685477);// https://github.com/KybernetikGames/animancer/issues/111
_Length = (float)_Asset.duration;
if (!_HasInitializedBindings)
InitializeBindings();
}
/************************************************************************************************************************/
private IList