-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilityScripts.cs
More file actions
118 lines (90 loc) · 3.68 KB
/
UtilityScripts.cs
File metadata and controls
118 lines (90 loc) · 3.68 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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UtilityScripts : MonoBehaviour
{
//Generic utility methods
/// <summary>
/// Method to find a child object.
/// </summary>
/// <param parent object to search="parentObject"></param>
/// <param name of child object to find="objectName"></param>
/// <returns>Child GameObject if found or null</returns>
public GameObject getChildGameObject(GameObject parentObject, string objectName)
{
Transform[] transforms = parentObject.transform.GetComponentsInChildren<Transform>(true);
foreach (Transform t in transforms) if (t.gameObject.name == objectName) return t.gameObject;
return null;
}
/// <summary>
/// Method to find the index of an item contained in a 2D array and return it in an array.
/// Access index with var = GetIndex()[0]; where 0 = x index and 1 = y index.
/// </summary>
/// <param name of array containing item="arrayName"></param>
/// <param item to find the index of="searchItem"></param>
/// <returns>An array containing the x and y values of the item's index</returns>
public int[] GetIndex(GameObject[,] arrayName, GameObject searchItem)
{
int[] index = new int[2];
//Iterate through array until gameobject matches search item
for (int x = 0; x < arrayName.GetLength(0); x++)
{
for (int y = 0; y < arrayName.GetLength(1); y++)
{
if (arrayName[x, y] == searchItem)
{
index[0] = x;
index[1] = y;
break;
}
}
}
return index;
}
/// <summary>
/// Method to deep copy a 2D array of GameObjects.
/// </summary>
/// <param the original array to be copied ="arrayToCopy"></param>
/// <returns>Returns 2Darray arrayToPaste</returns>
public GameObject[,] DeepCopy2DArray(GameObject[,] arrayToCopy)
{
int copyLength = arrayToCopy.GetLength(0);
int copyHeight = arrayToCopy.GetLength(1);
GameObject[,] arrayToPaste = new GameObject[copyLength, copyHeight];
//Iterate through original array in both directions
for (int x = 0; x < copyLength; x++)
{
for (int y = 0; y < copyHeight; y++)
{
//Instantiate new cell at each point in the array
arrayToPaste[x, y] = GameObject.Instantiate(arrayToCopy[x, y]);
//Update the gameStates of the cells to match the original array
arrayToPaste[x, y].GetComponent<CellStateScript>().gameState = arrayToCopy[x, y].GetComponent<CellStateScript>().gameState;
}
}
//Return 2D array
return arrayToPaste;
}
/// <summary>
/// Method to deep copy a 2D array of integers.
/// </summary>
/// <param the original array to be copied ="arrayToCopy"></param>
/// <returns>Returns 2Darray arrayToPaste</returns>
public int[,] DeepCopy2DArrayInt(int[,] arrayToCopy)
{
int copyLength = arrayToCopy.GetLength(0);
int copyHeight = arrayToCopy.GetLength(1);
int[,] arrayToPaste = new int[copyLength, copyHeight];
//Iterate through original array in both directions
for (int x = 0; x < copyLength; x++)
{
for (int y = 0; y < copyHeight; y++)
{
//copy each value in the array
arrayToPaste[x, y] = arrayToCopy[x, y];
}
}
//Return 2D array
return arrayToPaste;
}
}