Unity3D中Animation的常見屬性及方法

bean244發表於2014-03-05

Unity3D中Animation的常見屬性及方法如下:

Animation.Play播放

function Play (mode : PlayMode = PlayMode.StopSameLayer) : bool
function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool

Play()將開始播放名稱為animation的動畫,或者播放預設動畫。動畫會突然開始播放而沒有任何混合。如果模式是PlayMode.StopSameLayer,那麼所有在同一個層的動畫將停止播放。如果模式是PlayMode.StopAll,那麼所有當前在播放的動畫將停止播放。

如果動畫已經在播放過程中,別的動畫將停止但是動畫不會回退到開始位置。

如果動畫沒有被設定成迴圈模式,它將停止並且回退到開始位置。

如果動畫不能被播放(沒有動畫剪輯或者沒有預設動畫),Play()將返回false。

// 播放預設動畫。
animation.Play();// Plays the walk animation - stops all other animations in the same layer
// 播放walk動畫 - 停止同一層的其他動畫。
animation.Play("walk");
// Plays the walk animation - stops all other animations
// 播放walk動畫 - 停止其他動畫。
animation.Play("walk", PlayMode.StopAll);

Animation.CrossFade淡入淡出

function CrossFade (animation : string, fadeLength : float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer) : void

在一定時間內淡入名稱為name的動畫並且淡出其他動畫。如果模式是PlayMode.StopSameLayer,在同一層的動畫將在動畫淡入的時候淡出。如果模式是PlayMode.StopAll,所有動畫將在淡入的時候淡出。如果動畫沒有被設定成迴圈,它將停止並且在播放完成之後倒帶至開始。

// Fade the walk cycle in and fade all other animations in the same layer out.
// 淡入walk迴圈並且淡出同一層的所有其他動畫。
// Complete the fade within 0.2 seconds.
// 在0.2秒之內完成淡入淡出。
animation.CrossFade("Walk", 0.2);
// Makes a character contains a Run and Idle animation
// fade between them when the player wants to move
// 讓一個角色包含Run和Idle動畫,並且在玩家想移動的時候在他們之間淡入淡出。
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Run");
else
animation.CrossFade("Idle");
}

Animation.Sample取樣

function Sample () : void
在當前狀態對動畫進行取樣。
當你明確想設定一些動畫狀態並且對它取樣一次的時候有用。
// Set up some state;
// 設定一些狀態;
animation["MyClip"].time = 2.0;
animation["MyClip"].enabled = true;
// Sample animations now.
// 取樣動畫。
animation.Sample();animation["MyClip"].enabled = false;

Animation.Stop 停止

function Stop () : void
// Stop all animations
//停止所有動畫。
animation.Stop();

Animation.this[string name]操作名字

var this[name : string] : AnimationState
// Get the walk animation state and set its speed
// 取得walk動畫狀態並設定其速度。
animation["walk"].speed = 2.0;// Get the run animation state and set its weight
// 取得run動畫狀態並設定其重量。
animation["run"].weight = 0.5;

Animation.wrapMode迴圈模式動畫剪輯播放完成之後,應該如何操作?

WrapMode.Default:從動畫剪輯中讀取迴圈模式(預設是Once)。

WrapMode.Once:當時間播放到末尾的時候停止動畫的播放。

WrapMode.Loop:當時間播放到末尾的時候重新播放從開始播放。

WrapMode.ClampForever:播放動畫。當播放到結尾的時候,動畫總是處於最後一幀的取樣狀態。

//Make the animation loop
//使用動畫迴圈模式。
animation.wrapMode = WrapMode.Loop;

相關文章