Untiy中控制Animation的播放速度

Unity組件裏面的Animation是可以控制播放速度的,通過調整AnimationState的speed屬性即可更改某個動畫的速度。

在實際需求中,可以通過改變動畫速度來更快地打開一個獎勵或者過場等等,這樣可以讓用戶減少等待時間,提升體驗感。

官方也給出了相關的控制接口

https://docs.unity3d.com/ScriptReference/Animation.html

anim = GetComponent<Animation>();
foreach (AnimationState state in anim)
{
    state.speed = 0.5F;
}

或者控制某個單獨的

public void SetAnimationSpeed(Animation ani, string name, float speed)
{
    if (null == ani) return;
    AnimationState state = ani[name];
    if (!state) state.speed = speed;
}

在lua中寫這樣的接口:

注意到,獲取AnimationState的屬性

提供了一個get屬性的this字段,利用這個字段可以改變速度:

function SetAnimationSpeed(animation, name, speed)
    if animation == nil then return end
    local state = animation.this:get(name)
    if state then state.speed = speed end
end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章