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


Gõ tìm kiếm nhanh...