Unity3D中暫停時的動畫及粒子效果實現


暫停是游戲中經常出現的功能,而Unity3D中對於暫停的處理並不是很理想。一般的做法是將Time.timeScale設置為0。Unity的文檔中對於這種情況有以下描述;

The scale at which the time is passing. This can be used for slow motion effects….When timeScale is set to zero the game is basically paused …

timeScale表示游戲中時間流逝快慢的尺度。文檔中明確表示,這個參數是用來做慢動作效果的。對於將timeScale設置為0的情況,僅只有一個補充說明。在實際使用中,通過設置timeScale來實現慢動作特效,是一種相當簡潔且不帶任何毒副作用的方法,但是當將timeScale設置為0來實現暫停時,由於時間不再流逝,所有和時間有關的功能痘將停止,有些時候這正是我們想要的,因為畢竟是暫停。但是副作用也隨之而來,在暫停時各種動畫和粒子效果都將無法播放(因為是時間相關的),FixedUpdate也將不再被調用。

換句話說,最大的影響是,在timeScale=0的暫停情況下,你將無法實現暫停菜單的動畫以及各種漂亮的點擊效果。

但是並非真的沒辦法,關於timeScale的文檔下就有提示:

Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring variables of the Time class.

因為 realtimeSinceStartuptimeScale 無關,因此也就成了解決在暫停下的動畫和粒子效果的救命稻草。對於Unity動畫,在每一幀,根據實際時間尋找相應幀並采樣顯示的方法來模擬動畫:

AnimationState _currState = animation[clipName];

bool isPlaying = true; float _progressTime = 0F; float _timeAtLastFrame = 0F; float _timeAtCurrentFrame = 0F; bool _inReversePlaying = false; float _deltaTime = 0F; animation.Play(clipName); _timeAtLastFrame = Time.realtimeSinceStartup; while (isPlaying) { _timeAtCurrentFrame = Time.realtimeSinceStartup; _deltaTime = _timeAtCurrentFrame - _timeAtLastFrame; _timeAtLastFrame = _timeAtCurrentFrame; _progressTime += _deltaTime; _currState.normalizedTime = _inReversePlaying ? 1.0f - (_progressTime / _currState.length) : _progressTime / _currState.length; animation.Sample(); //…repeat or over by wrap mode } 

對於粒子效果,同樣進行計時,並通過粒子系統的Simulate方法來模擬對應時間的粒子狀態來完成效果,比如對於Legacy粒子,使Emitter在timeScale=0暫停時繼續有效發射並顯示效果:

_deltaTime = Time.realtimeSinceStartup - _timeAtLastFrame;
_timeAtLastFrame = Time.realtimeSinceStartup;
if (Time.timeScale == 0 ){ _emitter.Simulate(_deltaTime); _emitter.emit = true; } 

核心的代碼基本都在上面了,可以根據這個思路完成實現。完整的代碼和示例工程我放到了github上,有需要的朋友可以去查看,也歡迎大家指正。

 

https://onevcat.com/2013/01/do_not_pause_me/


免責聲明!

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



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