1 using UnityEngine;
2 using
System.Collections;
3
4 public
class DetonatorTest : MonoBehaviour
5 {
6     
public GameObject currentDetonator;
7     
private int _currentExpIdx = -1;
8     
public GameObject[] detonatorPrefabs;
9     
public float explosionLife = 10;
10     
public float timeScale = 1.0f;
11     
public float detailLevel = 1.0f;
12
13     
public GameObject wall;
14     
private GameObject _currentWall;
15     
private float _spawnWallTime = -1000;
16     
private Rect _guiRect;
17
18     
private void Start()
19     {
20         SpawnWall();
21         
if (!currentDetonator) NextExplosion();
22         
else _currentExpIdx = 0;
23     }
24
25     
private void OnGUI()
26     {
27         _guiRect =
new Rect(7, Screen.height - 180, 250, 200);
28         GUILayout.BeginArea(_guiRect);
29
30         GUILayout.BeginVertical();
31         
string expName = currentDetonator.name;
32         
if (GUILayout.Button(expName + " (Click For Next)"))
33         {
34             NextExplosion();
35         }
36         
if (GUILayout.Button("Rebuild Wall"))
37         {
38             SpawnWall();
39         }
40         
if (GUILayout.Button("Camera Far"))
41         {
42             Camera.main.transform.position =
new Vector3(0, 0, -7);
43             Camera.main.transform.eulerAngles =
new Vector3(13.5f, 0, 0);
44         }
45         
if (GUILayout.Button("Camera Near"))
46         {
47             Camera.main.transform.position =
new Vector3(0, -8.664466f, 31.38269f);
48             Camera.main.transform.eulerAngles =
new Vector3(1.213462f, 0, 0);
49         }
50
51         GUILayout.Label(
"Time Scale");
52         timeScale = GUILayout.HorizontalSlider(timeScale,
0.0f, 1.0f);
53
54         GUILayout.Label(
"Detail Level (re-explode after change)");
55         detailLevel = GUILayout.HorizontalSlider(detailLevel,
0.0f, 1.0f);
56
57         GUILayout.EndVertical();
58         GUILayout.EndArea();
59     }
60
61     
private void NextExplosion()
62     {
63         
if (_currentExpIdx >= detonatorPrefabs.Length - 1) _currentExpIdx = 0;
64         
else _currentExpIdx++;
65         currentDetonator = detonatorPrefabs[_currentExpIdx];
66     }
67
68     
private void SpawnWall()
69     {
70         
if (_currentWall) Destroy(_currentWall);
71         _currentWall = (GameObject) Instantiate(wall,
new Vector3(-7, -12, 48), Quaternion.identity);
72
73         _spawnWallTime = Time.time;
74     }
75
76     
//is this a bug? We can't use the same rect for placing the GUI as for checking if the mouse contains it...
77     
private Rect checkRect = new Rect(0, 0, 260, 180);
78
79     
private void Update()
80     {
81         
//keeps the UI in the corner in case of resize...
82         _guiRect =
new Rect(7, Screen.height - 150, 250, 200);
83
84         
//keeps the play button from making an explosion
85         
if ((Time.time + _spawnWallTime) > 0.5f)
86         {
87             
//don't spawn an explosion if we're using the UI
88             
if (!checkRect.Contains(Input.mousePosition))
89             {
90                 
if (Input.GetMouseButtonDown(0))
91                 {
92                     SpawnExplosion();
93                 }
94             }
95             Time.timeScale = timeScale;
96         }
97     }
98
99     
private void SpawnExplosion()
100     {
101         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
102         RaycastHit hit;
103         
if (Physics.Raycast(ray, out hit, 1000))
104         {
105             Detonator dTemp = (Detonator)currentDetonator.GetComponent(
"Detonator");
106
107             
float offsetSize = dTemp.size/3;
108             Vector3 hitPoint = hit.point +
109                                       ((Vector3.Scale(hit.normal,
new Vector3(offsetSize, offsetSize, offsetSize))));
110             GameObject exp = (GameObject) Instantiate(currentDetonator, hitPoint, Quaternion.identity);
111             dTemp = (Detonator)exp.GetComponent(
"Detonator");
112             dTemp.detail = detailLevel;
113
114             Destroy(exp, explosionLife);
115         }
116
117
118     }
119 }


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