live2D (sdk가 6.5 미지원으로 sprite로 대응)
This commit is contained in:
374
Assets/Live2D/Cubism/Framework/Motion/CubismMotionController.cs
Normal file
374
Assets/Live2D/Cubism/Framework/Motion/CubismMotionController.cs
Normal file
@@ -0,0 +1,374 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
|
||||
using Live2D.Cubism.Core;
|
||||
using Live2D.Cubism.Framework.MotionFade;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.Playables;
|
||||
|
||||
|
||||
namespace Live2D.Cubism.Framework.Motion
|
||||
{
|
||||
/// <summary>
|
||||
/// Cubism motion controller.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(CubismFadeController))]
|
||||
public class CubismMotionController : MonoBehaviour
|
||||
{
|
||||
#region Action
|
||||
|
||||
/// <summary>
|
||||
/// Action animation start handler.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public Action<int> AnimationBeginHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Action animation end handler.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public Action<int> AnimationEndHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Action OnAnimationEnd.
|
||||
/// </summary>
|
||||
private void OnAnimationBegin(int layerIndex, int instanceId)
|
||||
{
|
||||
if (AnimationBeginHandler != null)
|
||||
{
|
||||
AnimationBeginHandler(instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Action OnAnimationEnd.
|
||||
/// </summary>
|
||||
private void OnAnimationEnd(int layerIndex, int instanceId)
|
||||
{
|
||||
_motionPriorities[layerIndex] = CubismMotionPriority.PriorityNone;
|
||||
|
||||
if (AnimationEndHandler != null)
|
||||
{
|
||||
AnimationEndHandler(instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variable
|
||||
|
||||
/// <summary>
|
||||
/// Layer count.
|
||||
/// </summary>
|
||||
public int LayerCount = 1;
|
||||
|
||||
/// <summary>
|
||||
/// List of cubism fade motion.
|
||||
/// </summary>
|
||||
private CubismFadeMotionList _cubismFadeMotionList;
|
||||
|
||||
/// <summary>
|
||||
/// Motion controller is active.
|
||||
/// </summary>
|
||||
private bool _isActive = false;
|
||||
|
||||
/// <summary>
|
||||
/// Playable graph controller.
|
||||
/// </summary>
|
||||
private PlayableGraph _playableGrap;
|
||||
|
||||
/// <summary>
|
||||
/// Playable output.
|
||||
/// </summary>
|
||||
private AnimationPlayableOutput _playableOutput;
|
||||
|
||||
/// <summary>
|
||||
/// Animation layer mixer.
|
||||
/// </summary>
|
||||
private AnimationLayerMixerPlayable _layerMixer;
|
||||
|
||||
/// <summary>
|
||||
/// Cubism motion layers.
|
||||
/// </summary>
|
||||
private CubismMotionLayer[] _motionLayers;
|
||||
|
||||
/// <summary>
|
||||
/// Cubism motion priorities.
|
||||
/// </summary>
|
||||
private int[] _motionPriorities;
|
||||
|
||||
#endregion Variable
|
||||
|
||||
#region Function
|
||||
|
||||
/// <summary>
|
||||
/// Play animations.
|
||||
/// </summary>
|
||||
/// <param name="clip">Animator clip.</param>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
/// <param name="priority">Animation priority</param>
|
||||
/// <param name="isLoop">Animation is loop.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
public void PlayAnimation(AnimationClip clip, int layerIndex = 0, int priority = CubismMotionPriority.PriorityNormal, bool isLoop = true, float speed = 1.0f)
|
||||
{
|
||||
// Fail silently...
|
||||
if(!enabled || !_isActive || _cubismFadeMotionList == null || clip == null
|
||||
|| layerIndex < 0 || layerIndex >= LayerCount ||
|
||||
((_motionPriorities[layerIndex] >= priority) && (priority != CubismMotionPriority.PriorityForce)))
|
||||
{
|
||||
Debug.Log("can't start motion.");
|
||||
return;
|
||||
}
|
||||
|
||||
_motionPriorities[layerIndex] = priority;
|
||||
_motionLayers[layerIndex].PlayAnimation(clip, isLoop, speed);
|
||||
|
||||
// Play Playable Graph
|
||||
if(!_playableGrap.IsPlaying())
|
||||
{
|
||||
_playableGrap.Play();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop animation.
|
||||
/// </summary>
|
||||
/// <param name="animationIndex">Animator index.</param>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
public void StopAnimation(int animationIndex, int layerIndex = 0)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex < 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_motionLayers[layerIndex].StopAnimationClip();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop all animation.
|
||||
/// </summary>
|
||||
public void StopAllAnimation()
|
||||
{
|
||||
for(var i = 0; i < LayerCount; ++i)
|
||||
{
|
||||
_motionLayers[i].StopAnimationClip();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is playing animation.
|
||||
/// </summary>
|
||||
/// <returns>True if the animation is playing.</returns>
|
||||
public bool IsPlayingAnimation(int layerIndex = 0)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex < 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return !_motionLayers[layerIndex].IsFinished;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set layer weight.
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
/// <param name="weight">Layer weight.</param>
|
||||
public void SetLayerWeight(int layerIndex, float weight)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex <= 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_motionLayers[layerIndex].SetLayerWeight(weight);
|
||||
_layerMixer.SetInputWeight(layerIndex, weight);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set layer blend type is additive.
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
/// <param name="isAdditive">Blend type is additive.</param>
|
||||
public void SetLayerAdditive(int layerIndex, bool isAdditive)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex <= 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_layerMixer.SetLayerAdditive((uint)layerIndex, isAdditive);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set animation speed.
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
/// <param name="index">index of playing motion list.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
public void SetAnimationSpeed(int layerIndex, int index, float speed)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex < 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_motionLayers[layerIndex].SetStateSpeed(index, speed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set animation is loop.
|
||||
/// </summary>
|
||||
/// <param name="layerIndex">layer index.</param>
|
||||
/// <param name="index">Index of playing motion list.</param>
|
||||
/// <param name="isLoop">State is loop.</param>
|
||||
public void SetAnimationIsLoop(int layerIndex, int index, bool isLoop)
|
||||
{
|
||||
// Fail silently...
|
||||
if(layerIndex < 0 || layerIndex >= LayerCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_motionLayers[layerIndex].SetStateIsLoop(index, isLoop);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get cubism fade states.
|
||||
/// </summary>
|
||||
public ICubismFadeState[] GetFadeStates()
|
||||
{
|
||||
if(_motionLayers == null)
|
||||
{
|
||||
LayerCount = (LayerCount < 1) ? 1 : LayerCount;
|
||||
_motionLayers = new CubismMotionLayer[LayerCount];
|
||||
_motionPriorities = new int[LayerCount];
|
||||
}
|
||||
|
||||
return _motionLayers;
|
||||
}
|
||||
|
||||
#endregion Function
|
||||
|
||||
#region Unity Events Handling
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity.
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
_cubismFadeMotionList = GetComponent<CubismFadeController>().CubismFadeMotionList;
|
||||
|
||||
// Fail silently...
|
||||
if(_cubismFadeMotionList == null)
|
||||
{
|
||||
Debug.LogError("CubismMotionController : CubismFadeMotionList doesn't set in CubismFadeController.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get Animator.
|
||||
var animator = GetComponent<Animator>();
|
||||
|
||||
if (animator.runtimeAnimatorController != null)
|
||||
{
|
||||
Debug.LogWarning("Animator Controller was set in Animator component.");
|
||||
return;
|
||||
}
|
||||
|
||||
_isActive = true;
|
||||
|
||||
// Disable animator's playablegrap.
|
||||
var graph = animator.playableGraph;
|
||||
|
||||
if(graph.IsValid())
|
||||
{
|
||||
graph.GetOutput(0).SetWeight(0);
|
||||
}
|
||||
|
||||
// Create Playable Graph.
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
_playableGrap = PlayableGraph.Create("Playable Graph : " + this.FindCubismModel().name);
|
||||
#else
|
||||
_playableGrap = PlayableGraph.Create();
|
||||
#endif
|
||||
_playableGrap.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
|
||||
|
||||
// Create Playable Output.
|
||||
_playableOutput = AnimationPlayableOutput.Create(_playableGrap, "Animation", animator);
|
||||
_playableOutput.SetWeight(1);
|
||||
|
||||
// Create animation layer mixer.
|
||||
_layerMixer = AnimationLayerMixerPlayable.Create(_playableGrap, LayerCount);
|
||||
|
||||
// Create cubism motion layers.
|
||||
if(_motionLayers == null)
|
||||
{
|
||||
LayerCount = (LayerCount < 1) ? 1 : LayerCount;
|
||||
_motionLayers = new CubismMotionLayer[LayerCount];
|
||||
_motionPriorities = new int[LayerCount];
|
||||
}
|
||||
|
||||
for(var i = 0; i < LayerCount; ++i)
|
||||
{
|
||||
_motionLayers[i] = CubismMotionLayer.CreateCubismMotionLayer(_playableGrap, _cubismFadeMotionList, i);
|
||||
_motionLayers[i].AnimationBeginHandler += OnAnimationBegin;
|
||||
_motionLayers[i].AnimationEndHandler += OnAnimationEnd;
|
||||
_layerMixer.ConnectInput(i, _motionLayers[i].PlayableOutput, 0);
|
||||
_layerMixer.SetInputWeight(i, 1.0f);
|
||||
}
|
||||
|
||||
// Set Playable Output.
|
||||
_playableOutput.SetSourcePlayable(_layerMixer);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity.
|
||||
/// </summary>
|
||||
private void OnDisable()
|
||||
{
|
||||
// Destroy _playableGrap.
|
||||
if(_playableGrap.IsValid())
|
||||
{
|
||||
_playableGrap.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity.
|
||||
/// </summary>
|
||||
private void Update()
|
||||
{
|
||||
// Fail silently...
|
||||
if(!_isActive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for( var i = 0; i < _motionLayers.Length; ++i)
|
||||
{
|
||||
_motionLayers[i].Update();
|
||||
|
||||
if (_motionLayers[i].IsFinished)
|
||||
{
|
||||
_motionPriorities[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Unity Events Handling
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5ebe254d34723249a16ad2f6ba8058b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
415
Assets/Live2D/Cubism/Framework/Motion/CubismMotionLayer.cs
Normal file
415
Assets/Live2D/Cubism/Framework/Motion/CubismMotionLayer.cs
Normal file
@@ -0,0 +1,415 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
using Live2D.Cubism.Framework.MotionFade;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.Playables;
|
||||
|
||||
namespace Live2D.Cubism.Framework.Motion
|
||||
{
|
||||
/// <summary>
|
||||
/// Cubism motion layer.
|
||||
/// </summary>
|
||||
public class CubismMotionLayer : ICubismFadeState
|
||||
{
|
||||
#region Action
|
||||
|
||||
/// <summary>
|
||||
/// Action animation end handler.
|
||||
/// </summary>
|
||||
public Action<int, int> AnimationBeginHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Action animation end handler.
|
||||
/// </summary>
|
||||
public Action<int, int> AnimationEndHandler;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variable
|
||||
|
||||
/// <summary>
|
||||
/// Playable output.
|
||||
/// </summary>
|
||||
public AnimationMixerPlayable PlayableOutput { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Playable output.
|
||||
/// </summary>
|
||||
private PlayableGraph _playableGraph;
|
||||
|
||||
/// <summary>
|
||||
/// Cubism playing motions.
|
||||
/// </summary>
|
||||
private List<CubismFadePlayingMotion> _playingMotions;
|
||||
|
||||
/// <summary>
|
||||
/// Cubism playing motions.
|
||||
/// </summary>
|
||||
private CubismMotionState _motionState;
|
||||
|
||||
/// <summary>
|
||||
/// List of cubism fade motion.
|
||||
/// </summary>
|
||||
private CubismFadeMotionList _cubismFadeMotionList;
|
||||
|
||||
/// <summary>
|
||||
/// Layer index.
|
||||
/// </summary>
|
||||
private int _layerIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Layer weight.
|
||||
/// </summary>
|
||||
private float _layerWeight;
|
||||
|
||||
/// <summary>
|
||||
/// Animation is finished.
|
||||
/// </summary>
|
||||
private bool _isFinished;
|
||||
|
||||
/// <summary>
|
||||
/// Is finished.
|
||||
/// </summary>
|
||||
/// <returns>True if the animation is finished, false otherwise.</returns>
|
||||
public bool IsFinished
|
||||
{
|
||||
get { return _isFinished; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fade State Interface
|
||||
|
||||
/// <summary>
|
||||
/// Get cubism playing motion list.
|
||||
/// </summary>
|
||||
/// <returns>Cubism playing motion list.</returns>
|
||||
public List<CubismFadePlayingMotion> GetPlayingMotions()
|
||||
{
|
||||
return _playingMotions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is default state.
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> State is default; <see langword="false"/> otherwise.</returns>
|
||||
public bool IsDefaultState()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get layer weight.
|
||||
/// </summary>
|
||||
/// <returns>Layer weight.</returns>
|
||||
public float GetLayerWeight()
|
||||
{
|
||||
return _layerWeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get state transition finished.
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> State transition is finished; <see langword="false"/> otherwise.</returns>
|
||||
public bool GetStateTransitionFinished()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set state transition finished.
|
||||
/// </summary>
|
||||
/// <param name="isFinished">State is finished.</param>
|
||||
public void SetStateTransitionFinished(bool isFinished) {}
|
||||
|
||||
/// <summary>
|
||||
/// Stop animation.
|
||||
/// </summary>
|
||||
/// <param name="index">Playing motion index.</param>
|
||||
public void StopAnimation(int index)
|
||||
{
|
||||
// Remove from playing motion list.
|
||||
_playingMotions.RemoveAt(index);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Stop animation.
|
||||
/// </summary>
|
||||
public void StopAnimationClip()
|
||||
{
|
||||
// Remove from motion state list.
|
||||
if (_motionState == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_playableGraph.Disconnect(_motionState.ClipMixer, 0);
|
||||
_motionState = null;
|
||||
|
||||
_isFinished = true;
|
||||
|
||||
|
||||
StopAllAnimation();
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Function
|
||||
|
||||
/// <summary>
|
||||
/// Initialize motion layer.
|
||||
/// </summary>
|
||||
/// <param name="playableGraph">.</param>
|
||||
/// <param name="fadeMotionList">.</param>
|
||||
/// <param name="layerWeight">.</param>
|
||||
public static CubismMotionLayer CreateCubismMotionLayer(PlayableGraph playableGraph, CubismFadeMotionList fadeMotionList, int layerIndex, float layerWeight = 1.0f)
|
||||
{
|
||||
var ret = new CubismMotionLayer();
|
||||
|
||||
ret._playableGraph = playableGraph;
|
||||
ret._cubismFadeMotionList = fadeMotionList;
|
||||
ret._layerIndex = layerIndex;
|
||||
ret._layerWeight = layerWeight;
|
||||
ret._isFinished = true;
|
||||
ret._motionState = null;
|
||||
ret._playingMotions = new List<CubismFadePlayingMotion>();
|
||||
ret.PlayableOutput = AnimationMixerPlayable.Create(playableGraph, 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create fade playing motion.
|
||||
/// </summary>
|
||||
/// <param name="clip">Animator clip.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
private CubismFadePlayingMotion CreateFadePlayingMotion(AnimationClip clip, bool isLooping, float speed = 1.0f)
|
||||
{
|
||||
var ret = new CubismFadePlayingMotion();
|
||||
|
||||
var isNotFound = true;
|
||||
var instanceId = -1;
|
||||
var events = clip.events;
|
||||
for(var i = 0; i < events.Length; ++i)
|
||||
{
|
||||
if(events[i].functionName != "InstanceId")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
instanceId = events[i].intParameter;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _cubismFadeMotionList.MotionInstanceIds.Length; i++)
|
||||
{
|
||||
if(_cubismFadeMotionList.MotionInstanceIds[i] != instanceId)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
isNotFound = false;
|
||||
|
||||
ret.Speed = speed;
|
||||
ret.StartTime = Time.time;
|
||||
ret.FadeInStartTime = Time.time;
|
||||
ret.Motion = _cubismFadeMotionList.CubismFadeMotionObjects[i];
|
||||
ret.EndTime = (ret.Motion.MotionLength <= 0)
|
||||
? -1
|
||||
: ret.StartTime + ret.Motion.MotionLength / speed;
|
||||
ret.IsLooping = isLooping;
|
||||
ret.Weight = 0.0f;
|
||||
ret.InstanceId = instanceId;
|
||||
ret.IsAnimationEndEventInvoked = false;
|
||||
AnimationBeginHandler(_layerIndex, instanceId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if(isNotFound)
|
||||
{
|
||||
Debug.LogError("CubismMotionController : Not found motion from CubismFadeMotionList.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play animation.
|
||||
/// </summary>
|
||||
/// <param name="clip">Animation clip.</param>
|
||||
/// <param name="isLoop">Animation is loop.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
public void PlayAnimation(AnimationClip clip, bool isLoop = true, float speed = 1.0f)
|
||||
{
|
||||
if (_motionState != null)
|
||||
{
|
||||
_playableGraph.Disconnect(_motionState.ClipMixer, 0);
|
||||
}
|
||||
|
||||
// Create cubism motion state.
|
||||
_motionState = CubismMotionState.CreateCubismMotionState(_playableGraph, clip, isLoop, speed);
|
||||
|
||||
|
||||
#if UNITY_2018_2_OR_NEWER
|
||||
PlayableOutput.DisconnectInput(0);
|
||||
#else
|
||||
PlayableOutput.GetGraph().Disconnect(PlayableOutput, 0);
|
||||
#endif
|
||||
PlayableOutput.ConnectInput(0, _motionState.ClipMixer, 0);
|
||||
PlayableOutput.SetInputWeight(0, 1.0f);
|
||||
|
||||
|
||||
// Set last motion end time and fade in start time;
|
||||
if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
|
||||
{
|
||||
var motion = _playingMotions[_playingMotions.Count - 1];
|
||||
|
||||
var time = Time.time;
|
||||
|
||||
var newEndTime = time + motion.Motion.FadeOutTime;
|
||||
|
||||
if (newEndTime < 0.0f || newEndTime < motion.EndTime)
|
||||
{
|
||||
motion.EndTime = newEndTime;
|
||||
}
|
||||
|
||||
|
||||
while (motion.IsLooping)
|
||||
{
|
||||
if ((motion.StartTime + motion.Motion.MotionLength) >= time)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
motion.StartTime += motion.Motion.MotionLength;
|
||||
}
|
||||
|
||||
motion.IsLooping = false;
|
||||
|
||||
_playingMotions[_playingMotions.Count - 1] = motion;
|
||||
}
|
||||
|
||||
// Create fade playing motion.
|
||||
var playingMotion = CreateFadePlayingMotion(clip, isLoop, speed);
|
||||
_playingMotions.Add(playingMotion);
|
||||
|
||||
_isFinished = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop all animation.
|
||||
/// </summary>
|
||||
public void StopAllAnimation()
|
||||
{
|
||||
for(var i = _playingMotions.Count - 1; i >= 0; --i)
|
||||
{
|
||||
StopAnimation(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set layer weight.
|
||||
/// </summary>
|
||||
/// <param name="weight">Layer weight.</param>
|
||||
public void SetLayerWeight(float weight)
|
||||
{
|
||||
_layerWeight = weight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set state speed.
|
||||
/// </summary>
|
||||
/// <param name="index">index of playing motion list.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
public void SetStateSpeed(int index, float speed)
|
||||
{
|
||||
// Fail silently...
|
||||
if(index < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var playingMotionData = _playingMotions[index];
|
||||
playingMotionData.Speed = speed;
|
||||
playingMotionData.EndTime = (playingMotionData.EndTime - Time.time) / speed;
|
||||
_playingMotions[index] = playingMotionData;
|
||||
|
||||
_motionState.ClipMixer.SetSpeed(speed);
|
||||
_motionState.ClipPlayable.SetDuration(_motionState.Clip.length / speed - 0.0001f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set state is loop.
|
||||
/// </summary>
|
||||
/// <param name="index">index of playing motion list.</param>
|
||||
/// <param name="isLoop">Animation is loop.</param>
|
||||
public void SetStateIsLoop(int index, bool isLoop)
|
||||
{
|
||||
// Fail silently...
|
||||
if(index < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(isLoop)
|
||||
{
|
||||
_motionState.ClipPlayable.SetDuration(double.MaxValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
_motionState.ClipPlayable.SetDuration(_motionState.Clip.length - 0.0001f);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var isFinished = true;
|
||||
for (var i = 0; i < _playingMotions.Count; i++)
|
||||
{
|
||||
var playingMotion = _playingMotions[i];
|
||||
if (playingMotion.IsLooping)
|
||||
{
|
||||
isFinished = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (playingMotion.IsAnimationEndEventInvoked)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Time.time > playingMotion.EndTime)
|
||||
{
|
||||
playingMotion.IsAnimationEndEventInvoked = true;
|
||||
_playingMotions[i] = playingMotion;
|
||||
if (playingMotion.InstanceId.HasValue)
|
||||
{
|
||||
var instanceId = _playingMotions[i].InstanceId.Value;
|
||||
AnimationEndHandler?.Invoke(_layerIndex, instanceId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
isFinished = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isFinished)
|
||||
{
|
||||
_isFinished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c5ca528c1aee3fa4483d0d38e70bea22
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
|
||||
namespace Live2D.Cubism.Framework.Motion
|
||||
{
|
||||
/// <summary>
|
||||
/// The constants of Motion priorities.
|
||||
/// </summary>
|
||||
public class CubismMotionPriority
|
||||
{
|
||||
public const int PriorityNone = 0;
|
||||
public const int PriorityIdle = 1;
|
||||
public const int PriorityNormal = 2;
|
||||
public const int PriorityForce = 3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d514d1507418814cb77640285fd2279
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
85
Assets/Live2D/Cubism/Framework/Motion/CubismMotionState.cs
Normal file
85
Assets/Live2D/Cubism/Framework/Motion/CubismMotionState.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.Playables;
|
||||
|
||||
namespace Live2D.Cubism.Framework.Motion
|
||||
{
|
||||
/// <summary>
|
||||
/// Cubism motion state.
|
||||
/// </summary>
|
||||
public class CubismMotionState
|
||||
{
|
||||
#region Variable
|
||||
|
||||
/// <summary>
|
||||
/// Cubism motion state clip.
|
||||
/// </summary>
|
||||
public AnimationClip Clip { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Animation clip mixer.
|
||||
/// </summary>
|
||||
public AnimationMixerPlayable ClipMixer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Animation clip playable.
|
||||
/// </summary>
|
||||
public AnimationClipPlayable ClipPlayable { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Create motion state.
|
||||
/// </summary>
|
||||
/// <param name="playableGraph">Playable graph.</param>
|
||||
/// <param name="clip">Animation clip.</param>
|
||||
/// <param name="isLoop">Animation is loop.</param>
|
||||
/// <param name="speed">Animation speed.</param>
|
||||
public static CubismMotionState CreateCubismMotionState(PlayableGraph playableGraph, AnimationClip clip, bool isLoop = true, float speed = 1.0f)
|
||||
{
|
||||
var ret = new CubismMotionState();
|
||||
|
||||
ret.Clip = clip;
|
||||
|
||||
// Create animation clip mixer.
|
||||
ret.ClipMixer = AnimationMixerPlayable.Create(playableGraph, 2);
|
||||
ret.ClipMixer.SetSpeed(speed);
|
||||
|
||||
// Connect AnimationClip Playable
|
||||
ret.ClipPlayable = AnimationClipPlayable.Create(playableGraph, ret.Clip);
|
||||
|
||||
if(!isLoop)
|
||||
{
|
||||
ret.ClipPlayable.SetDuration(clip.length - 0.0001f);
|
||||
}
|
||||
|
||||
ret.ClipMixer.ConnectInput(0, ret.ClipPlayable, 0);
|
||||
ret.ClipMixer.SetInputWeight(0, 1.0f);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connect motion state clip mixer.
|
||||
/// </summary>
|
||||
/// <param name="clipMixer">.</param>
|
||||
public void ConnectClipMixer(AnimationMixerPlayable clipMixer)
|
||||
{
|
||||
var lastInput = ClipMixer.GetInputCount() - 1;
|
||||
#if UNITY_2018_2_OR_NEWER
|
||||
ClipMixer.DisconnectInput(lastInput);
|
||||
#else
|
||||
ClipMixer.GetGraph().Disconnect(ClipMixer, lastInput);
|
||||
#endif
|
||||
ClipMixer.ConnectInput(lastInput, clipMixer, 0);
|
||||
ClipMixer.SetInputWeight(lastInput, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c492d171ce0338f49a648b2cb5d0dabb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user