Example #1
0
        /// <summary>
        /// Instantiate a cell with a prefab as model.
        /// </summary>
        /// <param name="prefab">The prefab to instantiate as a cell.</param>
        /// <param name="grid">The grid the cell will belongs.</param>
        /// <param name="index"> The index of the cell.</param>
        /// <returns>The cell component associated to the gameobject.</returns>
        public static Cell InstantiateCell(GameObject prefab, Grid3D grid, Vector3Int index)
        {
            GameObject gameObject = PrefabUtility.InstantiatePrefab(prefab, grid.transform) as GameObject;

            gameObject.name = index.x + "_" + index.y + "_" + index.z + "_" + gameObject.name;

            Cell cell = gameObject.GetComponent <Cell>();

            if (cell == null)
            {
                cell = gameObject.AddComponent <Cell>();
            }
            grid.AddCell(index, cell);
            cell.ResetTransform();
            Undo.RegisterCreatedObjectUndo(cell.gameObject, "Cell created");
            return(cell);
        }