ParticleSceneControls.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using UnityStandardAssets.Effects;
  7. namespace UnityStandardAssets.SceneUtils
  8. {
  9. public class ParticleSceneControls : MonoBehaviour
  10. {
  11. public enum Mode
  12. {
  13. Activate,
  14. Instantiate,
  15. Trail
  16. }
  17. public enum AlignMode
  18. {
  19. Normal,
  20. Up
  21. }
  22. public DemoParticleSystemList demoParticles;
  23. public float spawnOffset = 0.5f;
  24. public float multiply = 1;
  25. public bool clearOnChange = false;
  26. public Text titleText;
  27. public Transform sceneCamera;
  28. public Text instructionText;
  29. public Button previousButton;
  30. public Button nextButton;
  31. public GraphicRaycaster graphicRaycaster;
  32. public EventSystem eventSystem;
  33. private ParticleSystemMultiplier m_ParticleMultiplier;
  34. private List<Transform> m_CurrentParticleList = new List<Transform>();
  35. private Transform m_Instance;
  36. private static int s_SelectedIndex = 0;
  37. private Vector3 m_CamOffsetVelocity = Vector3.zero;
  38. private Vector3 m_LastPos;
  39. private static DemoParticleSystem s_Selected;
  40. private void Awake()
  41. {
  42. Select(s_SelectedIndex);
  43. previousButton.onClick.AddListener(Previous);
  44. nextButton.onClick.AddListener(Next);
  45. }
  46. private void OnDisable()
  47. {
  48. previousButton.onClick.RemoveListener (Previous);
  49. nextButton.onClick.RemoveListener (Next);
  50. }
  51. private void Previous()
  52. {
  53. s_SelectedIndex--;
  54. if (s_SelectedIndex == -1)
  55. {
  56. s_SelectedIndex = demoParticles.items.Length - 1;
  57. }
  58. Select(s_SelectedIndex);
  59. }
  60. public void Next()
  61. {
  62. s_SelectedIndex++;
  63. if (s_SelectedIndex == demoParticles.items.Length)
  64. {
  65. s_SelectedIndex = 0;
  66. }
  67. Select(s_SelectedIndex);
  68. }
  69. private void Update()
  70. {
  71. #if !MOBILE_INPUT
  72. KeyboardInput();
  73. #endif
  74. sceneCamera.localPosition = Vector3.SmoothDamp(sceneCamera.localPosition, Vector3.forward*-s_Selected.camOffset,
  75. ref m_CamOffsetVelocity, 1);
  76. if (s_Selected.mode == Mode.Activate)
  77. {
  78. // this is for a particle system that just needs activating, and needs no interaction (eg, duststorm)
  79. return;
  80. }
  81. if (CheckForGuiCollision()) return;
  82. bool oneShotClick = (Input.GetMouseButtonDown(0) && s_Selected.mode == Mode.Instantiate);
  83. bool repeat = (Input.GetMouseButton(0) && s_Selected.mode == Mode.Trail);
  84. if (oneShotClick || repeat)
  85. {
  86. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  87. RaycastHit hit;
  88. if (Physics.Raycast(ray, out hit))
  89. {
  90. var rot = Quaternion.LookRotation(hit.normal);
  91. if (s_Selected.align == AlignMode.Up)
  92. {
  93. rot = Quaternion.identity;
  94. }
  95. var pos = hit.point + hit.normal*spawnOffset;
  96. if ((pos - m_LastPos).magnitude > s_Selected.minDist)
  97. {
  98. if (s_Selected.mode != Mode.Trail || m_Instance == null)
  99. {
  100. m_Instance = (Transform) Instantiate(s_Selected.transform, pos, rot);
  101. if (m_ParticleMultiplier != null)
  102. {
  103. m_Instance.GetComponent<ParticleSystemMultiplier>().multiplier = multiply;
  104. }
  105. m_CurrentParticleList.Add(m_Instance);
  106. if (s_Selected.maxCount > 0 && m_CurrentParticleList.Count > s_Selected.maxCount)
  107. {
  108. if (m_CurrentParticleList[0] != null)
  109. {
  110. Destroy(m_CurrentParticleList[0].gameObject);
  111. }
  112. m_CurrentParticleList.RemoveAt(0);
  113. }
  114. }
  115. else
  116. {
  117. m_Instance.position = pos;
  118. m_Instance.rotation = rot;
  119. }
  120. if (s_Selected.mode == Mode.Trail)
  121. {
  122. var emission = m_Instance.transform.GetComponent<ParticleSystem>().emission;
  123. emission.enabled = false;
  124. m_Instance.transform.GetComponent<ParticleSystem>().Emit(1);
  125. }
  126. m_Instance.parent = hit.transform;
  127. m_LastPos = pos;
  128. }
  129. }
  130. }
  131. }
  132. #if !MOBILE_INPUT
  133. void KeyboardInput()
  134. {
  135. if(Input.GetKeyDown(KeyCode.LeftArrow))
  136. Previous();
  137. if (Input.GetKeyDown(KeyCode.RightArrow))
  138. Next();
  139. }
  140. #endif
  141. bool CheckForGuiCollision()
  142. {
  143. PointerEventData eventData = new PointerEventData(eventSystem);
  144. eventData.pressPosition = Input.mousePosition;
  145. eventData.position = Input.mousePosition;
  146. List<RaycastResult> list = new List<RaycastResult>();
  147. graphicRaycaster.Raycast(eventData, list);
  148. return list.Count > 0;
  149. }
  150. private void Select(int i)
  151. {
  152. s_Selected = demoParticles.items[i];
  153. m_Instance = null;
  154. foreach (var otherEffect in demoParticles.items)
  155. {
  156. if ((otherEffect != s_Selected) && (otherEffect.mode == Mode.Activate))
  157. {
  158. otherEffect.transform.gameObject.SetActive(false);
  159. }
  160. }
  161. if (s_Selected.mode == Mode.Activate)
  162. {
  163. s_Selected.transform.gameObject.SetActive(true);
  164. }
  165. m_ParticleMultiplier = s_Selected.transform.GetComponent<ParticleSystemMultiplier>();
  166. multiply = 1;
  167. if (clearOnChange)
  168. {
  169. while (m_CurrentParticleList.Count > 0)
  170. {
  171. Destroy(m_CurrentParticleList[0].gameObject);
  172. m_CurrentParticleList.RemoveAt(0);
  173. }
  174. }
  175. instructionText.text = s_Selected.instructionText;
  176. titleText.text = s_Selected.transform.name;
  177. }
  178. [Serializable]
  179. public class DemoParticleSystem
  180. {
  181. public Transform transform;
  182. public Mode mode;
  183. public AlignMode align;
  184. public int maxCount;
  185. public float minDist;
  186. public int camOffset = 15;
  187. public string instructionText;
  188. }
  189. [Serializable]
  190. public class DemoParticleSystemList
  191. {
  192. public DemoParticleSystem[] items;
  193. }
  194. }
  195. }