-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from tier4/feature/autoware-simualtion-config
Implemented AutowareSimulation.cs
- Loading branch information
Showing
10 changed files
with
245 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
Assets/AWSIM/Scenes/Main/AutowareSimualtion/AutowareSimulation.cs
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,53 @@ | ||
using UnityEngine; | ||
using System; | ||
using AWSIM.TrafficSimulation; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// Script for Scene. By Scirpt-Execution-Order, This class is called earlier than other MonoBehaviour. | ||
/// Enables configuration at scene startup. | ||
/// </summary> | ||
public class AutowareSimulation : MonoBehaviour | ||
{ | ||
[SerializeField] TrafficManager trafficManager; | ||
|
||
[Header("Player Config")] | ||
[SerializeField] string commandLineConfigParam = "--json_path"; | ||
|
||
[Header("Editor Debug")] | ||
[SerializeField] bool useJsonConfig; | ||
|
||
[Tooltip("Specify this Path if you want to debug Json loading at Unity Editor runtime.")] | ||
[SerializeField] string jsonPath; | ||
|
||
[Serializable] | ||
public class Configuration | ||
{ | ||
public float TimeScale; // Reflected in Time.timeScale | ||
public int RandomTrafficSeed; // Reflected in TrafficManager.seed | ||
public int MaxVehicleCount; // Reflected in TrafficManager.maxVehicleCount | ||
} | ||
|
||
void Awake() | ||
{ | ||
|
||
#if !UNITY_EDITOR | ||
// initialize | ||
useJsonConfig = false; | ||
jsonPath = ""; | ||
|
||
// For player, if path is not specified as a command line argument, json is not read. | ||
useJsonConfig = CommandLineUtility.GetCommandLineArg(out jsonPath, commandLineConfigParam); | ||
#endif | ||
|
||
if (useJsonConfig) | ||
{ | ||
var config = CommandLineUtility.LoadJsonFromPath<Configuration>(jsonPath); | ||
Time.timeScale = config.TimeScale; | ||
trafficManager.seed = config.RandomTrafficSeed; | ||
trafficManager.maxVehicleCount = config.MaxVehicleCount; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/AWSIM/Scenes/Main/AutowareSimualtion/AutowareSimulation.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
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,5 @@ | ||
{ | ||
"TimeScale": 0.2, | ||
"RandomTrafficSeed": 33, | ||
"MaxVehicleCount": 2 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,73 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using System.IO; | ||
using System; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// Convenience static class for command line. | ||
/// </summary> | ||
public static class CommandLineUtility | ||
{ | ||
/// <summary> | ||
/// Get instance from Json by specifying Path. | ||
/// </summary> | ||
/// <typeparam name="T">Class to be converted from json.</typeparam> | ||
/// <param name="path">Path where Json is located.</param> | ||
/// <returns></returns> | ||
public static T LoadJsonFromPath<T>(string path) | ||
{ | ||
try | ||
{ | ||
string rawContent = File.ReadAllText(path); | ||
T content = JsonUtility.FromJson<T>(rawContent); | ||
return content; | ||
} | ||
catch (Exception exceptipn) | ||
{ | ||
Debug.LogError(exceptipn.Message); | ||
return default; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get the corresponding command line argument. | ||
/// </summary> | ||
/// <param name="param">Parameter</param> | ||
/// <returns></returns> | ||
public static string GetCommandLineArg(string param) | ||
{ | ||
var cmdArgs = System.Environment.GetCommandLineArgs(); | ||
for (int i = 0; i < cmdArgs.Length; i++) | ||
{ | ||
if (cmdArgs[i] == param && cmdArgs.Length > i + 1) | ||
{ | ||
return cmdArgs[i + 1]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Get the corresponding command line argument. | ||
/// </summary> | ||
/// <param name="arg">Argument</param> | ||
/// <param name="param">Parameter</param> | ||
/// <returns></returns> | ||
public static bool GetCommandLineArg(out string arg, string param) | ||
{ | ||
var _arg = GetCommandLineArg(param); | ||
if (_arg != null) | ||
{ | ||
arg = _arg; | ||
return true; | ||
} | ||
|
||
arg = ""; | ||
return false; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.