AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SimpleGridGenerator.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.InputModule.Tests
8 {
9  public class SimpleGridGenerator : MonoBehaviour
10  {
11  private const int DefaultRows = 3;
12  private const int DefaultColumns = 3;
13 
14  [Tooltip("Number of rows in the grid.")]
15  public int Rows = DefaultRows;
16 
17  [Tooltip("Number of columns in the grid.")]
18  public int Columns = DefaultColumns;
19 
20  [Tooltip("Distance between objects in the grid.")]
21  public float ObjectSpacing = 1.0f;
22 
23  [Tooltip("Array of object prefabs to instantiate on the grid.")]
24  public List<GameObject> ObjectPrefabs;
25 
26  [Tooltip("Indicates whether to generate grid on component start.")]
27  public bool GenerateOnStart = true;
28 
29  private void Start()
30  {
31  if (GenerateOnStart)
32  {
33  GenerateGrid();
34  }
35  }
36 
37  public void GenerateGrid()
38  {
39  float startX = -0.5f * ObjectSpacing * (Rows - 1);
40  float startY = -0.5f * ObjectSpacing * (Columns - 1);
41  for (int i = 0; i < Rows; i++)
42  {
43  for (int j = 0; j < Columns; j++)
44  {
45  GameObject prefab = GetRandomPrefab();
46  Vector3 pos = new Vector3(startX + i * ObjectSpacing, startY + j * ObjectSpacing, 0.0f);
47  GameObject go = Instantiate(prefab, pos, Quaternion.identity) as GameObject;
48  go.transform.SetParent(transform, false);
49  }
50  }
51  }
52 
53  private GameObject GetRandomPrefab()
54  {
55  if (ObjectPrefabs.Count > 0)
56  {
57  int index = Random.Range(0, ObjectPrefabs.Count);
58  return ObjectPrefabs[index];
59  }
60  return null;
61  }
62  }
63 }