BalloonSpawner.cs
2.84 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: Spawns balloons
//
//=============================================================================
using UnityEngine;
using System.Collections;
namespace Valve.VR.InteractionSystem
{
//-------------------------------------------------------------------------
public class BalloonSpawner : MonoBehaviour
{
public float minSpawnTime = 5f;
public float maxSpawnTime = 15f;
private float nextSpawnTime;
public GameObject balloonPrefab;
public bool autoSpawn = true;
public bool spawnAtStartup = true;
public bool playSounds = true;
public SoundPlayOneshot inflateSound;
public SoundPlayOneshot stretchSound;
public bool sendSpawnMessageToParent = false;
public float scale = 1f;
public Transform spawnDirectionTransform;
public float spawnForce;
public bool attachBalloon = false;
public Balloon.BalloonColor color = Balloon.BalloonColor.Random;
//-------------------------------------------------
void Start()
{
if ( balloonPrefab == null )
{
return;
}
if ( autoSpawn && spawnAtStartup )
{
SpawnBalloon( color );
nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
}
}
//-------------------------------------------------
void Update()
{
if ( balloonPrefab == null )
{
return;
}
if ( ( Time.time > nextSpawnTime ) && autoSpawn )
{
SpawnBalloon( color );
nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
}
}
//-------------------------------------------------
public GameObject SpawnBalloon( Balloon.BalloonColor color = Balloon.BalloonColor.Red )
{
if ( balloonPrefab == null )
{
return null;
}
GameObject balloon = Instantiate( balloonPrefab, transform.position, transform.rotation ) as GameObject;
balloon.transform.localScale = new Vector3( scale, scale, scale );
if ( attachBalloon )
{
balloon.transform.parent = transform;
}
if ( sendSpawnMessageToParent )
{
if ( transform.parent != null )
{
transform.parent.SendMessage( "OnBalloonSpawned", balloon, SendMessageOptions.DontRequireReceiver );
}
}
if ( playSounds )
{
if ( inflateSound != null )
{
inflateSound.Play();
}
if ( stretchSound != null )
{
stretchSound.Play();
}
}
balloon.GetComponentInChildren<Balloon>().SetColor( color );
if ( spawnDirectionTransform != null )
{
balloon.GetComponentInChildren<Rigidbody>().AddForce( spawnDirectionTransform.forward * spawnForce );
}
return balloon;
}
//-------------------------------------------------
public void SpawnBalloonFromEvent( int color )
{
// Copy of SpawnBalloon using int because we can't pass in enums through the event system
SpawnBalloon( (Balloon.BalloonColor)color );
}
}
}