This repository was archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathExampleUnityRandom.cs
More file actions
213 lines (189 loc) · 5.25 KB
/
ExampleUnityRandom.cs
File metadata and controls
213 lines (189 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using UnityEngine;
using System.Collections;
public class ExampleUnityRandom : MonoBehaviour
{
private UnityRandom urand;
private bool rotate = false;
public float sphereRadius = 20.0f;
public int max_objects = 1000;
public int seed = 123456;
/* LateUpdate is called after all Update functions have been called. */
void LateUpdate ()
{
if (Input.GetButtonDown("Jump")) {
rotate = !rotate;
ToggleRotation();
}
}
private void ToggleRotation()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.GetComponent<RotateAround>().rotating = rotate;
}
}
// GUI
void OnGUI()
{
GUILayout.BeginVertical("box");
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("UNITYRANDOM POINT EXAMPLES");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("SET A SEED: " + seed);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
// I cut the SEED space for the GUI
// Because the Slider get crazy with Int32.MaxValue (it goes back to Int.32.MinValue)
seed = (int) GUILayout.HorizontalSlider(seed, 0, UnityRandom.max_seed - 1000);
if (GUILayout.Button("IN SPHERE SURFACE")) ExampleInSpheres();
if (GUILayout.Button("IN CIRCLE")) ExampleInCircle();
if (GUILayout.Button("IN DISK")) ExampleInDisk();
if (GUILayout.Button("IN SPHERE VOLUME")) ExampleInSphere();
if (GUILayout.Button("IN CUBE")) ExampleInCube();
if (GUILayout.Button("ON CUBE")) ExampleOnCube();
if (GUILayout.Button("ON CAP")) ExampleOnCap();
if (GUILayout.Button("ON RING")) ExampleOnRing();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("TOGGLE ROTATION WITH JUMP");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
private void ExampleOnRing()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointOnRing(20.0f, 30.0f));
}
}
private void ExampleOnCap()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointOnCap(30.0f));
}
}
// INIT a new UnityRandom object with a random seed
private void InitRandom()
{
urand = new UnityRandom(seed);
}
private void ExampleInCircle()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointInACircle());
}
}
private void ExampleInDisk()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointInADisk());
}
}
private void ExampleInSphere()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointInASphere());
}
}
private void ExampleInCube()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointInACube());
}
}
private void ExampleOnCube()
{
InitRandom();
CleanUp();
MakeObjects();
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
item.transform.position = ScalePosition(urand.PointOnACube());
}
}
private void ExampleInSpheres()
{
InitRandom();
CleanUp();
MakeObjects();
}
private void CleanUp()
{
// Clear all
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject item in objs)
{
Destroy(item);
}
}
private void MakeObjects()
{
// CENTER OF THE SPHERE: x = 0, y = 20, z = 0 I generate an object there
GameObject _pivot = new GameObject();
_pivot.transform.position = new Vector3(0,sphereRadius,0);
for (int i = 0; i < max_objects; i++)
{
// WE make a small sphere
GameObject _sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
// WE SCALE THE SPHERE
_sphere.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
// TAKE A SCALED RANDOM POSITION
Vector3 pos = ScalePosition(urand.PointOnASphere());
// WE set the sphere position
_sphere.transform.position = pos;
// WE give a tag to the sphere
_sphere.tag = "Player";
// Attach the rotation script
RotateAround _ra = _sphere.AddComponent<RotateAround>();
// SET the PIVOT POINT IN SCRIPT
_ra.pivotPoint = _pivot.transform;
// SET the MATERIAL as a RANDOM COLOR
_sphere.renderer.material.color = urand.Rainbow();
}
}
// CAN BE ADDED IN THE LIBRARY??? FIXME
private Vector3 ScalePosition(Vector3 pos)
{
// WE scale TO 20,20,20
pos = Vector3.Scale(pos, new Vector3(sphereRadius,sphereRadius,sphereRadius));
// WE move UP
pos = pos + new Vector3(0,sphereRadius,0);
return pos;
}
}