//Sets up the outer walls and floor (background) of the game board. void BoardSetup() { //Instantiate Board and set boardHolder to its transform. boardHolder = new GameObject("Board").transform; //Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles. for (int x = m_MinColumns; x < columns + 1; x++) { //Loop along y axis, starting from -1 to place floor or outerwall tiles. for (int y = m_MinRows; y < rows + 1; y++) { //Choose a random tile from our array of floor tile prefabs and prepare to instantiate it. GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)]; Tuile t_tuile = toInstantiate.GetComponent <Tuile>(); t_tuile.x = (uint)(x + Math.Abs(m_MinColumns)); t_tuile.y = (uint)(y + Math.Abs(m_MinRows)); //Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles. if (x == m_MinColumns || x == columns && y > m_MinRows || y > rows) { toInstantiate = outerWallTiles[0]; } if (x < m_MinColumns || x < columns && y == m_MinRows || y == rows) { toInstantiate = outerWallTiles[1]; } if (x == m_MinColumns && y == rows) { toInstantiate = outerWallTiles[3]; } if (x == m_MinColumns && y == m_MinRows) { toInstantiate = outerWallTiles[5]; } if (x == columns && y == rows) { toInstantiate = outerWallTiles[2]; } if (x == columns && y == m_MinRows) { toInstantiate = outerWallTiles[4]; } //Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject. GameObject instance = Instantiate(toInstantiate, grille.GridToWorld(new Vector2Int((int)t_tuile.x, (int)t_tuile.y)), Quaternion.identity) as GameObject; //Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy. instance.transform.SetParent(boardHolder); } } }
private void OnSceneGUI() { // Si on (control + click) if (Event.current.type == EventType.MouseDown && Event.current.control) { // Pour prévenir la sélection par défaut, on génère un ID d'objet non sélectionnable GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive); // Trouver la position dans la grille où était la souris lors de l'event Vector3 t_ClickPosition = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin; Vector2Int t_GrillePos = m_Grille.WorldToGrid(t_ClickPosition); // Trouve l'ID de la tuile à instancier int t_SelectedTileID = serializedObject.FindProperty("m_SelectedTileID").intValue; // Trouver la taille de notre array de tuiles disponibles SerializedProperty t_TileArray = serializedObject.FindProperty("m_AvailableTiles"); int t_TileArraySize = t_TileArray.arraySize; // Si l'ID de tuile est invalide if (t_SelectedTileID >= t_TileArraySize) { throw new GrilleException("Selected Tile Out of bounds."); } // Trouver la référence vers le prefab à instancier GameObject t_TilePrefab = (GameObject)t_TileArray.GetArrayElementAtIndex(t_SelectedTileID).objectReferenceValue; // Trouver les coordonnées du centre de la cellule Vector3 t_CellCenter = m_Grille.GridToWorld(t_GrillePos); // Instancier la tuile en tant que prefab, parentée avec grid GameObject t_Tile = (GameObject)PrefabUtility.InstantiatePrefab(t_TilePrefab, m_Grille.transform); //On le place au centre de la cellule t_Tile.transform.position = t_CellCenter; if (t_GrillePos.x >= m_ColumnCount || t_GrillePos.y >= m_Rowcount) { RefreshTilesArray(); } if (m_Tuiles[t_GrillePos.x, t_GrillePos.y] != null) { DestroyImmediate(m_Tuiles[t_GrillePos.x, t_GrillePos.y].gameObject); } m_Tuiles[t_GrillePos.x, t_GrillePos.y] = t_Tile.GetComponent <Tuile>(); m_Tuiles[t_GrillePos.x, t_GrillePos.y].x = (uint)t_GrillePos.x; m_Tuiles[t_GrillePos.x, t_GrillePos.y].y = (uint)t_GrillePos.y; float t_CellSize = serializedObject.FindProperty("m_CellSize").floatValue; Sprite t_Sprite = t_Tile.GetComponent <SpriteRenderer>().sprite; float t_Scale = t_CellSize / (t_Sprite.rect.width / t_Sprite.pixelsPerUnit); t_Tile.transform.localScale = new Vector3(t_Scale, t_Scale, t_Scale); } }