01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
static IEnumerator Play ( Animation animation , string clipName , bool useTimeScale , System.Action onComplete )
{
if ( !useTimeScale )
{
AnimationState _currState = animation[clipName];
bool isPlaying = true ;
float _startTime = 0 F;
float _progressTime = 0 F;
float _timeAtLastFrame = 0 F;
float _timeAtCurrentFrame = 0 F;
float deltaTime = 0 F;
animation.Play ( clipName ) ;
_timeAtLastFrame = Time.realtimeSinceStartup;
while ( isPlaying )
{
_timeAtCurrentFrame = Time.realtimeSinceStartup;
deltaTime = _timeAtCurrentFrame - _timeAtLastFrame;
_timeAtLastFrame = _timeAtCurrentFrame;
_progressTime + = deltaTime;
_currState.normalizedTime = _progressTime / _currState.length;
animation.Sample ( ) ;
if ( _progressTime > = _currState.length )
{
if ( _currState.wrapMode ! = WrapMode.Loop )
{
isPlaying = false ;
}
else
{
_progressTime = 0.0 f;
}
}
yield return new WaitForEndOfFrame ( ) ;
}
yield return null;
if ( onComplete ! = null )
{
onComplete ( ) ;
}
}
else
{
animation.Play ( clipName ) ;
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using UnityEngine;
using System.Collections;
public class ParticaleAnimator : MonoBehaviour {
private void Awake ( )
{
particle = GetComponent < ParticleSystem > ( ) ;
}
/ / Use this for initialization
void Start ( )
{
lastTime = Time.realtimeSinceStartup;
}
/ / Update is called once per frame
void Update ( )
{
float deltaTime = Time.realtimeSinceStartup - ( float ) lastTime;
particle.Simulate ( deltaTime , true , false ) ; / / last must be false !!
lastTime = Time.realtimeSinceStartup;
}
private double lastTime;
private ParticleSystem particle;
}
|
1
2
3
4
5
6
|
/ / 添加一个声音组件
AudioSource source = gameObject.AddComponent < AudioSource > ( ) ;
/ / 设置播放声音的速度。 默认速度是 1 ,那如果我们加速了,那就应该是 Time.timeScale
source.pitch = IgnoreTimeScale? 1 : Time.timeScale;
/ / 播放声音
source.Play ( ) ;
|
1
2
3
4
5
6
7
8
|
/ / 创建一个音频的字典
private static Dictionary < AudioSource , bool > soundList = new Dictionary < AudioSource , bool > ( ) ;
/ / 播放声音
source.Play ( ) ;
/ / 把音频对象加入字典中, value 就是是否忽略timescale
soundList.Add ( source , IgnoreTimeScale ) ;
|
01
02
03
04
05
06
07
08
09
10
|
public static void TimeScaleChanged ( )
{
foreach ( AudioSource source in soundList.Keys )
{
if ( source ! = null )
{
source.pitch = soundList[source]? 1 : Time.timeScale;
}
}
}
|