變量:
clip
默認的動畫剪輯。
playAutomatically
是否在開始的時候自動播放默認的動畫(Animation.clip)。
wrapMode :循環模式
WrapMode.Default:
WrapMode.Default:從動畫剪輯中讀取循環模式(默認是Once)。
WrapMode.Once:
WrapMode.Once:當時間播放到末尾的時候停止動畫的播放。
WrapMode.Loop:
WrapMode.Loop:當時間播放到末尾的時候重新播放從開始播放。
WrapMode.PingPong:
WrapMode.PingPong:在開始和結束之間來回播放。
WrapMode.ClampForever:
WrapMode.ClampForever:播放動畫。當播放到結尾的時候,動畫總是處于最后一幀的采樣狀態。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public void Awake() {
animation.wrapMode = WrapMode.Loop;
}
}
//Make the animation loop
//使用動畫循環模式。
animation.wrapMode = WrapMode.Loop;
動畫剪輯播放完成之后,應該如何操作?
isPlaying
是否在播放任何動畫?
this [string name]
返回名稱為name的動畫狀態。
animatePhysics
如果打開這個選項,動畫會在物理循環過程中被執行。這個選項只有在結合運動學剛體的時候才有用。
一個動畫平臺可以應用速度和摩擦到其頂部的剛體。為了使用這個,animatePhysics必須被啟用,并且動畫物體必須為一個運動學剛體。
animateOnlyIfVisible
如果打開這個選項,Unity可能在它認為用戶不會看到當前動畫的時候停止播放。
當你有一些動畫在視口之外播放的時候,這個選項可以幫助你節省一些性能,比如那些只有在用戶實際上看得見的物體做動畫的時候。
Functions 函數:
Stop:
停止單簽Animation正在播放的動畫。
function Stop () : void
animation.Stop();
? function Stop (name : string) : void
停止播放名稱為name 的動畫
停止一個動畫會讓動畫重回開始位置
eg:animation.Stop("Walk");
Rewind:倒回;
function Rewind (name : string) : void
animation.Rewind("walk");
倒回名稱為name的動畫;
function Rewind () : void
倒回所有動畫;
animation.Rewind();
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;
IsPlaying:是否在播放:
function IsPlaying (name : string) : bool
名為name的動畫是否正在播放;
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
// 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);
CrossFade:淡入淡出
function CrossFade (animation : string, fadeLength
: float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer)
: void
在一定時間淡入名稱為name的動畫并且淡出其他動畫,
如果模式是PlayMode.StopSameLayer,在同一層的動畫將在動畫淡入的時候淡出。如果模式是PlayMode.StopAll,所有動畫將在淡入的時候淡出
如果動畫沒有被設置成循環,它將停止并且在播放完成之后倒帶至開始。
eg:
animation.CrossFade("Walk", 0.2);
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Run");
else
animation.CrossFade("Idle");
}
CrossFadeQueued:淡入淡出列
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Run");
else
animation.CrossFade("Idle");
}
在前一個動畫播放完成之后淡入淡出下一個動畫。
如果queue為QueueMode.CompleteOthers這個動畫只在所有其他動畫都停止播放時才開始。
如果queue為QueueMode.PlayNow這個動畫將以一個復制的動畫狀態立即開始播放。
動畫播放完成后它將自動清除它自己。在它播放完成后使用賦值的動畫將導致一個異常。
function Update () {
if (Input.GetButtonDown("Fire1"))
animation.CrossFadeQueued("shoot", 0.3, QueueMode.PlayNow);
}
PLayQueued:
在前一個動畫播放完成后直接播放下一個動畫。
function PlayQueued (animation : string, queue : QueueMode = QueueMode.CompleteOthers, mode : PlayMode = PlayMode.StopSameLayer) : AnimationState
比如你可能會一個接一個播放一個特殊序列的動畫。
動畫狀態在播放之前會復制自己,因此你可以在相同的動畫之間進行淡入淡出。這個可以用于覆蓋兩個相同的動畫。比如你可能有一個劍揮舞的動畫,玩家連續快速削兩次。你可以倒播這個動畫然后從頭再播放但是你會看到動畫之間有跳越現象。
如果queue是QueueMode.CompleteOthers,當所有其他動畫停止播放,這個動畫才會開始。
如果queue是QueueMode.PlayNow,這個動畫將在一個重復的動畫狀態下立即開始播放。
當動畫完成播放之后,它會自動清理。在一個動畫狀態結束之后使用它,將會導致一個異常。
function Update () {
if (Input.GetButtonDown("Fire1"))
animation.PlayQueued("shoot", QueueMode.PlayNow);
}
AddClip:添加剪輯
function AddClip (clip : AnimationClip, newName : string) : void
給動畫添加一個名稱為newName的動畫剪輯。
? function AddClip (clip : AnimationClip, newName : string, firstFrame : int, lastFrame : int, addLoopFrame : bool = false) : void
在播放的firstFrame和lastFrame之間添加動畫剪輯。新的動畫剪輯也會被添加到名稱為newName的動畫中。
addLoopFrame:
是否在第一幀之后添加一個額外的幀?如果你在制作一個循環的動畫,那么可以打開這個選項。如果那么名稱的動畫剪輯已經存在,那么會被新的動畫剪輯覆蓋。
/ Split the default clip into a shoot, walk and idle animation
// 把默認的動畫剪輯拆分成一個shoot,walk和idle的動畫。
animation.AddClip(animation.clip, "shoot", 0, 10);
// walk and idle will add an extra looping frame at the end
// walk和idle動畫剪輯會在結尾被添加一個額外的循環幀。
animation.AddClip(animation.clip, "walk", 11, 20, true);
animation.AddClip(animation.clip, "idle", 21, 30, true);
Animation.RemoveClip:移出剪輯:
function RemoveClip (clip : AnimationClip) : void
從動畫列表中移除剪輯。
這將去掉剪輯和所有基于這個剪輯的動畫狀態。
? function RemoveClip (clipName : string) : void
從動畫列表移除剪輯。
這將去掉名稱為name的動畫狀態。
GetClipCount:獲取剪輯數:
function GetClipCount () : int
取得當前動畫的動畫剪輯數量;
SynLayer:同步層:
function SyncLayer (layer : int) : void
同步某層的動畫的播放速度;
在混合兩個循環動畫的時候,他們通常有不同的長度。比如一個走路的動畫通常比一個跑步的動畫要長。當混合他們的時候,你需要確定走路和跑步動畫發生的時候腳的位置要在同一時間走動。換句話說,這兩個動畫播放速度要被調整一致,以便動畫的同步。SyncLayer函數將根據動畫的混合權重來計算同一層中動畫的平均正常播放速度。然后它會給同一層的所有動畫應用這個播放速度。
// Puts the walk and run animation in the same
// layer and synchronizes their speed
// 把walk和run動畫放到同一層,然后同步他們的速度。
animation["walk"].layer = 1;
animation["run"].layer = 1;
animation.SyncLayer(1);