123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
-
- // =================================
- // Namespaces.
- // =================================
- using UnityEngine;
- using UnityEngine.UI;
- // =================================
- // Define namespace.
- // =================================
- namespace MirzaBeig
- {
- namespace ParticleSystems
- {
- namespace Demos
- {
- // =================================
- // Classes.
- // =================================
- public class DemoManager : MonoBehaviour
- {
- // =================================
- // Nested classes and structures.
- // =================================
- public enum ParticleMode
- {
- looping,
- oneshot,
- }
- public enum Level
- {
- none,
- basic,
- }
- // =================================
- // Variables.
- // =================================
- public Transform cameraRotationTransform;
- public Transform cameraTranslationTransform;
- public Vector3 cameraLookAtPosition = new Vector3(0.0f, 3.0f, 0.0f);
- public MouseFollow mouse;
- Vector3 targetCameraPosition;
- Vector3 targetCameraRotation;
- Vector3 cameraPositionStart;
- Vector3 cameraRotationStart;
- Vector2 input;
- // Because Euler angles wrap around 360, I use
- // a separate value to store the full rotation.
- Vector3 cameraRotation;
- public float cameraMoveAmount = 2.0f;
- public float cameraRotateAmount = 2.0f;
- public float cameraMoveSpeed = 12.0f;
- public float cameraRotationSpeed = 12.0f;
- public Vector2 cameraAngleLimits = new Vector2(-8.0f, 60.0f);
- public GameObject[] levels;
- public Level currentLevel = Level.basic;
- public ParticleMode particleMode = ParticleMode.looping;
- public bool advancedRendering = true;
- public Toggle loopingParticleModeToggle;
- public Toggle oneshotParticleModeToggle;
- public Toggle advancedRenderingToggle;
- Toggle[] levelToggles;
- public ToggleGroup levelTogglesContainer;
- LoopingParticleSystemsManager loopingParticleSystems;
- OneshotParticleSystemsManager oneshotParticleSystems;
- public GameObject ui;
- public Text particleCountText;
- public Text currentParticleSystemText;
- public Text particleSpawnInstructionText;
- public Slider timeScaleSlider;
- public Text timeScaleSliderValueText;
- public Camera mainCamera;
- public MonoBehaviour[] mainCameraPostEffects;
- // =================================
- // Functions.
- // =================================
- // ...
- void Awake()
- {
- loopingParticleSystems = FindObjectOfType<LoopingParticleSystemsManager>();
- oneshotParticleSystems = FindObjectOfType<OneshotParticleSystemsManager>();
- loopingParticleSystems.Init();
- oneshotParticleSystems.Init();
- }
- // ...
- void Start()
- {
- // ...
- cameraPositionStart = cameraTranslationTransform.localPosition;
- cameraRotationStart = cameraRotationTransform.localEulerAngles;
- ResetCameraTransformTargets();
- // ...
- switch (particleMode)
- {
- case ParticleMode.looping:
- {
- SetToLoopingParticleMode(true);
- loopingParticleModeToggle.isOn = true;
- oneshotParticleModeToggle.isOn = false;
- break;
- }
- case ParticleMode.oneshot:
- {
- SetToOneshotParticleMode(true);
- loopingParticleModeToggle.isOn = false;
- oneshotParticleModeToggle.isOn = true;
- break;
- }
- default:
- {
- print("Unknown case.");
- break;
- }
- }
- // ...
- SetAdvancedRendering(advancedRendering);
- advancedRenderingToggle.isOn = advancedRendering;
- // ...
- levelToggles =
- levelTogglesContainer.GetComponentsInChildren<Toggle>(true);
- for (int i = 0; i < levels.Length; i++)
- {
- // Toggle's OnValueChanged handles
- // level state. No need to SetActive().
- if (i == (int)currentLevel)
- {
- levels[i].SetActive(true);
- levelToggles[i].isOn = true;
- }
- else
- {
- levels[i].SetActive(false);
- levelToggles[i].isOn = false;
- }
- }
- // ...
- UpdateCurrentParticleSystemNameText();
- timeScaleSlider.onValueChanged.AddListener(OnTimeScaleSliderValueChanged);
- OnTimeScaleSliderValueChanged(timeScaleSlider.value);
- }
- // ...
- public void OnTimeScaleSliderValueChanged(float value)
- {
- Time.timeScale = value;
- timeScaleSliderValueText.text = value.ToString("0.00");
- }
- // ...
- public void SetToLoopingParticleMode(bool set)
- {
- if (set)
- {
- oneshotParticleSystems.Clear();
- loopingParticleSystems.gameObject.SetActive(true);
- oneshotParticleSystems.gameObject.SetActive(false);
- particleSpawnInstructionText.gameObject.SetActive(false);
- particleMode = ParticleMode.looping;
- UpdateCurrentParticleSystemNameText();
- }
- }
- // ...
- public void SetToOneshotParticleMode(bool set)
- {
- if (set)
- {
- loopingParticleSystems.gameObject.SetActive(false);
- oneshotParticleSystems.gameObject.SetActive(true);
- particleSpawnInstructionText.gameObject.SetActive(true);
- particleMode = ParticleMode.oneshot;
- UpdateCurrentParticleSystemNameText();
- }
- }
- // ...
- public void SetLevel(Level level)
- {
- for (int i = 0; i < levels.Length; i++)
- {
- if (i == (int)level)
- {
- levels[i].SetActive(true);
- }
- else
- {
- levels[i].SetActive(false);
- }
- }
- currentLevel = level;
- }
- // ...
- public void SetLevelFromToggle(Toggle toggle)
- {
- if (toggle.isOn)
- {
- SetLevel((Level)System.Array.IndexOf(levelToggles, toggle));
- }
- }
- // ...
- public void SetAdvancedRendering(bool value)
- {
- advancedRendering = value;
- mainCamera.allowHDR = value;
- if (value)
- {
- QualitySettings.SetQualityLevel(32, true);
- mainCamera.renderingPath = RenderingPath.UsePlayerSettings;
- mouse.gameObject.SetActive(true);
- }
- else
- {
- QualitySettings.SetQualityLevel(0, true);
- mainCamera.renderingPath = RenderingPath.VertexLit;
- mouse.gameObject.SetActive(false);
- }
- for (int i = 0; i < mainCameraPostEffects.Length; i++)
- {
- if (mainCameraPostEffects[i])
- {
- mainCameraPostEffects[i].enabled = value;
- }
- }
- }
- // ...
- public static Vector3 DampVector3(Vector3 from, Vector3 to, float speed, float dt)
- {
- return Vector3.Lerp(from, to, 1.0f - Mathf.Exp(-speed * dt));
- }
- // ...
- Vector3 cameraPositionSmoothDampVelocity;
- Vector3 cameraRotationSmoothDampVelocity;
- void Update()
- {
- // ...
- input.x = Input.GetAxis("Horizontal");
- input.y = Input.GetAxis("Vertical");
- // Get targets.
- if (Input.GetKey(KeyCode.LeftShift))
- {
- targetCameraPosition.z += input.y * cameraMoveAmount;
- targetCameraPosition.z = Mathf.Clamp(targetCameraPosition.z, -6.3f, -1.0f);
- }
- else
- {
- targetCameraRotation.y += input.x * cameraRotateAmount;
- targetCameraRotation.x += input.y * cameraRotateAmount;
- targetCameraRotation.x = Mathf.Clamp(targetCameraRotation.x, cameraAngleLimits.x, cameraAngleLimits.y);
- }
- // Camera position.
- cameraTranslationTransform.localPosition = Vector3.SmoothDamp(
- cameraTranslationTransform.localPosition, targetCameraPosition, ref cameraPositionSmoothDampVelocity, 1.0f / cameraMoveSpeed, Mathf.Infinity, Time.unscaledDeltaTime);
- // Camera container rotation.
- cameraRotation = Vector3.SmoothDamp(
- cameraRotation, targetCameraRotation, ref cameraRotationSmoothDampVelocity, 1.0f / cameraRotationSpeed, Mathf.Infinity, Time.unscaledDeltaTime);
- cameraRotationTransform.localEulerAngles = cameraRotation;
- // Look at origin.
- cameraTranslationTransform.LookAt(cameraLookAtPosition);
- // Scroll through systems.
- if (Input.GetAxis("Mouse ScrollWheel") < 0)
- {
- Next();
- }
- else if (Input.GetAxis("Mouse ScrollWheel") > 0)
- {
- Previous();
- }
- // Toggle UI.
- if (Input.GetKeyDown(KeyCode.U))
- {
- ui.SetActive(!ui.activeSelf);
- }
- // Switch between one-shot and looping prefabs.
- if (Input.GetKeyDown(KeyCode.O))
- {
- if (particleMode == ParticleMode.looping)
- {
- SetToOneshotParticleMode(true);
- }
- else
- {
- SetToLoopingParticleMode(true);
- }
- }
- // Cycle levels.
- if (Input.GetKeyDown(KeyCode.L))
- {
- SetLevel((Level)((int)(currentLevel + 1) % System.Enum.GetNames(typeof(Level)).Length));
- }
- // Random prefab while holding key.
- else if (Input.GetKey(KeyCode.R))
- {
- //if (particleMode == ParticleMode.oneshot)
- //{
- // oneshotParticleSystems.randomize();
- // updateCurrentParticleSystemNameText();
- // // If also holding down, auto-spawn at random point.
- // if (Input.GetKey(KeyCode.T))
- // {
- // //oneshotParticleSystems.instantiateParticlePrefabRandom();
- // }
- //}
- }
- // Left-click to spawn once.
- // Right-click to continously spawn.
- if (particleMode == ParticleMode.oneshot)
- {
- Vector3 mousePosition = Input.mousePosition;
- if (Input.GetMouseButtonDown(0))
- {
- CameraShake cameraShake = FindObjectOfType<CameraShake>();
- cameraShake.Add(0.2f, 5.0f, 0.2f, CameraShakeTarget.Position, CameraShakeAmplitudeCurve.FadeInOut25);
- cameraShake.Add(4.0f, 5.0f, 0.5f, CameraShakeTarget.Rotation, CameraShakeAmplitudeCurve.FadeInOut25);
- oneshotParticleSystems.InstantiateParticlePrefab(mousePosition, mouse.distanceFromCamera);
- }
- if (Input.GetMouseButton(1))
- {
- oneshotParticleSystems.InstantiateParticlePrefab(mousePosition, mouse.distanceFromCamera);
- }
- }
- // Reset.
- if (Input.GetKeyDown(KeyCode.R))
- {
- ResetCameraTransformTargets();
- }
- }
- // ...
- void LateUpdate()
- {
- // Update particle count display.
- particleCountText.text = "PARTICLE COUNT: ";
- if (particleMode == ParticleMode.looping)
- {
- particleCountText.text += loopingParticleSystems.GetParticleCount().ToString();
- }
- else if (particleMode == ParticleMode.oneshot)
- {
- particleCountText.text += oneshotParticleSystems.GetParticleCount().ToString();
- }
- }
- // ...
- void ResetCameraTransformTargets()
- {
- targetCameraPosition = cameraPositionStart;
- targetCameraRotation = cameraRotationStart;
- }
- // ...
- void UpdateCurrentParticleSystemNameText()
- {
- if (particleMode == ParticleMode.looping)
- {
- currentParticleSystemText.text = loopingParticleSystems.GetCurrentPrefabName(true);
- }
- else if (particleMode == ParticleMode.oneshot)
- {
- currentParticleSystemText.text = oneshotParticleSystems.GetCurrentPrefabName(true);
- }
- }
- // ...
- public void Next()
- {
- if (particleMode == ParticleMode.looping)
- {
- loopingParticleSystems.Next();
- }
- else if (particleMode == ParticleMode.oneshot)
- {
- oneshotParticleSystems.Next();
- }
- UpdateCurrentParticleSystemNameText();
- }
- public void Previous()
- {
- if (particleMode == ParticleMode.looping)
- {
- loopingParticleSystems.Previous();
- }
- else if (particleMode == ParticleMode.oneshot)
- {
- oneshotParticleSystems.Previous();
- }
- UpdateCurrentParticleSystemNameText();
- }
- // =================================
- // End functions.
- // =================================
- }
- // =================================
- // End namespace.
- // =================================
- }
- }
- }
- // =================================
- // --END-- //
- // =================================
|