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);
	}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM