zoukankan      html  css  js  c++  java
  • Unity3D Animator控制参数和添加事件

    Animator控制参数和添加事件

     

    using UnityEngine;
    using System.Collections;
    
    public class AniControl : MonoBehaviour {
    
    	public bool IsRolling = false;
    	public bool IsDead = false;
    	public bool IsJump = false;
    	public float Direction = 0.5f;
    	public Animator Anitor;
    	public float Velocity = 0f;
    
    	public int IsRollingId =-1;
    	public int IsJumpId = -1;
    	public int IsDeadId = -1;
    
    	AnimatorOverrideController overrideController;
    
    	void Awake()
    	{
    		//获得哈希id
    		IsRollingId = Animator.StringToHash ("IsRolling");
    		IsDeadId = Animator.StringToHash ("IsDead");
    		IsJumpId = Animator.StringToHash ("IsJump");
    
    		//要动态修改Animator 需要给OverrideController
    		overrideController = new AnimatorOverrideController();  
    		overrideController.runtimeAnimatorController  = Anitor.runtimeAnimatorController;  
    	}
    
    	// Use this for initialization
    	void Start () {
    		
    		var runClip = overrideController["Run"];  
    		Anitor.runtimeAnimatorController = overrideController;  
    
    		//动态添加事件
    		AnimationEvent aEvent1 = new AnimationEvent();  
    		aEvent1.time           = runClip.length;  
    		aEvent1.functionName   = "OnOpenComplete";   
    		aEvent1.stringParameter = runClip.length.ToString ();
    		runClip.AddEvent(aEvent1);  
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    		if (Input.GetKey (KeyCode.W)) {
    			Velocity = 1.0f;
    		} else {
    			Velocity = 0f;
    		}
    	   
    		if (Input.GetKey (KeyCode.Space)) {
    			IsJump = true;
    		} else {
    			IsJump = false;
    		}
    		if (Input.GetKey (KeyCode.A)) {
    			Direction = 0;
    		} else if (Input.GetKey (KeyCode.D)) {
    			Direction = 1;
    		} else {
    			Direction = 0.5f;
    		}
    
    
    		Anitor.SetFloat ("Velocity",Velocity);
    		Anitor.SetBool (IsRollingId,IsRolling);
    		Anitor.SetBool (IsDeadId,IsDead);
    		Anitor.SetBool (IsJumpId,IsJump);
    		Anitor.SetFloat ("Direction",Direction);
    
    	}
    
    	void OnOpenComplete(string str)
    	{
    		Debug.Log ("OnOpenComplete="+str);
    	}
    }
    

      

  • 相关阅读:
    Elasticsearch入门系列(一)
    清楚Chrome缓存
    解决IIS启动站点报错
    Input type="file"上传文件change事件只触发一次解决方案
    本地计算机上的XXX服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止
    SQL Server Datetime类型为NULL不能用ISNULL(datetime,' ')来判断,会导致1900-01-01
    浏览指南
    谁发明的c++
    c++的用处
    不一样的二叉树遍历(小学生都会)
  • 原文地址:https://www.cnblogs.com/mrblue/p/7257718.html
Copyright © 2011-2022 走看看