-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Story #3 Add automatic generation of testScene
- Modified prefabs The generated map is a square with 3 platforms The player is automaticaly instanciated at a currently hardcoded spawn location The collisions are handled with BoxCollider2D An additional saveScene function as been added. It allows to store all the generated scene into a new file. It is then possible to reload the specific scene into unity.
- Loading branch information
Showing
10 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System.Collections; | ||
|
||
public class TestSceneGenerator : MonoBehaviour { | ||
|
||
[Tooltip("Put here Player Prefab ! ")] | ||
public Transform player; | ||
[Tooltip("Put here Player Camera Prefab ! ")] | ||
public Transform playerCamera; | ||
[Tooltip("Put here Wall Prefab ! ")] | ||
public Transform wall; | ||
|
||
private Transform environment; // Stores the roo object for the environment | ||
|
||
// Use this for initialization | ||
void Start () { | ||
|
||
/* Temporary hard coded parameters */ | ||
int width = 100; | ||
int height = 40; | ||
Vector2 spawnPosition = new Vector2 (10, 2); | ||
|
||
/* Create the root environment transform which will contain all the walls */ | ||
GameObject obj = new GameObject ("Environment"); | ||
environment = obj.GetComponent<Transform> (); | ||
|
||
/* Generate the scene */ | ||
generateSquare (width, height, Vector2.zero); | ||
generatePlatform (10, new Vector2(10, 20)); | ||
generatePlatform (10, new Vector2(25, 10)); | ||
generatePlatform (30, new Vector2(50, 25)); | ||
|
||
/* Instantiate the player */ | ||
spawnPlayer (spawnPosition); | ||
|
||
/* Save the scene to a file */ | ||
//saveGeneratedScene ("./Assets/Scene/GeneratedScene.unity"); | ||
} | ||
|
||
/** | ||
* Generate a bordered square (not filled) beginning at position | ||
*/ | ||
private void generateSquare(int width, int height, Vector2 position) { | ||
|
||
for (int i = 0; i <= width; i++) { | ||
for (int j = 0; j <= height; j++) { | ||
if(i == 0 || i == width || j == 0 || j == height) { | ||
|
||
Transform wallInstance = (Transform) Instantiate(wall, new Vector3(i + position.x, j + position.y, 0), Quaternion.identity); | ||
wallInstance.SetParent(environment); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generate Horizontal platform beginning at position | ||
*/ | ||
private void generatePlatform(int width, Vector2 position) { | ||
|
||
for (int i = 0; i <= width; i++) { | ||
Transform wallInstance = (Transform) Instantiate(wall, new Vector3(i + position.x, position.y, 0), Quaternion.identity); | ||
wallInstance.SetParent(environment); | ||
} | ||
} | ||
|
||
/** | ||
* Instantiate the player and camera at given spawn position | ||
*/ | ||
private void spawnPlayer(Vector2 spawnPosition) { | ||
Transform playerInstance = (Transform) Instantiate (player, new Vector3 (spawnPosition.x, spawnPosition.y, 0), Quaternion.identity); | ||
Transform playerCameraInstance = (Transform) Instantiate (playerCamera, new Vector3 (spawnPosition.x, spawnPosition.y, playerCamera.position.z), Quaternion.identity); | ||
|
||
playerCameraInstance.SetParent (playerInstance); | ||
} | ||
|
||
/** | ||
* Save the current scene to a scene file | ||
* Stores all previously instantiated GameObject | ||
*/ | ||
private void saveGeneratedScene(string path) { | ||
// path example : "./Assets/Scene/test.unity" | ||
EditorApplication.SaveScene (path); | ||
} | ||
} | ||
|
Binary file not shown.
Binary file not shown.