/// <summary> /// Called by Unity. /// </summary> /// <param name="animator">Animator.</param> /// <param name="stateInfo">Animator state info.</param> /// <param name="layerIndex">Index of the layer.</param> /// <param name="controller">Animation controller playable.</param> public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller) { var fadeController = animator.gameObject.GetComponent <CubismFadeController>(); // Fail silently... if (fadeController == null) { return; } _cubismFadeMotionList = fadeController.CubismFadeMotionList; _layerIndex = layerIndex; _layerWeight = (_layerIndex == 0) ? 1.0f : animator.GetLayerWeight(_layerIndex); var animatorClipInfo = controller.GetNextAnimatorClipInfo(layerIndex); _isDefaulState = (animatorClipInfo.Length == 0); if (_isDefaulState) { // Get the motion of Default State only for the first time. animatorClipInfo = controller.GetCurrentAnimatorClipInfo(layerIndex); } // Set playing motions end time. for (var i = 0; i < _playingMotions.Count; ++i) { var motion = _playingMotions[i]; if (motion.Motion == null) { continue; } var newEndTime = Time.time + motion.Motion.FadeOutTime; motion.EndTime = newEndTime; _playingMotions[i] = motion; } for (var i = 0; i < animatorClipInfo.Length; ++i) { CubismFadePlayingMotion playingMotion; var instanceId = -1; var events = animatorClipInfo[i].clip.events; for (var k = 0; k < events.Length; ++k) { if (events[k].functionName != "InstanceId") { continue; } instanceId = events[k].intParameter; break; } var motionIndex = -1; for (var j = 0; j < _cubismFadeMotionList.MotionInstanceIds.Length; ++j) { if (_cubismFadeMotionList.MotionInstanceIds[j] != instanceId) { continue; } motionIndex = j; break; } playingMotion.Motion = (motionIndex == -1) ? null : _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex]; playingMotion.Speed = 1.0f; playingMotion.StartTime = Time.time; playingMotion.FadeInStartTime = Time.time; playingMotion.EndTime = (playingMotion.Motion.MotionLength <= 0) ? -1 : playingMotion.StartTime + playingMotion.Motion.MotionLength; _playingMotions.Add(playingMotion); } }
/// <summary> /// /// </summary> /// <param name="animator"></param> /// <param name="stateInfo"></param> /// <param name="layerIndex"></param> /// <param name="controller"></param> public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller) { var fadeController = animator.gameObject.GetComponent <CubismFadeController>(); // Fail silently... if (fadeController == null) { return; } _cubismFadeMotionList = fadeController.CubismFadeMotionList; LayerIndex = layerIndex; LayerWeight = (LayerIndex == 0) ? 1.0f : animator.GetLayerWeight(LayerIndex); var animatorClipInfo = controller.GetNextAnimatorClipInfo(layerIndex); IsDefaultState = (animatorClipInfo.Length == 0); if (IsDefaultState) { // 初回のみDefault Stateのモーションを取得する animatorClipInfo = controller.GetCurrentAnimatorClipInfo(layerIndex); } // Set playing motions end time. for (var i = 0; i < PlayingMotions.Count; ++i) { var motion = PlayingMotions[i]; if (motion.Motion == null) { continue; } var newEndTime = Time.time + motion.Motion.FadeOutTime; if (motion.EndTime < 0.0f || newEndTime < motion.EndTime) { motion.EndTime = newEndTime; } } for (var i = 0; i < animatorClipInfo.Length; ++i) { CubismFadePlayingMotion playingMotion; var motionIndex = -1; for (var j = 0; j < _cubismFadeMotionList.MotionInstanceIds.Length; ++j) { if (_cubismFadeMotionList.MotionInstanceIds[j] != animatorClipInfo[i].clip.GetInstanceID()) { continue; } motionIndex = j; break; } playingMotion.Motion = (motionIndex == -1) ? null : _cubismFadeMotionList.CubismFadeMotionObjects[motionIndex]; playingMotion.StartTime = Time.time; playingMotion.FadeInStartTime = Time.time; playingMotion.EndTime = (playingMotion.Motion.MotionLength <= 0) ? -1 : playingMotion.StartTime + playingMotion.Motion.MotionLength + playingMotion.Motion.FadeOutTime; PlayingMotions.Add(playingMotion); } }