zoukankan      html  css  js  c++  java
  • unity 动画控制

    播放控制

    Animator animator;
    
    animator.speed=0;//停止播放
    animator.speed=-1;//倒放
    
    animator.Play("stateName",0,0);//播放指定状态(stateName是Animator窗口里各个状态的名称)
    

    跳到指定帧,并停止

    Animator animator;
    
    animator.speed=0;//停止播放
    
    AnimationClip clip=animator.runtimeAnimatorController.animationClips[0];
    float frameTime=(1f/clip.frameRate)*5;//跳到第5帧
    
    animator.Play("stateName",0,frameTime);//播放指定状态(stateName是Animator窗口里各个状态的名称)
    

    在动画指定的时间里,添加事件函数
    注意:函数所在的脚本和Animator 组件必须绑定到同一个GameObject

    using UnityEngine;
    using System.Collections;
    
    public class TestAnimationEvent:MonoBehaviour{
    	
    	private void Awake(){
    		Animator animator=GetComponent<Animator>();//Animator组件和此脚本必须绑定到同一个GameObject
                    
    		AnimatorOverrideController overrideController=new AnimatorOverrideController(animator.runtimeAnimatorController);
    		//通过子符串获取AnimationClip,注意并不是Animator窗口里各个状态的名称,而是在Animator窗口中选择的状态在Inspector面板的motion属性显示的名称
    		AnimationClip clip=overrideController["CINEMA_4D___"];
    
    		//在动画指定的时间,添加事件
    		AnimationEvent onCompleteEvent=new AnimationEvent();
    		onCompleteEvent.objectReferenceParameter=this;
    		onCompleteEvent.functionName=nameof(OnAnimationClipComplete);
    		onCompleteEvent.time=clip.length;//clip.length动画长度,单位为秒。这里表示在结束时添加事件
    		clip.AddEvent(onCompleteEvent);
    	}
    
    	private void OnAnimationClipComplete(UnityEngine.Object objectReference){//注意:参数类型是UnityEngine.Object不是object
    		//注意:当内存中拥有多个当前类的实例时,将会调用多次此函数,使用objectReferenceParameter属性传递this区分
    		if(objectReference!=this)return;
    
    		Debug.Log("onShakeAnimComplete;");
    	}
    }
    

    第二种方法:
    通过AnimatorStateInfo.normalizedTime判断播放完成

    using UnityEngine;
    using System.Collections;
    
    public class Test:MonoBehaviour{
    
    	public Animator animator;
    
    	private void Awake(){
    		animator.Play("attack");
    		InvokeRepeating(nameof(checkStateComplete),0.02f,0.02f);
    	}
    
    	private void checkStateComplete(){
    		AnimatorStateInfo stateInfo=animator.GetCurrentAnimatorStateInfo(0);
    		if(stateInfo.IsName("attack")){
                //normalizedTime:表示动画的标准化时间,区间:[0,1]
    			if(stateInfo.normalizedTime>=1.0f) {
    				onStateComplete();
    				CancelInvoke(nameof(checkStateComplete));
    			}
    		}
    	}
    	
    	private void onStateComplete(){
    		Debug.Log("onStateComplete");
    	}
    	
    	private void OnDestroy(){
    		CancelInvoke("checkStateComplete");
    	}
    }
    
  • 相关阅读:
    android Animation整理
    js的console总结
    [原创]cocos2d-lua学习笔记(0)-提纲
    【转】js怎么编译成JSC
    【转】PCDuino用python读取GPIO数据
    Mysql数据库大小相关的问题
    oracle with as
    python jar
    investopedia level 2
    warning MSB3391
  • 原文地址:https://www.cnblogs.com/kingBook/p/11435505.html
Copyright © 2011-2022 走看看