1
0

BalloonSpawner.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Spawns balloons
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class BalloonSpawner : MonoBehaviour
  12. {
  13. public float minSpawnTime = 5f;
  14. public float maxSpawnTime = 15f;
  15. private float nextSpawnTime;
  16. public GameObject balloonPrefab;
  17. public bool autoSpawn = true;
  18. public bool spawnAtStartup = true;
  19. public bool playSounds = true;
  20. public SoundPlayOneshot inflateSound;
  21. public SoundPlayOneshot stretchSound;
  22. public bool sendSpawnMessageToParent = false;
  23. public float scale = 1f;
  24. public Transform spawnDirectionTransform;
  25. public float spawnForce;
  26. public bool attachBalloon = false;
  27. public Balloon.BalloonColor color = Balloon.BalloonColor.Random;
  28. //-------------------------------------------------
  29. void Start()
  30. {
  31. if ( balloonPrefab == null )
  32. {
  33. return;
  34. }
  35. if ( autoSpawn && spawnAtStartup )
  36. {
  37. SpawnBalloon( color );
  38. nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
  39. }
  40. }
  41. //-------------------------------------------------
  42. void Update()
  43. {
  44. if ( balloonPrefab == null )
  45. {
  46. return;
  47. }
  48. if ( ( Time.time > nextSpawnTime ) && autoSpawn )
  49. {
  50. SpawnBalloon( color );
  51. nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
  52. }
  53. }
  54. //-------------------------------------------------
  55. public GameObject SpawnBalloon( Balloon.BalloonColor color = Balloon.BalloonColor.Red )
  56. {
  57. if ( balloonPrefab == null )
  58. {
  59. return null;
  60. }
  61. GameObject balloon = Instantiate( balloonPrefab, transform.position, transform.rotation ) as GameObject;
  62. balloon.transform.localScale = new Vector3( scale, scale, scale );
  63. if ( attachBalloon )
  64. {
  65. balloon.transform.parent = transform;
  66. }
  67. if ( sendSpawnMessageToParent )
  68. {
  69. if ( transform.parent != null )
  70. {
  71. transform.parent.SendMessage( "OnBalloonSpawned", balloon, SendMessageOptions.DontRequireReceiver );
  72. }
  73. }
  74. if ( playSounds )
  75. {
  76. if ( inflateSound != null )
  77. {
  78. inflateSound.Play();
  79. }
  80. if ( stretchSound != null )
  81. {
  82. stretchSound.Play();
  83. }
  84. }
  85. balloon.GetComponentInChildren<Balloon>().SetColor( color );
  86. if ( spawnDirectionTransform != null )
  87. {
  88. balloon.GetComponentInChildren<Rigidbody>().AddForce( spawnDirectionTransform.forward * spawnForce );
  89. }
  90. return balloon;
  91. }
  92. //-------------------------------------------------
  93. public void SpawnBalloonFromEvent( int color )
  94. {
  95. // Copy of SpawnBalloon using int because we can't pass in enums through the event system
  96. SpawnBalloon( (Balloon.BalloonColor)color );
  97. }
  98. }
  99. }