-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScalableGrid.cs
More file actions
178 lines (147 loc) · 4.58 KB
/
ScalableGrid.cs
File metadata and controls
178 lines (147 loc) · 4.58 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
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class ScalableGrid : MonoBehaviour
{
//define cell sizes and cell gap
[SerializeField]
private Vector3 CellSize = Vector3.one;
[SerializeField]
private Vector3 CellPadding = Vector3.one;
public Vector3 ColliderBounds
{
get
{
return GetComponent<BoxCollider>().bounds.size;
}
}
private Vector3 ColliderCenter
{
get
{
return GetComponent<BoxCollider>().bounds.center;
}
}
private int TileZAmount
{
get
{
float val = CellSize.z + CellPadding.z;
return (int)(ColliderBounds.z / val);
}
}
private int TileYAmount
{
get
{
float val = CellSize.y + CellPadding.y;
return (int)(ColliderBounds.y / val);
}
}
public VRGridData VRGrid;
public void SetupGrid(int x, int y, GameObject[] objects)
{
if(x < 0 || y < 0) { return; }
VRGrid = new VRGridData(x, y, objects);
}
public void SetGridItem(GameObject cell, Vector2Int location = default)
{
if(location == default)
{
//add cell as child
cell.transform.parent = transform;
//create new grid
GameObject[] children = new GameObject[transform.childCount - 1];
for (int i = 0; i < transform.childCount - 1; i++)
{
children[i] = transform.GetChild(i).gameObject;
}
//setup grid again
SetupGrid(TileZAmount, TileYAmount, children);
}
else
{
VRGrid.tiles[location.x * location.y] = cell;
}
}
private void Awake()
{
GameObject[] children = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i ++)
{
children[i] = transform.GetChild(i).gameObject;
}
SetupGrid(TileZAmount, TileYAmount, children);
SetObjects();
}
public void Start()
{
}
public void FixedUpdate()
{
//debugging
SetObjects();
}
private void SetObjects()
{
// Find center of the collider
Vector3 center = ColliderCenter;
// Get the extents (half size) of the collider
Vector3 extentsMin = GetComponent<BoxCollider>().bounds.min;
Debug.Log(extentsMin);
// Calculate starting area
Vector3 placementPosition = new Vector3(0f, extentsMin.y + CellPadding.y, extentsMin.z + (CellPadding.z * 2));
Vector3 sizeOfCell = CellSize + CellPadding;
int counter = 0;
// Loop through each cell position in the grid
for (int y = TileYAmount - 1; y >= 0 && counter < VRGrid.tiles.Length; y--)
{
for (int z = 0; z < TileZAmount && counter < VRGrid.tiles.Length; z++)
{
Vector3 setPlacement = new Vector3(0f, y * sizeOfCell.y, z * sizeOfCell.z);
// Place a child object at the calculated position
VRGrid.tiles[counter].transform.position = placementPosition + setPlacement;
//confirms if the objects can be enabled or disabled
if (!VRGrid.tiles[counter].gameObject.activeSelf && IsPositionWithinCollider(VRGrid.tiles[counter].transform.position))
{
VRGrid.tiles[counter].gameObject.SetActive(true);
}
counter++;
// Break out of the loop if counter exceeds tile length
if (counter >= VRGrid.tiles.Length)
break;
}
}
// Disable remaining objects
for (int i = counter; i < VRGrid.tiles.Length; i++)
{
VRGrid.tiles[i].gameObject.SetActive(false);
}
}
private bool IsPositionWithinCollider(Vector3 position)
{
Collider collider = GetComponent<Collider>();
if (collider != null)
{
return collider.bounds.Contains(position);
}
return false;
}
public struct VRGridData
{
[SerializeField]
public GameObject[] tiles;
[SerializeField]
public Vector2Int gridSize;
public VRGridData(int x, int y)
{
tiles = new GameObject[x * y];
gridSize = new Vector2Int(x, y);
}
public VRGridData(int x, int y, GameObject[] objects)
{
//tiles are organised in the Grid
tiles = objects;
gridSize = new Vector2Int(x, y);
}
}
}