У меня есть игра в гараже.Я делаю скрипт выбора машины для работы с динамическими машинами GameObjects, загружаемыми как объекты из ресурсов.Я также хочу динамически загружать колеса, потому что у меня есть набор из 40 колес.
Итак, когда начинается игра, первая машина инициализируется на сцене, и колеса сценария генерируют колеса, добавляя их к контейнерам колес автомобиля.После смены автомобиля я применяю ту же логику, но колеса больше не запускаются.
Вот моя гаражная логика.Может кто-нибудь помочь, пожалуйста?
public class GarageController : MonoBehaviour {
public GameObject m_CarsCollectionHolder;
private Object[] cars = null;
private Object[] wheels = null;
private GameObject current_car = null;
private GameObject current_wheel_FL, current_wheel_FR, current_wheel_RL, current_wheel_RR;
private GameObject FL, FR, RL, RR;
private int current_car_id = 0;
private int current_wheels_id = 0;
void Start () {
int first_time_init = PlayerPrefs.GetInt("first_time_init");
if(first_time_init == 0)
{
this.FirstTimeInit();
}
this.LoadWheels();
//Car load last;
this.CarInit();
}
/*
* First time game open init
*/
private void FirstTimeInit()
{
PlayerPrefs.SetInt("first_time_init", 1);
PlayerPrefs.SetInt("selected_car_id", 0);
}
/*
* Car init when game object ( based on last car selection and customizations )
*/
private void CarInit()
{
this.current_car_id = PlayerPrefs.GetInt("selected_car_id");
this.cars = Resources.LoadAll("cars", typeof(GameObject));
this.SetCar(this.current_car_id);
}
/*
* Car selection controllers
*/
public void NextCar()
{
if(this.current_car_id + 1 < cars.Length)
{
this.current_car_id++;
}
else
{
this.current_car_id = 0;
}
this.SetCar(this.current_car_id);
}
public void PreviousCar()
{
if (this.current_car_id - 1 >= 0)
{
this.current_car_id--;
}
else
{
this.current_car_id = cars.Length - 1;
}
this.SetCar(this.current_car_id);
}
private void SetCar(int car_id)
{
Destroy(this.current_car);
this.current_car = Instantiate(cars[car_id]) as GameObject;
this.current_car.transform.SetParent(m_CarsCollectionHolder.transform, false);
// Get current wheels containers
this.current_wheel_FL = GameObject.Find("FL");
this.current_wheel_FR = GameObject.Find("FR");
this.current_wheel_RL = GameObject.Find("RL");
this.current_wheel_RR = GameObject.Find("RR");
//Set history wheels
this.current_wheels_id = PlayerPrefs.GetInt("selected_wheels_for_car_" + car_id);
this.ChangeWheels(current_wheels_id);
SetCurrentCarId(car_id);
}
private void SetCurrentCarId(int car_id)
{
PlayerPrefs.SetInt("selected_car_id", car_id);
}
// Wheels controller
private void LoadWheels()
{
this.wheels = Resources.LoadAll("wheels", typeof(GameObject));
}
private void ChangeWheels(int current_wheels_id)
{
//Destroy old wheels
Destroy(this.FL);
Destroy(this.FR);
Destroy(this.RL);
Destroy(this.RR);
//Init wheels
this.FL = Instantiate(this.wheels[current_wheels_id]) as GameObject;
this.FR = Instantiate(this.wheels[current_wheels_id]) as GameObject;
this.RL = Instantiate(this.wheels[current_wheels_id]) as GameObject;
this.RR = Instantiate(this.wheels[current_wheels_id]) as GameObject;
//Move wheels in their containers, and rotate
this.FL.transform.SetParent(current_wheel_FL.transform, false);
this.FR.transform.SetParent(current_wheel_FR.transform, false);
this.FR.transform.Rotate(0, 180, 0);
this.RL.transform.SetParent(current_wheel_RL.transform, false);
this.RR.transform.SetParent(current_wheel_RR.transform, false);
this.RR.transform.Rotate(0, 180, 0);
}
}