/// <summary> /// Instantiates selected prefab in the unique relative path /// </summary> /// <param name="prefabId">Id of instantiated prefab in <see cref="NetworkSettings.DistributedObjectPrefabs"/></param> /// <param name="relativePath">Relative path to the root where object will be created, can be changed if object name is not unique</param> /// <returns>Instantiated new distributed object</returns> /// <exception cref="ArgumentException">Invalid configuration of the instantiated prefab</exception> private DistributedObject InstantiatePrefab(int prefabId, string relativePath) { if (prefabId < 0 || prefabId >= Settings.DistributedObjectPrefabs.Length) { throw new ArgumentException( $"Prefab of distributed object with id {prefabId} is not defined in {typeof(NetworkSettings).Name}."); } var distributedObjectParent = HierarchyUtilities.GetOrCreateChild(transform, relativePath); var newGameObject = Instantiate(Settings.DistributedObjectPrefabs[prefabId], distributedObjectParent); HierarchyUtilities.ChangeToUniqueName(newGameObject); var distributedObject = newGameObject.GetComponent <DistributedObject>(); if (distributedObject == null) { throw new ArgumentException( $"Prefab of distributed object with id {prefabId} has no {typeof(DistributedObject).Name} component in the root game object."); } instantiatedObjects.Add(new InstantiatedObjectData(prefabId, distributedObject)); return(distributedObject); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; var agentController = go.GetComponent <AgentController>(); agentController.SensorsChanged += AgentControllerOnSensorsChanged; agentController.Config = config; agentController.Config.AgentGO = go; SIM.LogSimulation(SIM.Simulation.VehicleStart, config.Name); ActiveAgents.Add(agentController.Config); agentController.GTID = ++SimulatorManager.Instance.GTIDs; agentController.Config.GTID = agentController.GTID; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (config.Connection != null) { var split = config.Connection.Split(':'); if (split.Length != 2) { throw new Exception("Incorrect bridge connection string, expected HOSTNAME:PORT"); } bridgeClient.Connect(split[0], int.Parse(split[1])); } } SIM.LogSimulation(SIM.Simulation.BridgeTypeStart, config.Bridge != null ? config.Bridge.Name : "None"); var sensorsController = go.AddComponent <SensorsController>(); agentController.AgentSensorsController = sensorsController; sensorsController.SetupSensors(config.Sensors); //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); if (network.IsClient) { //Disable controller and dynamics on clients so it will not interfere mocked components agentController.enabled = false; var vehicleDynamics = agentController.GetComponent <IVehicleDynamics>() as MonoBehaviour; if (vehicleDynamics != null) { vehicleDynamics.enabled = false; } } //Change the simulation type only if it's not set in the prefab var distributedRigidbody = go.GetComponent <DistributedRigidbody>(); if (distributedRigidbody == null) { distributedRigidbody = go.AddComponent <DistributedRigidbody>(); distributedRigidbody.SimulationType = DistributedRigidbody.MockingSimulationType.ExtrapolateVelocities; } //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); } go.transform.position = config.Position; go.transform.rotation = config.Rotation; agentController.Init(); #if UNITY_EDITOR // TODO remove hack for editor opaque with alpha clipping 2019.3.3 Array.ForEach(go.GetComponentsInChildren <Renderer>(), renderer => { foreach (var m in renderer.materials) { m.shader = Shader.Find(m.shader.name); } }); Array.ForEach(go.GetComponentsInChildren <DecalProjector>(), decal => { decal.material.shader = Shader.Find(decal.material.shader.name); }); #endif return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; // set it inactive until we can be sure setting up sensors etc worked without exceptions and it AgentController was initialized go.SetActive(false); var agentController = go.GetComponent <AgentController>(); agentController.Config = config; agentController.Config.AgentGO = go; var lane = go.AddComponent <VehicleLane>(); var baseLink = go.GetComponentInChildren <BaseLink>(); if (baseLink == null) { baseLink = new GameObject("BaseLink").AddComponent <BaseLink>(); baseLink.transform.SetParent(go.transform, false); } ActiveAgents.Add(agentController.Config); agentController.GTID = ++SimulatorManager.Instance.GTIDs; agentController.Config.GTID = agentController.GTID; BridgeClient bridgeClient = null; if (config.Bridge != null) { bridgeClient = go.AddComponent <BridgeClient>(); bridgeClient.Init(config.Bridge); if (!String.IsNullOrEmpty(config.Connection)) { bridgeClient.Connect(config.Connection); } } var sensorsController = go.AddComponent <SensorsController>(); agentController.AgentSensorsController = sensorsController; //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); if (network.IsClient) { //Disable controller and dynamics on clients so it will not interfere mocked components agentController.enabled = false; var vehicleDynamics = agentController.GetComponent <IVehicleDynamics>() as MonoBehaviour; if (vehicleDynamics != null) { vehicleDynamics.enabled = false; } } //Change the simulation type only if it's not set in the prefab var distributedRigidbody = go.GetComponent <DistributedRigidbody>(); if (distributedRigidbody == null) { distributedRigidbody = go.AddComponent <DistributedRigidbody>(); distributedRigidbody.SimulationType = DistributedRigidbody.MockingSimulationType.ExtrapolateVelocities; } //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); } go.transform.position = config.Position; go.transform.rotation = config.Rotation; sensorsController.SetupSensors(config.Sensors); agentController.Init(); go.SetActive(true); return(go); }
public GameObject SpawnAgent(AgentConfig config) { var go = Instantiate(config.Prefab, transform); go.name = config.Name; // set it inactive until we can be sure setting up sensors etc worked without exceptions and it AgentController was initialized go.SetActive(false); var controller = go.GetComponent <IAgentController>(); if (controller == null) { Debug.LogWarning($"{nameof(IAgentController)} implementation not found on the {config.Name} vehicle. This vehicle can't be used as an ego vehicle."); } else { controller.Config = config; controller.Config.AgentGO = go; ActiveAgents.Add(controller.Config); controller.GTID = ++SimulatorManager.Instance.GTIDs; controller.Config.GTID = controller.GTID; } var lane = go.AddComponent <VehicleLane>(); var baseLink = go.GetComponentInChildren <BaseLink>(); if (baseLink == null) { baseLink = new GameObject("BaseLink").AddComponent <BaseLink>(); baseLink.transform.SetParent(go.transform, false); } var sensorsController = go.GetComponent <ISensorsController>() ?? go.AddComponent <SensorsController>(); if (controller != null) { controller.AgentSensorsController = sensorsController; } //Add required components for distributing rigidbody from master to clients var network = Loader.Instance.Network; if (network.IsClusterSimulation) { HierarchyUtilities.ChangeToUniqueName(go); //Add the rest required components for cluster simulation ClusterSimulationUtilities.AddDistributedComponents(go); if (network.IsClient) { controller?.DisableControl(); } } BridgeClient bridgeClient = null; if (config.BridgeData != null) { var dir = Path.Combine(Simulator.Web.Config.PersistentDataPath, "Bridges"); var vfs = VfsEntry.makeRoot(dir); Simulator.Web.Config.CheckDir(vfs.GetChild(config.BridgeData.AssetGuid), Simulator.Web.Config.LoadBridgePlugin); bridgeClient = go.AddComponent <BridgeClient>(); config.Bridge = BridgePlugins.Get(config.BridgeData.Type); bridgeClient.Init(config.Bridge); if (!String.IsNullOrEmpty(config.Connection)) { bridgeClient.Connect(config.Connection); } } go.transform.position = config.Position; go.transform.rotation = config.Rotation; sensorsController.SetupSensors(config.Sensors); controller?.Init(); if (SimulatorManager.Instance.IsAPI) { SimulatorManager.Instance.EnvironmentEffectsManager.InitRainVFX(go.transform); } go.SetActive(true); return(go); }