-
Notifications
You must be signed in to change notification settings - Fork 1
/
MediaSpawner.cs
86 lines (66 loc) · 2.03 KB
/
MediaSpawner.cs
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
using UnityEngine;
using System.Collections;
public abstract class Spawner {
private static MonoBehaviour _monoBehaviour;
private float _delay = 0f;
public static void init(MonoBehaviour monoBehaviour) {
_monoBehaviour = monoBehaviour;
}
protected static MonoBehaviour getMonoBehaviour() {
return _monoBehaviour;
}
public Spawner setDelay(float delay) {
_delay = delay;
return this;
}
public void spawnDelayed() {
getMonoBehaviour().StartCoroutine(_spawnDelayed(_delay));
}
private IEnumerator _spawnDelayed(float delaySeconds = 0) {
yield return new WaitForSeconds(delaySeconds);
this.spawn();
}
public abstract void spawn();
}
public abstract class MediaSpawner<MediaObject> : Spawner{
private MediaObject _mediaObject;
public MediaObject getMediaObject() {
return _mediaObject;
}
public MediaSpawner<MediaObject> setMediaObject(MediaObject mediaObject) {
_mediaObject = mediaObject;
return this;
}
}
public class AudioSpawner : MediaSpawner<AudioClip> {
private AudioSource _audioSource;
public AudioSpawner setAudioSource(AudioSource audioSource) {
_audioSource = audioSource;
return this;
}
public override void spawn() {
AudioClip ac = getMediaObject();
Debug.Log("Play audio: " + ac.name);
_audioSource.PlayOneShot(getMediaObject());
}
}
public class ImageSpawner : MediaSpawner<Texture> {
private GameObject _container;
private GameObject _imageSource;
public ImageSpawner setImageSource(GameObject imageSource) {
_imageSource = imageSource;
return this;
}
public ImageSpawner setContainer(GameObject container) {
_container = container;
return this;
}
public override void spawn() {
GameObject content = null;
Texture tex = getMediaObject();
Debug.Log ("Spawn texture: " + tex.name);
content = GameObject.Instantiate (_container, _imageSource.transform.position, _imageSource.transform.rotation) as GameObject;
content.GetComponent<Renderer> ().material.SetTexture ("_MainTex", tex);
content.GetComponent<Renderer> ().material.SetTexture ("_EmissionMap", tex);
}
}