void FixedUpdate() { //WrapMode emulator if (currentAnimationData.clip == null) { return; } if (currentAnimationData.secondsPlayed == currentAnimationData.length) { if (currentAnimationData.clip.wrapMode == WrapMode.Loop || currentAnimationData.clip.wrapMode == WrapMode.PingPong) { if (MecanimControl.OnAnimationLoop != null) { MecanimControl.OnAnimationLoop(currentAnimationData); } currentAnimationData.timesPlayed++; if (currentAnimationData.clip.wrapMode == WrapMode.Loop) { SetCurrentClipPosition(0); } if (currentAnimationData.clip.wrapMode == WrapMode.PingPong) { SetSpeed(currentAnimationData.clipName, -currentAnimationData.speed); SetCurrentClipPosition(0); } } else if (currentAnimationData.timesPlayed == 0) { if (MecanimControl.OnAnimationEnd != null) { MecanimControl.OnAnimationEnd(currentAnimationData); } currentAnimationData.timesPlayed = 1; if (currentAnimationData.clip.wrapMode == WrapMode.Once && alwaysPlay) { Play(defaultAnimation, currentMirror); } else if (!alwaysPlay) { animator.speed = 0; } } } else { //currentAnimationData.secondsPlayed += Time.deltaTime * animator.speed * Time.timeScale; currentTime = Time.time - startTime; currentAnimationData.secondsPlayed += (Time.fixedDeltaTime * animator.speed); if (currentAnimationData.secondsPlayed > currentAnimationData.length) { currentAnimationData.secondsPlayed = currentAnimationData.length; } } }
public void Copy(MecanimControl original) { ClearClips(); foreach (var pair in original.animations) { var data = pair.Value; AddClip(data.clip, data.clipName, data.speed, data.wrapMode); } Init(); }
void Start() { character = GameObject.FindGameObjectWithTag("Player"); CharacterInitialize(); rnd = new System.Random(); pitchShifter = Resources.Load <AudioMixerGroup>(Global.mixer); // Components added during runtime mecanimControl = character.AddComponent <MecanimControl>(); facialControl = character.AddComponent <ExpressionControl>(); mainOffsetCtrl = character.AddComponent <MainOffset>(); spineOffsetCtrl = character.AddComponent <SpineOffset>(); // Static method can be called doesn't have StartCoroutine() character.AddComponent <HandControl>(); character.AddComponent <FootControl>(); gameObject.AddComponent <SlideControl>(); gameObject.AddComponent <AudioControl>(); }
private void _playAnimation(AnimationData targetAnimationData, float blendingTime, float normalizedTime, bool mirror) { //The overrite machine. Creates an overrideController, replace its core animations and restate it back in if (targetAnimationData == null || targetAnimationData.clip == null) { return; } AnimatorOverrideController overrideController = new AnimatorOverrideController(); currentMirror = mirror; float newAnimatorSpeed = Mathf.Abs(targetAnimationData.speed); float currentNormalizedTime = GetCurrentClipPosition(); if (mirror) { if (targetAnimationData.speed > 0) { overrideController.runtimeAnimatorController = controller2; } else { overrideController.runtimeAnimatorController = controller4; } } else { if (targetAnimationData.speed > 0) { overrideController.runtimeAnimatorController = controller1; } else { overrideController.runtimeAnimatorController = controller3; } } overrideController["State1"] = currentAnimationData.clip; overrideController["State2"] = targetAnimationData.clip; if (blendingTime == -1) { blendingTime = currentAnimationData.transitionDuration; } if (blendingTime == -1) { blendingTime = defaultTransitionDuration; } if (blendingTime <= 0) { animator.runtimeAnimatorController = overrideController; animator.Play("State2", 0, normalizedTime); } else { animator.runtimeAnimatorController = overrideController; //animator.Play(state1Hash, 0, currentNormalizedTime); //currentAnimationData.secondsPlayed = currentNormalizedTime * currentAnimationData.length; //currentAnimationData.stateHash = state1Hash; currentAnimationData.stateName = "State1"; SetCurrentClipPosition(currentNormalizedTime); animator.Update(0); animator.CrossFade("State2", blendingTime / newAnimatorSpeed, 0, normalizedTime); } targetAnimationData.timesPlayed = 0; targetAnimationData.secondsPlayed = (normalizedTime * targetAnimationData.clip.length) / newAnimatorSpeed; targetAnimationData.length = targetAnimationData.clip.length; if (overrideRootMotion) { animator.applyRootMotion = targetAnimationData.applyRootMotion; } SetSpeed(targetAnimationData.speed); currentAnimationData = targetAnimationData; currentAnimationData.stateName = "State2"; //currentAnimationData.stateHash = state2Hash; if (MecanimControl.OnAnimationBegin != null) { MecanimControl.OnAnimationBegin(currentAnimationData); } }
void Start() { mecanimControl = gameObject.GetComponent <MecanimControl>(); }
public override void OnInspectorGUI() { EditorGUIUtility.labelWidth = 200f; MecanimControl mc = target as MecanimControl; GUILayout.Space(6f); EditorGUILayout.LabelField("Model Name", mc.modelName); mc.debugMode = EditorGUILayout.Toggle("Debug Mode", mc.debugMode); mc.alwaysPlay = EditorGUILayout.Toggle("Always Play", mc.alwaysPlay); mc.playOnStart = EditorGUILayout.Toggle("Play On Start", mc.playOnStart); mc.overrideRootMotion = EditorGUILayout.Toggle("Override Root Motion", mc.overrideRootMotion); mc.defaultTransitionDuration = EditorGUILayout.FloatField("Default Transition Duration", mc.defaultTransitionDuration); mc.defaultWrapMode = (WrapMode)EditorGUILayout.EnumPopup("Default Wrap Mode", mc.defaultWrapMode); if (DrawHeader("Default Animation", true)) { BeginContents(false); EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Clip", GUILayout.MinWidth(150), GUILayout.Height(50)); EditorGUILayout.LabelField("Name", GUILayout.MinWidth(150), GUILayout.Height(50)); EditorGUILayout.LabelField("Speed", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.LabelField("Transition\nDuration", GUILayout.Width(60), GUILayout.Height(50)); EditorGUILayout.LabelField("Wrap\nMode", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.LabelField("Apply\nRoot\nMotion", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); var clip = mc.defaultAnimationData.clip; clip = EditorGUILayout.ObjectField(clip, typeof(AnimationClip), false, GUILayout.MinWidth(150)) as AnimationClip; mc.defaultAnimationData.clipName = EditorGUILayout.TextField(mc.defaultAnimationData.clipName, GUILayout.MinWidth(150)); mc.defaultAnimationData.speed = EditorGUILayout.FloatField(mc.defaultAnimationData.speed, GUILayout.Width(50)); mc.defaultAnimationData.transitionDuration = EditorGUILayout.FloatField(mc.defaultAnimationData.transitionDuration, GUILayout.Width(50)); mc.defaultAnimationData.wrapMode = (WrapMode)EditorGUILayout.EnumPopup(mc.defaultAnimationData.wrapMode, GUILayout.Width(50)); mc.defaultAnimationData.applyRootMotion = EditorGUILayout.Toggle(mc.defaultAnimationData.applyRootMotion, GUILayout.Width(50)); if (clip != mc.defaultAnimationData.clip) { mc.defaultAnimationData.clip = clip; mc.defaultAnimationData.clipName = clip.name; mc.defaultAnimationData.speed = 1; mc.defaultAnimationData.transitionDuration = -1; mc.defaultAnimationData.wrapMode = clip.wrapMode; mc.defaultAnimationData.applyRootMotion = false; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); EndContents(); } if (DrawHeader("Animations", true)) { BeginContents(false); EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Clip", GUILayout.MinWidth(150), GUILayout.Height(50)); EditorGUILayout.LabelField("Name", GUILayout.MinWidth(150), GUILayout.Height(50)); EditorGUILayout.LabelField("Speed", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.LabelField("Transition\nDuration", GUILayout.Width(60), GUILayout.Height(50)); EditorGUILayout.LabelField("Wrap\nMode", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.LabelField("Apply\nRoot\nMotion", GUILayout.Width(50), GUILayout.Height(50)); EditorGUILayout.EndHorizontal(); EditorGUI.BeginDisabledGroup(true); foreach (var pairs in mc.animations) { var animData = pairs.Value; EditorGUILayout.BeginHorizontal(); var clip = animData.clip; clip = EditorGUILayout.ObjectField(clip, typeof(AnimationClip), false, GUILayout.MinWidth(150)) as AnimationClip; animData.clipName = EditorGUILayout.TextField(animData.clipName, GUILayout.MinWidth(150)); animData.speed = EditorGUILayout.FloatField(animData.speed, GUILayout.Width(50)); animData.transitionDuration = EditorGUILayout.FloatField(animData.transitionDuration, GUILayout.Width(50)); animData.wrapMode = (WrapMode)EditorGUILayout.EnumPopup(animData.wrapMode, GUILayout.Width(50)); animData.applyRootMotion = EditorGUILayout.Toggle(animData.applyRootMotion, GUILayout.Width(50)); EditorGUILayout.EndHorizontal(); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); EndContents(); } }
private void _playAnimation(AnimationData targetAnimationData, float blendingTime, float normalizedTime, bool mirror) { //The overrite machine. Creates an overrideController, replace its core animations and restate it back in ////("cyan", "_playAnimation:"+targetAnimationData.clipName); if (targetAnimationData == null || targetAnimationData.clip == null) { return; } if (currentAnimationData == null) { Debug.LogError("_playAnimation :currentAnimationData == null!"); return; } if (currentAnimationData.clipName != targetAnimationData.clipName) { SetOverrideController(currentAnimationData, targetAnimationData, mirror); } float newAnimatorSpeed = Mathf.Abs(targetAnimationData.speed) * speed; float currentNormalizedTime = GetNormalizedTime(); if (blendingTime == -1) { blendingTime = currentAnimationData.transitionDuration; } if (blendingTime == -1) { blendingTime = defaultTransitionDuration; } currentBlendingTime = blendingTime; if (blendingTime <= 0 || currentAnimationData.clip == targetAnimationData.clip) { //Util.LogColor("red", modelName + "play:" + targetAnimationData.clipName); //animator.Rebind(); AnimatorPlay("State2", 0, normalizedTime); } else { //Util.LogColor("cyan", modelName + "crossfade:" + targetAnimationData.clipName+ "blendingTime:"+ blendingTime); currentAnimationData.stateName = "State1"; SetCurrentClipPosition(currentNormalizedTime); animator.Update(0); animator.CrossFade("State2", blendingTime / newAnimatorSpeed); } targetAnimationData.timesPlayed = 0; targetAnimationData.secondsPlayed = (normalizedTime * targetAnimationData.clip.length) / newAnimatorSpeed; targetAnimationData.length = targetAnimationData.clip.length; if (overrideRootMotion) { animator.applyRootMotion = targetAnimationData.applyRootMotion; } SetSpeed(targetAnimationData.speed); currentAnimationData = targetAnimationData; currentAnimationData.stateName = "State2"; //currentAnimationData.stateHash = state2Hash; if (MecanimControl.OnAnimationBegin != null) { MecanimControl.OnAnimationBegin(currentAnimationData); } }
private void _playAnimation(MecanimAnimationData targetAnimationData, float blendingTime, float normalizedTime, bool mirror) { //The overrite machine. Creates an overrideController, replace its core animations and restate it back in if (targetAnimationData == null || targetAnimationData.clip == null) { return; } bool prevMirror = currentMirror; currentMirror = mirror; float animSpeed = targetAnimationData.originalSpeed * (targetAnimationData.originalSpeed < 0 ? -1 : 1); currentNormalizedTime = GetCurrentClipPosition(); currentState = "State1"; if (!mirror) { if (targetAnimationData.originalSpeed >= 0) { currentState = "State1"; } else { currentState = "State2"; } } else { if (targetAnimationData.originalSpeed >= 0) { currentState = "State3"; } else { currentState = "State4"; } } overrideController = new AnimatorOverrideController(); overrideController.runtimeAnimatorController = controller; if (currentAnimationData != null && currentAnimationData.clip != null) { overrideController["Default"] = currentAnimationData.clip; } overrideController[currentState] = targetAnimationData.clip; if (blendingTime == -1) { blendingTime = currentAnimationData.transitionDuration; } if (blendingTime == -1) { blendingTime = defaultTransitionDuration; } if (blendingTime <= 0 || currentAnimationData == null) { animator.runtimeAnimatorController = overrideController; animator.Play(currentState, 0, normalizedTime); } else { animator.runtimeAnimatorController = overrideController; currentAnimationData.stateName = "Default"; SetCurrentClipPosition(currentNormalizedTime); animator.Play("Default", 0, normalizedTime); animator.CrossFade(currentState, (blendingTime / animSpeed), 0, normalizedTime); } // Update Previous Mirror AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0); if (info.IsName("Default")) { if (animator.GetBool("Mirror") != prevMirror) { animator.SetBool("Mirror", prevMirror); } } animator.Update(0); targetAnimationData.timesPlayed = 0; targetAnimationData.secondsPlayed = (normalizedTime * targetAnimationData.length) / animSpeed; targetAnimationData.normalizedTime = normalizedTime; targetAnimationData.speed = targetAnimationData.originalSpeed; if (overrideRootMotion) { animator.applyRootMotion = targetAnimationData.applyRootMotion; } SetSpeed(targetAnimationData.originalSpeed); if (currentAnimationData != null) { currentAnimationData.speed = currentAnimationData.originalSpeed; currentAnimationData.normalizedSpeed = 1; currentAnimationData.timesPlayed = 0; } currentAnimationData = targetAnimationData; currentAnimationData.stateName = currentState; if (MecanimControl.OnAnimationBegin != null) { MecanimControl.OnAnimationBegin(currentAnimationData); } }
public void DoFixedUpdate() { //WrapMode emulator if (overrideAnimatorUpdate) { animator.enabled = false; animator.Update(Time.fixedDeltaTime); } if (currentAnimationData == null || currentAnimationData.clip == null) { return; } currentAnimationData.secondsPlayed += (Time.fixedDeltaTime * GetSpeed()); if (currentAnimationData.secondsPlayed > currentAnimationData.length) { currentAnimationData.secondsPlayed = currentAnimationData.length; } currentAnimationData.normalizedTime = currentAnimationData.secondsPlayed / currentAnimationData.length; if (currentAnimationData.secondsPlayed == currentAnimationData.length) { if (currentAnimationData.clip.wrapMode == WrapMode.Loop || currentAnimationData.clip.wrapMode == WrapMode.PingPong) { if (MecanimControl.OnAnimationLoop != null) { MecanimControl.OnAnimationLoop(currentAnimationData); } currentAnimationData.timesPlayed++; if (currentAnimationData.clip.wrapMode == WrapMode.Loop) { SetCurrentClipPosition(0); } if (currentAnimationData.clip.wrapMode == WrapMode.PingPong) { SetSpeed(currentAnimationData.clipName, -currentAnimationData.speed); SetCurrentClipPosition(0); } } else if (currentAnimationData.timesPlayed == 0) { if (MecanimControl.OnAnimationEnd != null) { MecanimControl.OnAnimationEnd(currentAnimationData); } currentAnimationData.timesPlayed = 1; if ((currentAnimationData.clip.wrapMode == WrapMode.Once || currentAnimationData.clip.wrapMode == WrapMode.Clamp) && alwaysPlay) { Play(defaultAnimation, currentMirror); } else if (!alwaysPlay) { SetSpeed(0); } } } }
//The overrite machine. Creates an overrideController, replace its core animations and restate it back in private void _playAnimation(AnimationData targetAnimationData, float blendingTime, float normalizedTime, bool mirror) { if (targetAnimationData == null || targetAnimationData.clip == null) { return; } AnimatorOverrideController overrideController = new AnimatorOverrideController(); float newAnimatorSpeed = Mathf.Abs(targetAnimationData.originalSpeed); float currentNormalizedTime = GetCurrentClipPosition(); currentMirror = mirror; if (mirror) { if (targetAnimationData.originalSpeed >= 0) { overrideController.runtimeAnimatorController = controller2; } else { overrideController.runtimeAnimatorController = controller4; } } else { if (targetAnimationData.originalSpeed >= 0) { overrideController.runtimeAnimatorController = controller1; } else { overrideController.runtimeAnimatorController = controller3; } } if (currentAnimationData != null) { overrideController["State1"] = currentAnimationData.clip; } overrideController["State2"] = targetAnimationData.clip; if (blendingTime == -1) { blendingTime = currentAnimationData.transitionDuration; } if (blendingTime == -1) { blendingTime = defaultTransitionDuration; } if (blendingTime <= 0 || currentAnimationData == null) { animator.runtimeAnimatorController = overrideController; animator.Play("State2", 0, normalizedTime); } else { animator.runtimeAnimatorController = overrideController; currentAnimationData.stateName = "State1"; SetCurrentClipPosition(currentNormalizedTime); animator.Update(0); animator.CrossFade("State2", blendingTime / newAnimatorSpeed, 0, normalizedTime); } targetAnimationData.secondsPlayed = (normalizedTime * targetAnimationData.clip.length) / newAnimatorSpeed; targetAnimationData.length = targetAnimationData.clip.length; targetAnimationData.normalizedTime = normalizedTime; if (overrideRootMotion) { animator.applyRootMotion = targetAnimationData.applyRootMotion; } SetSpeed(targetAnimationData.originalSpeed); if (currentAnimationData != null) { currentAnimationData.speed = currentAnimationData.originalSpeed; currentAnimationData.normalizedSpeed = 1; currentAnimationData.timesPlayed = 0; } currentAnimationData = targetAnimationData; currentAnimationData.stateName = "State2"; if (MecanimControl.OnAnimationBegin != null) { MecanimControl.OnAnimationBegin(currentAnimationData); } }