/Assets/JMO Assets/Cartoon FX/Demo/Assets/CFX_Demo_New.cs

https://bitbucket.org/hautecouture_matsuyama/100apps_ppg_readyset · C# · 209 lines · 161 code · 35 blank · 13 comment · 27 complexity · 9edfbd265edf7e4789e2dd43f7fc0ec5 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. // Cartoon FX - (c) 2015 - Jean Moreno
  6. //
  7. // Script handling the Demo scene of the Cartoon FX Packs
  8. public class CFX_Demo_New : MonoBehaviour
  9. {
  10. public GUIText EffectLabel;
  11. public GUIText EffectIndexLabel;
  12. public Renderer groundRenderer;
  13. public Collider groundCollider;
  14. //-------------------------------------------------------------
  15. private GameObject[] ParticleExamples;
  16. private int exampleIndex;
  17. private bool slowMo;
  18. private Vector3 defaultCamPosition;
  19. private Quaternion defaultCamRotation;
  20. private List<GameObject> onScreenParticles = new List<GameObject>();
  21. //-------------------------------------------------------------
  22. void Awake()
  23. {
  24. List<GameObject> particleExampleList = new List<GameObject>();
  25. int nbChild = this.transform.childCount;
  26. for(int i = 0; i < nbChild; i++)
  27. {
  28. GameObject child = this.transform.GetChild(i).gameObject;
  29. particleExampleList.Add(child);
  30. }
  31. particleExampleList.Sort( delegate(GameObject o1, GameObject o2) { return o1.name.CompareTo(o2.name); } );
  32. ParticleExamples = particleExampleList.ToArray();
  33. defaultCamPosition = Camera.main.transform.position;
  34. defaultCamRotation = Camera.main.transform.rotation;
  35. StartCoroutine("CheckForDeletedParticles");
  36. UpdateUI();
  37. }
  38. void Update()
  39. {
  40. if(Input.GetKeyDown(KeyCode.LeftArrow))
  41. {
  42. prevParticle();
  43. }
  44. else if(Input.GetKeyDown(KeyCode.RightArrow))
  45. {
  46. nextParticle();
  47. }
  48. else if(Input.GetKeyDown(KeyCode.Delete))
  49. {
  50. destroyParticles();
  51. }
  52. if(Input.GetMouseButtonDown(0))
  53. {
  54. RaycastHit hit = new RaycastHit();
  55. if(groundCollider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 9999f))
  56. {
  57. GameObject particle = spawnParticle();
  58. particle.transform.position = hit.point + particle.transform.position;
  59. }
  60. }
  61. float scroll = Input.GetAxis("Mouse ScrollWheel");
  62. if(scroll != 0f)
  63. {
  64. Camera.main.transform.Translate(Vector3.forward * (scroll < 0f ? -1f : 1f), Space.Self);
  65. }
  66. if(Input.GetMouseButtonDown(2))
  67. {
  68. Camera.main.transform.position = defaultCamPosition;
  69. Camera.main.transform.rotation = defaultCamRotation;
  70. }
  71. }
  72. //-------------------------------------------------------------
  73. // MESSAGES
  74. void OnToggleGround()
  75. {
  76. groundRenderer.enabled = !groundRenderer.enabled;
  77. }
  78. void OnToggleCamera()
  79. {
  80. CFX_Demo_RotateCamera.rotating = !CFX_Demo_RotateCamera.rotating;
  81. }
  82. void OnToggleSlowMo()
  83. {
  84. slowMo = !slowMo;
  85. if(slowMo) Time.timeScale = 0.33f;
  86. else Time.timeScale = 1.0f;
  87. }
  88. void OnPreviousEffect()
  89. {
  90. prevParticle();
  91. }
  92. void OnNextEffect()
  93. {
  94. nextParticle();
  95. }
  96. //-------------------------------------------------------------
  97. // UI
  98. private void UpdateUI()
  99. {
  100. EffectLabel.text = ParticleExamples[exampleIndex].name;
  101. EffectIndexLabel.text = string.Format("{0}/{1}", (exampleIndex+1).ToString("00"), ParticleExamples.Length.ToString("00"));
  102. }
  103. //-------------------------------------------------------------
  104. // SYSTEM
  105. private GameObject spawnParticle()
  106. {
  107. GameObject particles = (GameObject)Instantiate(ParticleExamples[exampleIndex]);
  108. particles.transform.position = new Vector3(0,particles.transform.position.y,0);
  109. #if UNITY_3_5
  110. particles.SetActiveRecursively(true);
  111. #else
  112. particles.SetActive(true);
  113. // for(int i = 0; i < particles.transform.childCount; i++)
  114. // particles.transform.GetChild(i).gameObject.SetActive(true);
  115. #endif
  116. ParticleSystem ps = particles.GetComponent<ParticleSystem>();
  117. #if UNITY_5_5_OR_NEWER
  118. if (ps != null)
  119. {
  120. var main = ps.main;
  121. if (main.loop)
  122. {
  123. ps.gameObject.AddComponent<CFX_AutoStopLoopedEffect>();
  124. ps.gameObject.AddComponent<CFX_AutoDestructShuriken>();
  125. }
  126. }
  127. #else
  128. if(ps != null && ps.loop)
  129. {
  130. ps.gameObject.AddComponent<CFX_AutoStopLoopedEffect>();
  131. ps.gameObject.AddComponent<CFX_AutoDestructShuriken>();
  132. }
  133. #endif
  134. onScreenParticles.Add(particles);
  135. return particles;
  136. }
  137. IEnumerator CheckForDeletedParticles()
  138. {
  139. while(true)
  140. {
  141. yield return new WaitForSeconds(5.0f);
  142. for(int i = onScreenParticles.Count - 1; i >= 0; i--)
  143. {
  144. if(onScreenParticles[i] == null)
  145. {
  146. onScreenParticles.RemoveAt(i);
  147. }
  148. }
  149. }
  150. }
  151. private void prevParticle()
  152. {
  153. exampleIndex--;
  154. if(exampleIndex < 0) exampleIndex = ParticleExamples.Length - 1;
  155. UpdateUI();
  156. }
  157. private void nextParticle()
  158. {
  159. exampleIndex++;
  160. if(exampleIndex >= ParticleExamples.Length) exampleIndex = 0;
  161. UpdateUI();
  162. }
  163. private void destroyParticles()
  164. {
  165. for(int i = onScreenParticles.Count - 1; i >= 0; i--)
  166. {
  167. if(onScreenParticles[i] != null)
  168. {
  169. GameObject.Destroy(onScreenParticles[i]);
  170. }
  171. onScreenParticles.RemoveAt(i);
  172. }
  173. }
  174. }