private void Bake() { if (m_HasBake) { return; } if (Application.isPlaying || effectAnim == null) { return; } const float frameRate = 30.0f; int frameCount = ((clip.Length * (int)frameRate) + 2); effectAnim.Rebind(); effectAnim.StopPlayback(); effectAnim.recorderStartTime = 0.0f; effectAnim.StartRecording(frameCount); for (int i = 0; i < frameCount - 1; i++) { effectAnim.Update(1.0f / frameRate); } effectAnim.StopRecording(); effectAnim.StartPlayback(); m_HasBake = true; m_RecorderStopTime = effectAnim.recorderStopTime; }
public void Sample() { AnimatorOverrideController aoc = new AnimatorOverrideController(Rac); List <KeyValuePair <AnimationClip, AnimationClip> > overrides = new List <KeyValuePair <AnimationClip, AnimationClip> >(); List <KeyValuePair <AnimationClip, AnimationClip> > overrides1 = new List <KeyValuePair <AnimationClip, AnimationClip> >(); aoc.GetOverrides(overrides); foreach (KeyValuePair <AnimationClip, AnimationClip> pair in overrides) { overrides1.Add(new KeyValuePair <AnimationClip, AnimationClip>(pair.Key, AniClip)); } aoc.ApplyOverrides(overrides1); Ani.runtimeAnimatorController = aoc; Ani.Rebind(); Ani.StopPlayback(); Ani.recorderStartTime = 0; int frameCount = (int)(AniClip.frameRate * AniClip.length); Ani.StartRecording(frameCount); for (int i = 0; i < frameCount; i++) { Ani.Update(1.0f / frameCount); } Ani.StopRecording(); Ani.playbackTime = 0; Ani.StartPlayback(); }
/// <summary> /// 击飞 /// </summary> /// <param name="floatType"></param> public void BlowupAnim(int floatType) { if (animator.GetBool("ToppleState")) { PlayToppleback(); return; } if (animator.GetBool("Blow")) { PlayFloatback(); return; } if (floatType < 1) { return; } if (!animator.GetBool("InjuredState")) { animator.SetBool("InjuredState", true); } string floatName = UtilTools.StringBuilder("float", floatType); animator.Play(floatName, -1, 0.0f); animator.Update(0.0f); }
/// 播放动画 public void PlayAnimator(string stateName) { if (_animator == null) { _animator = this.gameObject.GetComponent <Animator>(); } if (_animator == null) { return; } if (!_animator.enabled) { _animator.enabled = true; } _animator.Play(stateName, 0, 0f); CurrentAnimatorStateName = stateName; //为了正确调用[animator.GetCurrentAnimatorStateInfo(0)],必须要两次Update _animator.Update(0); _animator.Update(0); //记录normalizedTime _currentAnimatorStateCounter = (int)_animator.GetCurrentAnimatorStateInfo(0).normalizedTime; //派发事件 DispatchAnimatorEvent(UIAnimatorEventType.AnimatorStart, CurrentAnimatorStateName); }
/// Plays the specified clip public void Play(AnimationClip anim, float speed = 1, float time = 0, bool force = false) { if (anim == null) return; if (m_animator.enabled == false) m_animator.enabled = true; m_speed = Mathf.Max(0, speed); m_animator.speed = m_speed; if (force == false && IsPlaying(anim)) return; // Reset animation nodes so any curves are if (m_nodes != null) m_nodes.Reset(); #if UNITY_5_6_OR_NEWER m_clipPairList[0] = new KeyValuePair<AnimationClip, AnimationClip>(m_clipPairList[0].Key, anim); m_controller.ApplyOverrides(m_clipPairList); #else m_clipPairArray[0].overrideClip = anim; m_controller.clips = m_clipPairArray; #endif m_animator.Update(0.0f); // Update so that new clip state is reset before hitting play m_animator.Play(STATE_NAME, 0, time); // m_speed = Mathf.Max(0,speed); // m_animator.speed = m_speed; m_currAnim = anim; m_animator.Update(0.0f); // Update so that normalized time is updated immediately }
/// Plays the specified clip public void Play(AnimationClip anim, float speed = 1) { if (anim == null) { return; } if (m_animator.enabled == false) { m_animator.enabled = true; } // Reset animation nodes if (m_nodes != null) { m_nodes.Reset(); } #if UNITY_5_6_OR_NEWER m_clipPairList[0] = new KeyValuePair <AnimationClip, AnimationClip>(m_clipPairList[0].Key, anim); m_controller.ApplyOverrides(m_clipPairList); #else m_clipPairArray[0].overrideClip = anim; m_controller.clips = m_clipPairArray; #endif m_animator.Update(0.0f); // Update so that new clip state is reset before hitting play m_animator.Play(STATE_NAME_HASH, 0, 0); m_speed = Mathf.Max(0, speed); m_animator.speed = m_speed; m_currAnim = anim; m_animator.Update(0.0f); // Update so that normalized time is updated immediately }
//void UpdateAnimationASD() //{ // GameObject t_hips = GameObject.FindGameObjectWithTag("Bicubic"); // Transform[] t_bones = t_hips.GetComponentsInChildren<Transform>(); // //print(SQUAD.Spline(poses[0][1], poses[1][1], poses[2][1], poses[3][1], 0.0f).eulerAngles); // //print(SQUAD.Spline(poses[0][1], poses[1][1], poses[2][1], poses[3][1], 1.0f).eulerAngles); // // for (int i = 0; i < t_bones.Length; i++) // { // if (i == 2 && m_transition > 0.5f && m_prevTrans < 0.5f) // { // //print(t_bones[i].rotation.eulerAngles); // //testStruct d = new testStruct(); // SQUAD.testStruct d = new SQUAD.testStruct(); // d = SQUAD.Spline(poses[0][i], poses[1][i], poses[2][i], poses[3][i], m_prevTrans); // //print(d.alongLine); // d = SQUAD.Spline(poses[0][i], poses[1][i], poses[2][i], poses[3][i], m_transition); // t_bones[i].rotation = d.quat; // //print(d.alongLine); // } // else // { // t_bones[i].rotation = SQUAD.Spline(poses[0][i], poses[1][i], poses[2][i], poses[3][i], m_transition).quat; // } // } // if (!headBob) // { // if (m_transition < 0.25f) // { // t_bones[0].position = Vector3.Slerp(hipspos[0], hipspos[1], // m_transition / 0.25f); // } // // else if (m_transition < 0.5f) // { // t_bones[0].position = Vector3.Slerp(hipspos[1], hipspos[2], // (m_transition - 0.25f) / 0.25f); // //print(poses[1][1].name + poses[1][1].transform.rotation.eulerAngles); // } // else if (m_transition < 0.75f) // { // t_bones[0].position = Vector3.Slerp(hipspos[2], hipspos[3], // (m_transition - 0.5f) / 0.25f); // } // else if (m_transition < 1.0f) // { // t_bones[0].position = Vector3.Slerp(hipspos[3], hipspos[0], // (m_transition - 0.75f) / 0.25f); // } // } //} void SaveKeyFramesWalkFWD0() { GameObject t_hybrid1 = gameObject; //if (m_flip) //{ // t_hybrid1.transform.position = new Vector3(1, 0, 0); //} //else //{ // t_hybrid1.transform.position = new Vector3(-1, 0, 0); //} List <Transform> t_transforms = new List <Transform>(); m_animator.Play(m_extendHash, 0, 0); m_animator.Update(0.001f); m_animator.Stop(); Transform t_hips = transform.Find("Reference").Find("Hips"); t_hips.GetComponentsInChildren <Transform>(t_transforms); List <Quaternion> t_quaternions = new List <Quaternion>(); for (int i = 0; i < t_transforms.Count; i++) { t_quaternions.Add(new Quaternion(t_transforms[i].rotation.x, t_transforms[i].rotation.y, t_transforms[i].rotation.z, t_transforms[i].rotation.w)); } poses.Add(t_quaternions); hipspos.Add(new Vector3(t_hips.transform.position.x, t_hips.transform.position.y, t_hips.transform.position.z)); }
public void AttackDraw(MapBoard _mcBoard) { if (this.IsAttack) { this.IsAttack = false; animator.SetTrigger("AttackTrigger"); // animator.SetInteger("animation",11); animator.Update(0); stateInfo = this.animator.GetCurrentAnimatorStateInfo(0); StartCoroutine(DelayDamageDraw(stateInfo.length, _mcBoard)); if (this.IsDamage) { Invoke("ViewDamage", stateInfo.length / 2f); } } else if (this.IsDamage) { StartCoroutine(DelayDamage(0.25f, _mcBoard)); Invoke("ViewDamage", 0.3f); } else { gameMaster.GetComponent <GameMaster>().SetCountDownBool(); } // 終了 }
public AnimationEvent AddAnimationEvent <T>(string functionName, float time) where T : MonoBehaviour { AnimationEvent evt = null; if (animator != null) { GetARequiredComponent <T>(gameObject); evt = new AnimationEvent(); //更新动画,以便可以读取正确的info animator.Update(0); AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0); if (info.length > 0) { AddAnEvent(evt, functionName, time); } else { StartCoroutine(AddAnEventRoutine(evt, functionName, time)); } } return(evt); }
public void Bake() { if (m_HasBake) { return; } if (Application.isPlaying || animatior == null) { return; } int frameCount = Mathf.RoundToInt((clipInfo[0].clip.length * clipInfo[0].clip.frameRate)); animatior.Rebind(); animatior.StopPlayback(); animatior.recorderStartTime = 0.0f; animatior.StartRecording(frameCount); for (int i = 0; i < frameCount - 1; i++) { animatior.Update(1.0f / clipInfo[0].clip.frameRate); } animatior.StopRecording(); animatior.StartPlayback(); m_HasBake = true; m_RecorderStopTime = animatior.recorderStopTime; }
// Update is called once per frame void Update() { if (animator && animator.enabled) { animator.enabled = false; } float dt = Time.deltaTime; float beginTime = Time.time - dt; while (true) { if (dt > timeStep) { dt -= timeStep; animator.Update(timeStep); if (_onStepUpdate != null) { _onStepUpdate(beginTime + timeStep); } } else { animator.Update(dt); if (_onStepUpdate != null) { _onStepUpdate(beginTime + dt); } break; } } }
public void PlayRestartFrame(float frameIndex) { _animator.gameObject.SetActive(false); _animator.gameObject.SetActive(true); //更新动画到指定帧 //_animator.Play(AnimationStateHash,la); _animator.Update(frameIndex); }
/// <summary> /// フェードアニメーションをセット /// </summary> private void setFadeAnim(float time) { if (_FadeAnimator != null) { _FadeAnimator.Play(FadeAnim[(int)_FadeType]); _FadeAnimator.Update(0f); _FadeAnimator.speed = time == 0f ? 1f : 1f / time; } }
// Update is called once per frame void Update() { if (posse.Count > 0) { posseAnchor.Rotate(new Vector3(0, 0, 1), -4 * Time.deltaTime); foreach (GameObject go in posse) { go.transform.Rotate(new Vector3(0, 0, 1), 4 * Time.deltaTime); } } if (Input.GetKeyDown(KeyCode.Space)) { kill(); } if (Input.GetKeyDown(KeyCode.R)) { charAnimator.SetBool("IsDead", false); charAnimator.Update(0); } if (Input.GetKeyDown(KeyCode.E)) { charAnimator.SetTrigger("Attack"); } Vector3 move_vector = new Vector3(); bool isDead = charAnimator.GetBool("IsDead"); if (Input.GetKey(KeyCode.A) && !isDead) { charKing.flipX = true; move_vector.x -= moveSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.D) && !isDead) { charKing.flipX = false; move_vector.x += moveSpeed * Time.deltaTime; } if (Input.GetKey(KeyCode.W) && !isDead) { move_vector.y += moveSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.S) && !isDead) { move_vector.y -= moveSpeed * Time.deltaTime; } if (move_vector.magnitude > 0) { charAnimator.SetBool("isWalking", true); transform.position += move_vector; } else { charAnimator.SetBool("isWalking", false); } }
protected override void _ResetImpl() { if (animator == null) { return; } animator.Play("", 0, 0f); animator.Update(0); }
public void Play(string name, string endName, Action action) { GetAnimator(); isPlaying = true; _animName = name; _endName = endName; _action = action; _animator.Play(name, 0, 0f); _animator.Update(0); }
void OnControllerColliderHit(ControllerColliderHit hit) { if (hit.gameObject.tag == "Obstacle") { isFail = true; isFailRun = true; SetState(State.Fail); animator.Update(0); } }
public override void Evaluate(float time) { base.Evaluate(time); if (_Animator != null && (!Application.isPlaying && Preview)) { if (DeltaTime != 0) { _Animator.Update(DeltaTime); } } }
void OnEnable() { if (anim != null) { Random.InitState(seed); anim.updateMode = AnimatorUpdateMode.UnscaledTime; anim.Play("anim" + Random.Range(0, 2), 0, Random.Range(0.0f, 1.0f)); anim.speed = 0f; anim.Update(Time.deltaTime); } }
public void Play() { animator.enabled = true; isDone = false; clipPairList[0] = new KeyValuePair <AnimationClip, AnimationClip>(clipPairList[0].Key, anim); overrideController.ApplyOverrides(clipPairList); animator.Update(0.0f); animator.Play(sharedController.StateName, 0, 0f); animator.speed = 1f; animator.Update(0.0f); }
/// <summary> /// Rewind animation to a certain normalized time /// </summary> /// <param name="normalizedTime"></param> /// <param name="movementDirection"></param> /// <param name="camForward"></param> public void RewindAnimation(float normalizedTime, Vector3 movementDirection, Vector3 camForward) { tempNormalizedTime = GetNormalizedTime(); Vector2 animationDir = GetAnimationDirection(movementDirection, camForward); animator.SetFloat("xVelocity", animationDir.x); animator.SetFloat("zVelocity", animationDir.y); animator.Update(0f); animator.Play(0, -1, normalizedTime); animator.Update(0f); }
private void Update() { if (_autoUpdate) { if (_eventAnimator) { _eventAnimator.Update(Time.deltaTime); } Render(Time.deltaTime); } }
private void OnValidate() { if (anim == null) { anim = GetComponent <Animator>(); } if (gameObject.activeInHierarchy) { anim.Update(Time.deltaTime); } }
private void InitAnimation() { if (animator == null) { return; } string animName = actionMachine.GetAnimName(); animator.Play(animName, 0, 0); animator.Update(0); }
public void OnGUI(Rect r, GUIStyle background) { if (Event.current.type == EventType.Repaint) { uTimeControl.Update(); #if !UNITY_2017_3_OR_NEWER if (mode2D) { PreviewDir = Vector2.zero; } #endif { var clip = dg_get_m_SourcePreviewMotion(instance); uTimeControl.loop = true; uTimeControl.startTime = 0f; uTimeControl.stopTime = clip.length; dg_set_fps(instance, (int)clip.frameRate); if (!clip.legacy && animator != null && animator.runtimeAnimatorController != null) { dg_set_ShowIKOnFeetButton(animator.isHuman && clip.isHumanMotion); AnimationClipSettings animationClipSettings = AnimationUtility.GetAnimationClipSettings(clip); if (m_State != null) { m_State.iKOnFeet = dg_get_ShowIKOnFeetButton() && dg_get_IKOnFeet(); } var normalizedTime = animationClipSettings.stopTime - animationClipSettings.startTime == 0.0 ? 0.0f : (float)((uTimeControl.currentTime - animationClipSettings.startTime) / (animationClipSettings.stopTime - animationClipSettings.startTime)); animator.Play(0, 0, normalizedTime); animator.Update(uTimeControl.deltaTime); } else if (animation != null) { dg_set_ShowIKOnFeetButton(false); clip.SampleAnimation(gameObject, uTimeControl.currentTime); } } } dg_DoAvatarPreview(r, background); if (animator.applyRootMotion && transformPoseSave != null) { var rect = r; rect.yMin = rect.yMax - 40f; rect.yMax -= 15f; var invRot = Quaternion.Inverse(transformPoseSave.originalRotation); var pos = invRot * (gameObject.transform.position - transformPoseSave.originalPosition); var rot = (invRot * gameObject.transform.rotation).eulerAngles; EditorGUI.DropShadowLabel(rect, string.Format("Root Motion Position {0}\nRoot Motion Rotation {1}", pos, rot)); } }
private void CaptureBoneSnapShot(AnimationClip animClip, MotionFrameData motionFrameData, MotionFrameData lastMotionFrameData, GameObject sampleGO, float bakeFrames, float CurrentFrame) { float frameSkipsTimeStep = frameSkips[animClip.name] / (float)fps; float bakeDelta = CurrentFrame / bakeFrames; float animationTime = bakeDelta * animClip.length; if (requiresAnimator) { float normalizedTime = animationTime / animClip.length; animator.Play(animClip.name, 0, normalizedTime); animator.Update(frameSkipsTimeStep); } else { GameObject sampleObject = sampleGO; Animation legacyAnimation = sampleObject.GetComponentInChildren <Animation>(); if (animator && animator.gameObject != sampleObject) { sampleObject = animator.gameObject; } else if (legacyAnimation && legacyAnimation.gameObject != sampleObject) { sampleObject = legacyAnimation.gameObject; } animClip.SampleAnimation(sampleObject, animationTime); } int index = 0; foreach (string boneName in bonesMap.Keys) { int boneIndex = bonesMap[boneName]; Transform child = joints[boneIndex]; MotionBoneData motionBoneData = motionFrameData.motionBoneDataList[index]; motionBoneData.position = child.position; motionBoneData.localPosition = child.localPosition; motionBoneData.rotation = child.rotation; motionBoneData.localRotation = child.localRotation; motionBoneData.velocity = Vector3.zero; motionBoneData.boneName = child.name; motionBoneData.boneIndex = boneIndex; //calc velocity if (lastMotionFrameData != null) { MotionBoneData lastMotionBoneData = lastMotionFrameData.motionBoneDataList[index]; lastMotionBoneData.velocity = (motionBoneData.localPosition - lastMotionBoneData.localPosition) / frameSkipsTimeStep; } index++; } }
void Start() { if (animator == null) { return; } _currentSceneIndex = SceneManager.GetActiveScene().buildIndex; animator.Update(0.01f); UpdateClipLabel("Pose: ", poseLabel, "BaseLayer"); UpdateClipLabel("Face: ", faceLabel, "FaceLayer"); UpdateSceneLabel(); }
public override void Trigger(GameObject actor) { Animator animator = actor.GetComponentInChildren <Animator>(true); if (animator == null) { return; } RuntimeAnimatorController ac = animator.runtimeAnimatorController; int frameCount = 0; for (int i = 0; i < ac.animationClips.Length; i++) { AnimationClip clip = ac.animationClips[i]; if (clip.name == StateName) { animationClip = clip; frameCount = (int)(clip.length * clip.frameRate); break; } } if (animationClip == null) { Debug.LogError("Cant find Animation ! Animation name is " + (string.IsNullOrEmpty(StateName) ? "Null" : StateName) + " in " + animator.gameObject.name); return; } actor.SetActive(true); animator.Play(StateName, Layer, Normalizedtime); animator.Update(0); if (!Application.isPlaying) { animator.Rebind(); animator.StopPlayback(); animator.recorderStartTime = 0; animator.StartRecording(frameCount); const float frameRate = 1.0f / 30; for (int i = 0; i < frameCount; i++) { animator.Update(frameRate); //¼Ç¼ÿһ֡ } animator.StopRecording(); Debug.LogFormat("Recording:{0},{1}", animator.recorderStartTime, animator.recorderStopTime); animator.StartPlayback(); } }
/// <summary> /// 动画状态机切动画特殊处理(为了短时间内切动画失败问题) /// </summary> /// <param name="animator"></param> /// <param name="name"></param> /// <param name="transitionDuration"></param> /// <param name="layer"></param> /// <param name="normalizedTime"></param> public static void CCSetInteger(this Animator animator, string name, int transitionDuration, int layer = 0, float normalizedTime = float.NegativeInfinity) { animator.Update(0); if (animator.GetNextAnimatorStateInfo(layer).fullPathHash == 0) { animator.SetInteger(name, transitionDuration); } else { animator.Play(animator.GetNextAnimatorStateInfo(layer).fullPathHash, layer); animator.Update(0); animator.SetInteger(name, transitionDuration); } }
public void FixedUpdateGOF() { if (overrideAnimationUpdate) { 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.nomalizedTime = currentAnimationData.secondsPlayed / currentAnimationData.length; // 애니메이션 길이와 같으면 if (currentAnimationData.secondsPlayed == currentAnimationData.length) { if (currentAnimationData.clip.wrapMode == WrapMode.Loop || currentAnimationData.clip.wrapMode == WrapMode.PingPong) { currentAnimationData.timesPlayed++; if (currentAnimationData.clip.wrapMode == WrapMode.Loop) { SetCurrentClipPosition(0); } } else if (currentAnimationData.timesPlayed == 0) { currentAnimationData.timesPlayed = 1; if ((currentAnimationData.clip.wrapMode == WrapMode.Once || currentAnimationData.clip.wrapMode == WrapMode.Clamp) && alwaysPlay) { Play(defaultAnimation, currentMirror); } else if (!alwaysPlay) { SetSpeed(0); } } } }
public static bool Play(Animator anim, string name, float speed = 1.0f, float time = 0.0f) { if (!anim) { return false; } // Enable animator if (!anim.enabled) { anim.enabled = true; } anim.speed = speed; anim.Play(name, -1, time); anim.Update(0.0f); // Update for the latest state return true; }
private void SyncAnimation(Animator animator, float normalizedTime) { animator.Play(0, -1, normalizedTime); animator.Update(Time.deltaTime); }
void TestAnis() { ani = sellast.GetComponentInChildren<Animator>(); Animation aniold = sellast.GetComponentInChildren<Animation>(); SkinnedMeshRenderer[] skins = sellast.GetComponentsInChildren<SkinnedMeshRenderer>(); GUILayout.Label("Find Ani:" + (ani != null) + ", skinmesh count=" + skins.Length); if (ani == null) { GUILayout.Label("Need Animator Component"); if(aniold!=null) { GUILayout.Label("You have a old Animation Component.But we only support Animator."); } return; } List<AnimationClip> anis = new List<AnimationClip>(); var ac = ani.runtimeAnimatorController as UnityEditorInternal.AnimatorController; FindAllAniInControl(ac, anis); foreach (var a in anis) { if (GUILayout.Button("haveani:" + a.name, GUILayout.MinWidth(1))) { _selectClip = a; anistart = 0; aniend = a.length; bPlay = false; //ani.Play(a.name, 0, 0); } } if(_selectClip) { var an = ani.GetCurrentAnimatorStateInfo(0); anistart = GUILayout.HorizontalSlider(anistart, 0, _selectClip.length, GUILayout.MinHeight(1)); if (anistart >= aniend) anistart = aniend; aniend = GUILayout.HorizontalSlider(aniend, 0, _selectClip.length, GUILayout.MinHeight(1)); if (aniend <= anistart) aniend = anistart; aninow = GUILayout.HorizontalSlider(aninow, anistart, aniend, GUILayout.MinHeight(1)); if (!bPlay) { ani.Play(_selectClip.name, 0, 0); ani.speed = 1.0f; ani.Update(aninow); } bPlay = GUILayout.Toggle(bPlay, "Play", GUILayout.MaxWidth(100)); } }