public static void Initialize(VehicleCollection collection, Transform customPrefabs) { if (sm_initialized) { return; } Debug.Log("Traffic++: Initializing Bus.\n"); VehicleInfo originalBus = collection.m_prefabs.Where(p => p.name.Contains("Bus")).FirstOrDefault(); if (originalBus == null) { throw new KeyNotFoundException("Bus was not found on " + collection.name); } GameObject instance = GameObject.Instantiate <GameObject>(originalBus.gameObject); instance.name = "Bus"; instance.transform.SetParent(customPrefabs); BusAI busAI = instance.GetComponent <BusAI>(); TransportInfo transportInfo = busAI.m_transportInfo; GameObject.Destroy(instance.GetComponent <BusAI>()); CustomBusAI customBusAI = instance.AddComponent <CustomBusAI>(); customBusAI.m_transportInfo = transportInfo; VehicleInfo bus = instance.GetComponent <VehicleInfo>(); bus.m_prefabInitialized = false; bus.m_vehicleAI = null; MethodInfo initMethod = typeof(VehicleCollection).GetMethod("InitializePrefabs", BindingFlags.Static | BindingFlags.NonPublic); Initializer.QueuePrioritizedLoadingAction((IEnumerator)initMethod.Invoke(null, new object[] { collection.name, new[] { bus }, new string[] { "Bus" } })); sm_initialized = true; }
/* * In here I'm changing the prefabs to have my classes. This way, every time the game instantiates * a prefab that I've changed, that object will run my code. * The prefabs aren't available at the moment of creation of this class, that's why I keep trying to * run it on update. I want to make sure I make the switch as soon as they exist to prevent the game * from instantianting objects without my code. */ void TryReplacePrefabs() { NetCollection beautificationNetCollection = null; NetCollection roadsNetCollection = null; NetCollection publicTansportNetCollection = null; VehicleCollection garbageVehicleCollection = null; VehicleCollection policeVehicleCollection = null; VehicleCollection publicTansportVehicleCollection = null; VehicleCollection healthCareVehicleCollection = null; VehicleCollection fireDepartmentVehicleCollection = null; VehicleCollection industrialVehicleCollection = null; TransportCollection publicTransportTransportCollection = null; ToolController toolController = null; try { // NetCollections beautificationNetCollection = TryGetComponent <NetCollection>("Beautification"); if (beautificationNetCollection == null) { return; } roadsNetCollection = TryGetComponent <NetCollection>("Road"); if (roadsNetCollection == null) { return; } publicTansportNetCollection = TryGetComponent <NetCollection>("Public Transport"); if (publicTansportNetCollection == null) { return; } // VehicleCollections garbageVehicleCollection = TryGetComponent <VehicleCollection>("Garbage"); if (garbageVehicleCollection == null) { return; } policeVehicleCollection = TryGetComponent <VehicleCollection>("Police Department"); if (policeVehicleCollection == null) { return; } publicTansportVehicleCollection = TryGetComponent <VehicleCollection>("Public Transport"); if (publicTansportVehicleCollection == null) { return; } healthCareVehicleCollection = TryGetComponent <VehicleCollection>("Health Care"); if (healthCareVehicleCollection == null) { return; } fireDepartmentVehicleCollection = TryGetComponent <VehicleCollection>("Fire Department"); if (fireDepartmentVehicleCollection == null) { return; } industrialVehicleCollection = TryGetComponent <VehicleCollection>("Industrial"); if (industrialVehicleCollection == null) { return; } // Transports publicTransportTransportCollection = TryGetComponent <TransportCollection>("Public Transport"); if (publicTransportTransportCollection == null) { return; } // Tools toolController = TryGetComponent <ToolController>("Tool Controller"); if (toolController == null) { return; } } catch (Exception e) { Debug.Log("Traffic++: Unexpected " + e.GetType().Name + " getting required components: " + e.Message + "\n" + e.StackTrace + "\n"); return; } Debug.Log("Traffic++: Queueing prefabs for loading..."); Singleton <LoadingManager> .instance.QueueLoadingAction(ActionWrapper(() => { try { // roads ZonablePedestrianPathAI.Initialize(beautificationNetCollection, transform); ZonablePedestrianBridgeAI.Initialize(beautificationNetCollection, transform); LargeRoadWithBusLanesAI.Initialize(roadsNetCollection, transform); LargeRoadWithBusLanesBridgeAI.Initialize(roadsNetCollection, transform); if ((CSLTraffic.Options & OptionsManager.ModOptions.GhostMode) != OptionsManager.ModOptions.GhostMode) { // Transports BusTransportLineAI.Initialize(publicTansportNetCollection, publicTansportVehicleCollection, publicTransportTransportCollection, transform); // vehicles CustomAmbulanceAI.Initialize(healthCareVehicleCollection, transform); CustomBusAI.Initialize(publicTansportVehicleCollection, transform); CustomCargoTruckAI.Initialize(industrialVehicleCollection, transform); CustomFireTruckAI.Initialize(fireDepartmentVehicleCollection, transform); CustomGarbageTruckAI.Initialize(garbageVehicleCollection, transform); CustomHearseAI.Initialize(healthCareVehicleCollection, transform); CustomPoliceCarAI.Initialize(policeVehicleCollection, transform); //Tools CustomTransportTool.Initialize(toolController); } // Localization UpdateLocalization(); AddQueuedActionsToLoadingQueue(); } catch (KeyNotFoundException knf) { Debug.Log("Traffic++: Error initializing a prefab: " + knf.Message + "\n" + knf.StackTrace + "\n"); } catch (Exception e) { Debug.Log("Traffic++: Unexpected " + e.GetType().Name + " initializing prefabs: " + e.Message + "\n" + e.StackTrace + "\n"); } })); m_initialized = true; Debug.Log("Traffic++: Prefabs queued for loading."); }