forked from bbbscarter/UberAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioEmitter.cs
More file actions
executable file
·97 lines (84 loc) · 2.76 KB
/
AudioEmitter.cs
File metadata and controls
executable file
·97 lines (84 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
namespace UberAudio
{
/// <summary>
/// An audio emitter is UberAudio's version of an AudioSource.
/// AudioManager.Play() returns an AudioEmitter GameObject
/// AudioEmitters automatically track the life, position, etc of the GameObject they're attached to.
/// </summary>
public class AudioEmitter : MonoBehaviour
{
Transform AttachedTo;
AudioSource _Source;
AudioEvent AudioSoundEvent;
/// <summary>
/// Update the emitter
/// </summary>
public void LateUpdate()
{
if(_Source==null)
{
return;
}
//Unless the emitter is flagged to stay still, track the gameobject we're attached to
if (!AudioSoundEvent.DoNotTrackSourceMovement)
{
if (AttachedTo != null && AttachedTo.hasChanged)
{
transform.position = AttachedTo.position;
}
}
//If we're a looping sound and our parent has died, release the loop.
if(_Source.loop)
{
if(AttachedTo==null && !AudioSoundEvent.KeepLoopingWhenSourceDies)
{
_Source.loop = false;
}
}
//If attached object is dead and the emitter is flagged to die if so, stop playing
if (AudioSoundEvent.StopWhenSourceDies && AttachedTo == null)
{
_Source.Stop();
}
}
public void Play()
{
_Source.Play();
}
public AudioSource Source
{
get{ return _Source; }
}
public bool Finished
{
get
{
return _Source==null || !_Source.isPlaying;
}
}
public static AudioEmitter Create(Transform attachedTo, AudioEvent ev)
{
GameObject go = new GameObject("AudioEmitter");
AudioEmitter emitter = go.AddComponent<AudioEmitter>();
emitter._Source = go.AddComponent<AudioSource>();
go.transform.parent = AudioManager.Instance.gameObject.transform;
go.transform.position = attachedTo.position;
emitter.AttachedTo = attachedTo;
emitter.AudioSoundEvent = ev;
ev.TransferToAudioSource(emitter._Source);
return emitter;
}
public void Stop()
{
if(_Source)
{
_Source.Stop();
_Source = null;
}
}
}
}