DemoManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. // =================================
  8. // Define namespace.
  9. // =================================
  10. namespace MirzaBeig
  11. {
  12. namespace ParticleSystems
  13. {
  14. namespace Demos
  15. {
  16. // =================================
  17. // Classes.
  18. // =================================
  19. public class DemoManager : MonoBehaviour
  20. {
  21. // =================================
  22. // Nested classes and structures.
  23. // =================================
  24. public enum ParticleMode
  25. {
  26. looping,
  27. oneshot,
  28. }
  29. public enum Level
  30. {
  31. none,
  32. basic,
  33. }
  34. // =================================
  35. // Variables.
  36. // =================================
  37. public Transform cameraRotationTransform;
  38. public Transform cameraTranslationTransform;
  39. public Vector3 cameraLookAtPosition = new Vector3(0.0f, 3.0f, 0.0f);
  40. public MouseFollow mouse;
  41. Vector3 targetCameraPosition;
  42. Vector3 targetCameraRotation;
  43. Vector3 cameraPositionStart;
  44. Vector3 cameraRotationStart;
  45. Vector2 input;
  46. // Because Euler angles wrap around 360, I use
  47. // a separate value to store the full rotation.
  48. Vector3 cameraRotation;
  49. public float cameraMoveAmount = 2.0f;
  50. public float cameraRotateAmount = 2.0f;
  51. public float cameraMoveSpeed = 12.0f;
  52. public float cameraRotationSpeed = 12.0f;
  53. public Vector2 cameraAngleLimits = new Vector2(-8.0f, 60.0f);
  54. public GameObject[] levels;
  55. public Level currentLevel = Level.basic;
  56. public ParticleMode particleMode = ParticleMode.looping;
  57. public bool advancedRendering = true;
  58. public Toggle loopingParticleModeToggle;
  59. public Toggle oneshotParticleModeToggle;
  60. public Toggle advancedRenderingToggle;
  61. Toggle[] levelToggles;
  62. public ToggleGroup levelTogglesContainer;
  63. LoopingParticleSystemsManager loopingParticleSystems;
  64. OneshotParticleSystemsManager oneshotParticleSystems;
  65. public GameObject ui;
  66. public Text particleCountText;
  67. public Text currentParticleSystemText;
  68. public Text particleSpawnInstructionText;
  69. public Slider timeScaleSlider;
  70. public Text timeScaleSliderValueText;
  71. public Camera mainCamera;
  72. public MonoBehaviour[] mainCameraPostEffects;
  73. // =================================
  74. // Functions.
  75. // =================================
  76. // ...
  77. void Awake()
  78. {
  79. loopingParticleSystems = FindObjectOfType<LoopingParticleSystemsManager>();
  80. oneshotParticleSystems = FindObjectOfType<OneshotParticleSystemsManager>();
  81. loopingParticleSystems.Init();
  82. oneshotParticleSystems.Init();
  83. }
  84. // ...
  85. void Start()
  86. {
  87. // ...
  88. cameraPositionStart = cameraTranslationTransform.localPosition;
  89. cameraRotationStart = cameraRotationTransform.localEulerAngles;
  90. ResetCameraTransformTargets();
  91. // ...
  92. switch (particleMode)
  93. {
  94. case ParticleMode.looping:
  95. {
  96. SetToLoopingParticleMode(true);
  97. loopingParticleModeToggle.isOn = true;
  98. oneshotParticleModeToggle.isOn = false;
  99. break;
  100. }
  101. case ParticleMode.oneshot:
  102. {
  103. SetToOneshotParticleMode(true);
  104. loopingParticleModeToggle.isOn = false;
  105. oneshotParticleModeToggle.isOn = true;
  106. break;
  107. }
  108. default:
  109. {
  110. print("Unknown case.");
  111. break;
  112. }
  113. }
  114. // ...
  115. SetAdvancedRendering(advancedRendering);
  116. advancedRenderingToggle.isOn = advancedRendering;
  117. // ...
  118. levelToggles =
  119. levelTogglesContainer.GetComponentsInChildren<Toggle>(true);
  120. for (int i = 0; i < levels.Length; i++)
  121. {
  122. // Toggle's OnValueChanged handles
  123. // level state. No need to SetActive().
  124. if (i == (int)currentLevel)
  125. {
  126. levels[i].SetActive(true);
  127. levelToggles[i].isOn = true;
  128. }
  129. else
  130. {
  131. levels[i].SetActive(false);
  132. levelToggles[i].isOn = false;
  133. }
  134. }
  135. // ...
  136. UpdateCurrentParticleSystemNameText();
  137. timeScaleSlider.onValueChanged.AddListener(OnTimeScaleSliderValueChanged);
  138. OnTimeScaleSliderValueChanged(timeScaleSlider.value);
  139. }
  140. // ...
  141. public void OnTimeScaleSliderValueChanged(float value)
  142. {
  143. Time.timeScale = value;
  144. timeScaleSliderValueText.text = value.ToString("0.00");
  145. }
  146. // ...
  147. public void SetToLoopingParticleMode(bool set)
  148. {
  149. if (set)
  150. {
  151. oneshotParticleSystems.Clear();
  152. loopingParticleSystems.gameObject.SetActive(true);
  153. oneshotParticleSystems.gameObject.SetActive(false);
  154. particleSpawnInstructionText.gameObject.SetActive(false);
  155. particleMode = ParticleMode.looping;
  156. UpdateCurrentParticleSystemNameText();
  157. }
  158. }
  159. // ...
  160. public void SetToOneshotParticleMode(bool set)
  161. {
  162. if (set)
  163. {
  164. loopingParticleSystems.gameObject.SetActive(false);
  165. oneshotParticleSystems.gameObject.SetActive(true);
  166. particleSpawnInstructionText.gameObject.SetActive(true);
  167. particleMode = ParticleMode.oneshot;
  168. UpdateCurrentParticleSystemNameText();
  169. }
  170. }
  171. // ...
  172. public void SetLevel(Level level)
  173. {
  174. for (int i = 0; i < levels.Length; i++)
  175. {
  176. if (i == (int)level)
  177. {
  178. levels[i].SetActive(true);
  179. }
  180. else
  181. {
  182. levels[i].SetActive(false);
  183. }
  184. }
  185. currentLevel = level;
  186. }
  187. // ...
  188. public void SetLevelFromToggle(Toggle toggle)
  189. {
  190. if (toggle.isOn)
  191. {
  192. SetLevel((Level)System.Array.IndexOf(levelToggles, toggle));
  193. }
  194. }
  195. // ...
  196. public void SetAdvancedRendering(bool value)
  197. {
  198. advancedRendering = value;
  199. mainCamera.allowHDR = value;
  200. if (value)
  201. {
  202. QualitySettings.SetQualityLevel(32, true);
  203. mainCamera.renderingPath = RenderingPath.UsePlayerSettings;
  204. mouse.gameObject.SetActive(true);
  205. }
  206. else
  207. {
  208. QualitySettings.SetQualityLevel(0, true);
  209. mainCamera.renderingPath = RenderingPath.VertexLit;
  210. mouse.gameObject.SetActive(false);
  211. }
  212. for (int i = 0; i < mainCameraPostEffects.Length; i++)
  213. {
  214. if (mainCameraPostEffects[i])
  215. {
  216. mainCameraPostEffects[i].enabled = value;
  217. }
  218. }
  219. }
  220. // ...
  221. public static Vector3 DampVector3(Vector3 from, Vector3 to, float speed, float dt)
  222. {
  223. return Vector3.Lerp(from, to, 1.0f - Mathf.Exp(-speed * dt));
  224. }
  225. // ...
  226. Vector3 cameraPositionSmoothDampVelocity;
  227. Vector3 cameraRotationSmoothDampVelocity;
  228. void Update()
  229. {
  230. // ...
  231. input.x = Input.GetAxis("Horizontal");
  232. input.y = Input.GetAxis("Vertical");
  233. // Get targets.
  234. if (Input.GetKey(KeyCode.LeftShift))
  235. {
  236. targetCameraPosition.z += input.y * cameraMoveAmount;
  237. targetCameraPosition.z = Mathf.Clamp(targetCameraPosition.z, -6.3f, -1.0f);
  238. }
  239. else
  240. {
  241. targetCameraRotation.y += input.x * cameraRotateAmount;
  242. targetCameraRotation.x += input.y * cameraRotateAmount;
  243. targetCameraRotation.x = Mathf.Clamp(targetCameraRotation.x, cameraAngleLimits.x, cameraAngleLimits.y);
  244. }
  245. // Camera position.
  246. cameraTranslationTransform.localPosition = Vector3.SmoothDamp(
  247. cameraTranslationTransform.localPosition, targetCameraPosition, ref cameraPositionSmoothDampVelocity, 1.0f / cameraMoveSpeed, Mathf.Infinity, Time.unscaledDeltaTime);
  248. // Camera container rotation.
  249. cameraRotation = Vector3.SmoothDamp(
  250. cameraRotation, targetCameraRotation, ref cameraRotationSmoothDampVelocity, 1.0f / cameraRotationSpeed, Mathf.Infinity, Time.unscaledDeltaTime);
  251. cameraRotationTransform.localEulerAngles = cameraRotation;
  252. // Look at origin.
  253. cameraTranslationTransform.LookAt(cameraLookAtPosition);
  254. // Scroll through systems.
  255. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  256. {
  257. Next();
  258. }
  259. else if (Input.GetAxis("Mouse ScrollWheel") > 0)
  260. {
  261. Previous();
  262. }
  263. // Toggle UI.
  264. if (Input.GetKeyDown(KeyCode.U))
  265. {
  266. ui.SetActive(!ui.activeSelf);
  267. }
  268. // Switch between one-shot and looping prefabs.
  269. if (Input.GetKeyDown(KeyCode.O))
  270. {
  271. if (particleMode == ParticleMode.looping)
  272. {
  273. SetToOneshotParticleMode(true);
  274. }
  275. else
  276. {
  277. SetToLoopingParticleMode(true);
  278. }
  279. }
  280. // Cycle levels.
  281. if (Input.GetKeyDown(KeyCode.L))
  282. {
  283. SetLevel((Level)((int)(currentLevel + 1) % System.Enum.GetNames(typeof(Level)).Length));
  284. }
  285. // Random prefab while holding key.
  286. else if (Input.GetKey(KeyCode.R))
  287. {
  288. //if (particleMode == ParticleMode.oneshot)
  289. //{
  290. // oneshotParticleSystems.randomize();
  291. // updateCurrentParticleSystemNameText();
  292. // // If also holding down, auto-spawn at random point.
  293. // if (Input.GetKey(KeyCode.T))
  294. // {
  295. // //oneshotParticleSystems.instantiateParticlePrefabRandom();
  296. // }
  297. //}
  298. }
  299. // Left-click to spawn once.
  300. // Right-click to continously spawn.
  301. if (particleMode == ParticleMode.oneshot)
  302. {
  303. Vector3 mousePosition = Input.mousePosition;
  304. if (Input.GetMouseButtonDown(0))
  305. {
  306. CameraShake cameraShake = FindObjectOfType<CameraShake>();
  307. cameraShake.Add(0.2f, 5.0f, 0.2f, CameraShakeTarget.Position, CameraShakeAmplitudeCurve.FadeInOut25);
  308. cameraShake.Add(4.0f, 5.0f, 0.5f, CameraShakeTarget.Rotation, CameraShakeAmplitudeCurve.FadeInOut25);
  309. oneshotParticleSystems.InstantiateParticlePrefab(mousePosition, mouse.distanceFromCamera);
  310. }
  311. if (Input.GetMouseButton(1))
  312. {
  313. oneshotParticleSystems.InstantiateParticlePrefab(mousePosition, mouse.distanceFromCamera);
  314. }
  315. }
  316. // Reset.
  317. if (Input.GetKeyDown(KeyCode.R))
  318. {
  319. ResetCameraTransformTargets();
  320. }
  321. }
  322. // ...
  323. void LateUpdate()
  324. {
  325. // Update particle count display.
  326. particleCountText.text = "PARTICLE COUNT: ";
  327. if (particleMode == ParticleMode.looping)
  328. {
  329. particleCountText.text += loopingParticleSystems.GetParticleCount().ToString();
  330. }
  331. else if (particleMode == ParticleMode.oneshot)
  332. {
  333. particleCountText.text += oneshotParticleSystems.GetParticleCount().ToString();
  334. }
  335. }
  336. // ...
  337. void ResetCameraTransformTargets()
  338. {
  339. targetCameraPosition = cameraPositionStart;
  340. targetCameraRotation = cameraRotationStart;
  341. }
  342. // ...
  343. void UpdateCurrentParticleSystemNameText()
  344. {
  345. if (particleMode == ParticleMode.looping)
  346. {
  347. currentParticleSystemText.text = loopingParticleSystems.GetCurrentPrefabName(true);
  348. }
  349. else if (particleMode == ParticleMode.oneshot)
  350. {
  351. currentParticleSystemText.text = oneshotParticleSystems.GetCurrentPrefabName(true);
  352. }
  353. }
  354. // ...
  355. public void Next()
  356. {
  357. if (particleMode == ParticleMode.looping)
  358. {
  359. loopingParticleSystems.Next();
  360. }
  361. else if (particleMode == ParticleMode.oneshot)
  362. {
  363. oneshotParticleSystems.Next();
  364. }
  365. UpdateCurrentParticleSystemNameText();
  366. }
  367. public void Previous()
  368. {
  369. if (particleMode == ParticleMode.looping)
  370. {
  371. loopingParticleSystems.Previous();
  372. }
  373. else if (particleMode == ParticleMode.oneshot)
  374. {
  375. oneshotParticleSystems.Previous();
  376. }
  377. UpdateCurrentParticleSystemNameText();
  378. }
  379. // =================================
  380. // End functions.
  381. // =================================
  382. }
  383. // =================================
  384. // End namespace.
  385. // =================================
  386. }
  387. }
  388. }
  389. // =================================
  390. // --END-- //
  391. // =================================