summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSAudio.cs
blob: 3b181435d0a7ddd01522344739b539aa3968eb3c (plain)
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
using System.Collections.Generic;
using UnityEngine;

namespace Pathfinding.Examples.RTS {
	[HelpURL("https://arongranberg.com/astar/documentation/stable/rtsaudio.html")]
	public class RTSAudio : VersionedMonoBehaviour {
		List<Source> sources = new List<Source>();

		class Source {
			public AudioSource source;

			public bool available {
				get {
					return !source.isPlaying;
				}
			}

			public void Play (AudioClip clip) {
				source.PlayOneShot(clip);
			}
		}

		Source GetSource () {
			for (int i = 0; i < sources.Count; i++) {
				if (sources[i].available) {
					return sources[i];
				}
			}

			var go = new GameObject("Source");
			go.transform.SetParent(transform, false);
			var source = new Source {
				source = go.AddComponent<AudioSource>()
			};

			sources.Add(source);
			return source;
		}

		public void Play (AudioClip clip) {
			GetSource().Play(clip);
		}
	}
}