Commit 1afde5bb authored by BRUNEAU Julien's avatar BRUNEAU Julien
Browse files

+ Add starting menu allowing to choose which scenario (config file) to load

parent d76a36a8
......@@ -23,6 +23,7 @@
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// Main manager: Load the scene, create all the agents and manage users input
......@@ -31,12 +32,11 @@ public class LoadEnv : MonoBehaviour
{
#region attributes
private string rootFolder = "Assets/Resources/CSV/"; // Folder containing trajectory files
GameObject[] avatars; // List of agents
List<GameObject> avatars; // List of agents
public GameObject cam; // Scene camera
private CamRecorder cam_Rec; // Control scene recording
private CamMvt cam_Movement; // Control camera movement behavior
private MenuManager menuM;
//private float rotSpeed = 5;
// --------------------------
......@@ -54,6 +54,19 @@ public class LoadEnv : MonoBehaviour
/// Scene and agents initialization
/// </summary>
void Start()
{
avatars = new List<GameObject>();
cam_Rec = cam.GetComponent<CamRecorder>();
cam_Movement = cam.GetComponent<CamMvt>();
cam_Rec.enabled = false;
cam_Movement.enabled = false;
menuM = gameObject.GetComponent<MenuManager>();
}
public void loadScenario(string trajDir)
{
// --------------------------------------------------
// SET RANDOM SEED TO HAVE SAME RESULTS AT EVERY RUNS
......@@ -61,19 +74,18 @@ public class LoadEnv : MonoBehaviour
// ---------------------------------
// INITIALIZE SCENE FROM CONFIG FILE
cam_Rec = cam.GetComponent<CamRecorder>();
cam_Movement = cam.GetComponent<CamMvt>();
rootFolder = ConfigReader.trajectoriesDir;
// -------------
// CAMERA CONFIG
cam_Rec.enabled = true;
cam_Movement.enabled = true;
cam.transform.position = ConfigReader.camPosition;
cam.transform.rotation = Quaternion.Euler(ConfigReader.camRotation);
cam_Movement.lookAt_Id = ConfigReader.camLookAtTarget;
cam_Movement.follow_Id = ConfigReader.camFollowTarget;
cam_Movement.follow_LockX = !ConfigReader.camFollowOnX;
cam_Movement.follow_LockZ = !ConfigReader.camFollowOnY;
// -------------
// RECORD CONFIG
cam_Rec.record = ConfigReader.recording;
......@@ -86,9 +98,17 @@ public class LoadEnv : MonoBehaviour
else
cam_Rec.saveDir = ConfigReader.recordingSaveDir;
// ---------------------
// CLEAR PREVIOUS AGENTS
foreach (GameObject a in avatars)
{
Destroy(a);
}
avatars.Clear();
// -------------
// CREATE AGENTS
DirectoryInfo dir = new DirectoryInfo(rootFolder);
DirectoryInfo dir = new DirectoryInfo(trajDir);
FileInfo[] info = dir.GetFiles("*.csv");
if (info.Length == 0)
info = dir.GetFiles("*.txt");
......@@ -113,16 +133,16 @@ public class LoadEnv : MonoBehaviour
// -----------------
// SET AGENT'S COLOR
Color tmpColor = new Color(Random.value, Random.value, Random.value, 1);
if (ConfigReader.agentsColor!=null && ConfigReader.agentsColor.Count>0)
if (ConfigReader.agentsColor != null && ConfigReader.agentsColor.Count > 0)
{
foreach (ConfigAgentColor c in ConfigReader.agentsColor)
{
if (i>c.firstAgent-1 && i<c.lastAgent+1 && (i-c.firstAgent)%c.step==0)
if (i > c.firstAgent - 1 && i < c.lastAgent + 1 && (i - c.firstAgent) % c.step == 0)
{
tmpColor = new Color(c.red, c.green, c.blue, 1);
}
}
}
}
SkinnedMeshRenderer tmpRenderer = temp.GetComponentInChildren<SkinnedMeshRenderer>();
Material tmpMaterial = new Material(Shader.Find("Legacy Shaders/Diffuse"));
tmpMaterial.color = tmpColor;
......@@ -138,7 +158,8 @@ public class LoadEnv : MonoBehaviour
{
path = "male/prefab_light/LS_m";
modelId = i / 2;
}else
}
else
{
path = "female/prefab_light/LS_f";
modelId = (i - 1) / 2;
......@@ -181,6 +202,7 @@ public class LoadEnv : MonoBehaviour
}
}
}
avatars.Add(temp);
// ----------------------------------
// FOLLOWTRAJECTORY SCRIPT MANAGEMENT
......@@ -193,16 +215,22 @@ public class LoadEnv : MonoBehaviour
}
}
private void Update()
{
ConfigReader.camPosition = cam.transform.position;
ConfigReader.camRotation = cam.transform.rotation.eulerAngles;
}
/// <summary>
/// Manage users Input
/// </summary>
void Update()
void LateUpdate()
{
// -------------------
// ESCAPE => EXIT APPS
if (Input.GetKey(KeyCode.Escape) == true)
if (Input.GetKeyDown(KeyCode.Escape) == true)
{
Application.Quit();
menuM.toogleMenu();
}
// -------------------------------
......@@ -271,11 +299,7 @@ public class LoadEnv : MonoBehaviour
// F5 => SAVE CAMERA POSITION
if (Input.GetKeyDown(KeyCode.F5))
{
ConfigReader.camPosition = cam.transform.position;
ConfigReader.camRotation = cam.transform.rotation.eulerAngles;
ConfigReader.CreateXML();
menuM.saveConfig();
}
// ----------------------
......
fileFormatVersion: 2
guid: 3f97242106d28474a9317bf19c10b3d3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class MenuManager : MonoBehaviour {
string configPath;
Camera cam;
InputField inputConfigPath;
Dropdown configFilesMenu;
GameObject menu;
// Use this for initialization
void Start () {
configPath = null;
cam = Camera.main;
menu=GameObject.FindGameObjectWithTag("Menu");
string dataPath = defaultScenarioPath();
inputConfigPath = menu.GetComponentInChildren<InputField>();
inputConfigPath.text = dataPath;
configFilesMenu = menu.GetComponentInChildren<Dropdown>();
updateConfigList(dataPath);
Time.timeScale = 0;
}
public string defaultScenarioPath()
{
string pathPlayer = Application.dataPath;
int lastIndex = pathPlayer.LastIndexOf('/');
string dataPath = pathPlayer.Remove(lastIndex, pathPlayer.Length - lastIndex);
string scenarioPath = dataPath + "/Scenarios";
if (Directory.Exists(scenarioPath))
return scenarioPath;
return dataPath;
}
public void updateConfigList(string NewPath)
{
if (configFilesMenu == null)
return;
DirectoryInfo dir;
if (Directory.Exists(inputConfigPath.text))
dir = new DirectoryInfo(inputConfigPath.text);
else
{
string dataPath = defaultScenarioPath();
dir = new DirectoryInfo(dataPath);
}
FileInfo[] infos = dir.GetFiles("*.xml");
configFilesMenu.options.Clear();
configFilesMenu.options.Add(new Dropdown.OptionData("NONE"));
foreach (FileInfo i in infos)
{
configFilesMenu.options.Add(new Dropdown.OptionData(i.Name.Remove(i.Name.Length-4)));
}
configFilesMenu.value = 0;
configFilesMenu.RefreshShownValue();
}
public void updateConfigFile(int i)
{
if (configFilesMenu == null || configFilesMenu.value == 0)
return;
configPath = inputConfigPath.text + '\\' + configFilesMenu.options[configFilesMenu.value].text + ".xml";
//cam.transform.position = ConfigReader.camPosition;
//cam.transform.rotation = Quaternion.Euler(ConfigReader.camRotation);
}
// Update is called once per frame
void Update () {
}
public void startScenario()
{
if (configPath == null || configFilesMenu.value == 0)
return;
ConfigReader.loadConfig(configPath);
LoadEnv env =gameObject.GetComponent<LoadEnv>();
env.loadScenario(ConfigReader.trajectoriesDir);
menu.SetActive(false);
}
public void toogleMenu()
{
menu.SetActive(!menu.activeSelf);
}
public void saveConfig()
{
ConfigReader.CreateXML(configPath);
}
public void exit()
{
Application.Quit();
}
}
fileFormatVersion: 2
guid: d678a9f002f1dee4da6aa37341e90a91
timeCreated: 1538729447
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
<?xml version="1.0" encoding="utf-8"?>
<ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<env_filesPath>.\TrajExample\ExampleTwoColor\</env_filesPath>
<cam>
<position x="33" y="1.25" z="3.8" />
<rotation x="30" y="0" z="-90" />
<lookAtAgent agentID="-1" />
<followAgent agentID="267" followOnX="true" followOnY="false" />
</cam>
<recording start="0" end="0" framerate="15">
<saveDir>.\Output\</saveDir>
</recording>
<?xml version="1.0" encoding="utf-8"?>
<ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<env_filesPath>.\Scenarios\ExampleLookAt\</env_filesPath>
<cam>
<position x="-28" y="-7" z="5" />
<rotation x="30.0000038" y="0" z="90" />
<lookAtAgent agentID="-1" />
<followAgent agentID="0" followOnX="true" followOnY="false" />
</cam>
<recording start="0" end="0" framerate="15">
<saveDir>.\Output\</saveDir>
</recording>
<AgentColorList />
</ConfigData>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Specify the directory with all the trajectory files -->
<env_filesPath>.\TrajExample\ExampleLookAt\</env_filesPath>
<env_filesPath>.\Scenarios\ExampleLookAt\</env_filesPath>
<!-- Camera positioning -->
<cam>
<position x="15" y="2" z="5" />
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment