RESOLU [MY-AL] Modifier un serializable

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

RESOLU [MY-AL] Modifier un serializable

Message par freepl » 17 Mars 2022 09:34

Bonjour
Dans ce script je serialise le nombre de trajet en incluant la modification par trajet, la vitesse, si je créée dès le départ des véhicules ou pas et les temps aléatoire de création des véhicules .

Par contre le pourcentage de type de véhicule est commun à tous.
Il y a très longtemps que je l'ai fait et je ne me rappelle plus comment faire pour que la partie de pourcentage de création soit serialisé individuellement pour chaque trajet et non plus communs à tous.

En clair pour le trajet Element 0, je ne voudrais mettre que 100% de PUtilitaire alors que pour Element 1 , je voudrai x% de PSemi, y % de Pbus ....


Code : Tout sélectionner

using UnityEngine;
using System.Collections;
using System;
// donne le% de véhicules

[Serializable]
public struct Trajets {
	public GameObject trajet;
	public float vitesse;
	public float mEntrePoint;
	public DepartVl[] departVl;
}

[Serializable]
public struct DepartVl {
	public int positionDepart;
	public float vitesseDepart;
}

public enum InterieurVl {
	None,
	Oui,
	Non
}

public class DebutRoute : MonoBehaviour {


	public Trajets[] trajets;
	private int choixTrajet;

	// pour multiplier le temps d'apparition des vl la nuit
	public float multiplicateurNuit = 1.0f; 
	// temps apparition entre chaque voiture choisi au hasard
	public float[] tempsApparition; // dans inspector donner le nombre de temsp puis les renseigner
	//choisis un temps aléatoire
	private int choixTemps;
	private float temps;

	public float pCar;
	public float pUtilitaire;
	public float pSemi;
	public float pBus;
	public float SpSemiLong;
	public float pMoto;

	private int choixPourcentage;

	private bool finStart;
	private Light jourNuit;

	void Start () {
		choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
		choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
		choixPourcentage = UnityEngine.Random.Range(1,101);

		if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto > 100)
			Debug.LogError("il y a plus de 100%");
		if (pCar + pUtilitaire + pSemi + pBus  + SpSemiLong  + pMoto < 100)
			Debug.LogError("il y a moin de 100%");

		for (int i = 0; i < trajets.Length; i++) {
			CreationVlDepart (i);
		}
		// 1 ° gamobject car seul dans la hierarchy.on va chercher sky Dome
		// je prends son enfant Light
		// dans Inspector on prend Light

		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();

		finStart = true;
	}

	// Update is called once per frame
	void Update () {
		if (finStart && Time.timeScale != 0) {
			temps += Time.fixedDeltaTime;
		
			// on multiplie le temps d'apparition si light < à 0.15
			float tempsTempoApparition = 0.0f;
			if ( jourNuit.intensity < 0.15f) {
				tempsTempoApparition = tempsApparition[choixTemps] * multiplicateurNuit;
			}
			else  {
				tempsTempoApparition = tempsApparition[choixTemps];
			}

			//automatiquement
			if (temps >= tempsTempoApparition) {
				CreationVl(trajets[choixTrajet].trajet, trajets[choixTrajet].mEntrePoint, trajets[choixTrajet].vitesse, 0);
				choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
				choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
				choixPourcentage = UnityEngine.Random.Range(1,101);
				temps = 0;
			}
		}
	}

	void CreationVlDepart (int countTrajet) {
		if (trajets[countTrajet].departVl.Length != 0) {
			for (int i = 0; i < trajets[countTrajet].departVl.Length; i++) {
				CreationVl (trajets[countTrajet].trajet, trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
			}
		}
	}

	public void CreationVl (GameObject trajet, float metre, float vitesseFraction, int position) {
		string nom = "";
		int choixVl = 0;
		InterieurVl interieur = InterieurVl.None;

		  //voitures
		if (choixPourcentage < pCar) {
			choixVl = UnityEngine.Random.Range(1,19); //19car il y a 18 voitures + 1
			nom = "Voitures/Car_";
			interieur = InterieurVl.Oui;
			//utilitaires
		} else if (choixPourcentage < pCar + pUtilitaire) {
			choixVl = UnityEngine.Random.Range(1,14);//14 car il y a  13 utilitaires+ 1
			nom = "Utilitaires/utilitaire_";
			interieur = InterieurVl.Oui;
			//semi
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi) {
			choixVl = UnityEngine.Random.Range(1,9);//car 8 camions ne pas prendre les porte char et le double remoque
			nom = "Poids_Lourds/semi_";
			interieur = InterieurVl.Oui;

			//bus
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi + pBus) {
			choixVl = UnityEngine.Random.Range(1,4);//car 3 bus +1
			nom = "BUS/bus_";
			interieur = InterieurVl.Oui;


			//semi long  2 remorque et porte char
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi+ pBus + SpSemiLong) {
			choixVl = UnityEngine.Random.Range(1,4);//car 2 porte chars et le double remoque
			nom = "Super_Poids_Lourds_Longs/semiLong_";
			interieur = InterieurVl.Oui;
			//moto
		} else if (choixPourcentage <= pCar + pUtilitaire + pSemi+ pBus + SpSemiLong + pMoto) {
			choixVl = UnityEngine.Random.Range(1,9);//9
			nom = "Motos/moto_";
			interieur = InterieurVl.Non;
		}


		GameObject instance = Instantiate(Resources.Load(""+nom+choixVl,typeof(GameObject))) as GameObject;

		//modif du non
		instance.name += trajet.name;
		//change la postion de l'object dans le scéne
		//donc transform = à l'obejct qui contient le script DebutRoute
		instance.transform.SetParent(transform);

		if (GetComponent<TrajetVl>() != null) {
			//set le parcour sur l'object
			instance.GetComponent<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponent<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponent<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponent<TrajetVl>().minTab = position;
			instance.GetComponent<TrajetVl>().mEntrePoint = metre;
			instance.GetComponent<TrajetVl>().interieur = interieur;
			instance.GetComponent<TrajetVl>().DemrageVl();
		} else {
			//set le parcour sur l'object
			instance.GetComponentInChildren<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponentInChildren<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().minTab = position;
			instance.GetComponentInChildren<TrajetVl>().mEntrePoint = metre;
			instance.GetComponentInChildren<TrajetVl>().interieur = interieur;
			instance.GetComponentInChildren<TrajetVl>().DemrageVl();
		}
	}
}

Z045.jpg
Z045.jpg (50.29 Kio) Consulté 2126 fois
Dernière édition par freepl le 07 Avr 2022 10:23, édité 1 fois.

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Modifier un serializable

Message par boubouk50 » 17 Mars 2022 10:38

Il faut juste que ces données soient incorporées dans Trajets. Comme ça pour chaque élément du tableau du trajet tu définis les pourcentages.

Sinon, tu crées un scriptableObject qui les contient et tu crées plusieurs configurations que tu passes à Trajets.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Modifier un serializable

Message par freepl » 17 Mars 2022 14:33

J'ai bien les % des véhicules mais Unity dis que je ne peux pas créer de clones.
Je n'ai donc pas incorporé la création dans Trajets.
Et que dois-je modifier pour ne plus avoir ( si cela marche) à renseigner les % d'engins en bas de l'inspector ?

Code : Tout sélectionner

ArgumentException: The thing you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:104)
UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:90)
DebutRoute.CreationVl (UnityEngine.GameObject trajet, Single metre, Single vitesseFraction, Int32 position) (at Assets/SCRIPT/Route/DebutRoute.cs:172)
DebutRoute.CreationVlDepart (Int32 countTrajet) (at Assets/SCRIPT/Route/DebutRoute.cs:126)
DebutRoute.Start () (at Assets/SCRIPT/Route/DebutRoute.cs:87)

Code : Tout sélectionner

using UnityEngine;
using System.Collections;
using System;
// donne le% de véhicules

[Serializable]
public struct Trajets {
	public GameObject trajet;
	public float vitesse;
	public float mEntrePoint;
	public DepartVl[] departVl;
	public float pCar;//
	public float pUtilitaire;//
	public float pSemi;//
	public float pBus;//
	public float pSemiLong;//
	public float pMoto;//
}

[Serializable]
public struct DepartVl {
	public int positionDepart;
	public float vitesseDepart;
}

public enum InterieurVl {
	None,
	Oui,
	Non
}

public class DebutRoute : MonoBehaviour {

	// script qui detruit le vl à la fin du parcours et l'instancie au départ
	// si true coché = début si false non coché = à fin

	/*Donc tu choisie dans l'inspector le nombre de vl que tu veux au lancement de ta maquette pour chaque trajet et le programme vas te les instancié automatiquement par rapport à la fraction et au trajet.

exemple :
ligne Vl depart 4 , car j'ai 4 voies
element 0  : 4 si je veux 4 vl affichés dès le lancement de la maquette sur cette voie
element 1  : 1 si je veux 1 vl affichés dès le lancement de la maquette sur cette voie
element 2  : 3 si je veux 3 vl affichés dès le lancement de la maquette sur cette voie
element 3  : 10 si je veux 10 vl affichés dès le lancement de la maquette sur cette voie

Pour element0 , on a mis 0. Il y aura 0 véhicuke sur le trajet à 0.0,  0.25,  0.5 et  0.75
Pour element3 , on a mis 10. Il y aura 10 véhicuke sur le trajet à 0,0.  0.1,  0.2,  0.3, ....., 0.9

Il n'y a acucun vl à 1 car il serait à la fin du trajet et donc invisible !


*/
	public Trajets[] trajets;
	private int choixTrajet;

	// pour multiplier le temps d'apparition des vl la nuit
	public float multiplicateurNuit = 1.0f; 
	// temps apparition entre chaque voiture choisi au hasard
	public float[] tempsApparition; // dans inspector donner le nombre de temsp puis les renseigner
	//choisis un temps aléatoire
	private int choixTemps;
	private float temps;

	public float pCar;
	public float pUtilitaire;
	public float pSemi;
	public float pBus;
	public float SpSemiLong;
	public float pMoto;

	private int choixPourcentage;

	private bool finStart;
	private Light jourNuit;

	void Start () {
		choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
		choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
		choixPourcentage = UnityEngine.Random.Range(1,101);

		if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto > 100)
			Debug.LogError("il y a plus de 100%");
		if (pCar + pUtilitaire + pSemi + pBus  + SpSemiLong  + pMoto < 100)
			Debug.LogError("il y a moin de 100%");

		for (int i = 0; i < trajets.Length; i++) {
			CreationVlDepart (i);
		}
		// 1 ° gamobject car seul dans la hierarchy.on va chercher sky Dome
		// je prends son enfant Light
		// dans Inspector on prend Light

		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();

		finStart = true;
	}

	// Update is called once per frame
	void Update () {
		if (finStart && Time.timeScale != 0) {
			temps += Time.fixedDeltaTime;
		
			// on multiplie le temps d'apparition si light < à 0.15
			float tempsTempoApparition = 0.0f;
			if ( jourNuit.intensity < 0.15f) {
				tempsTempoApparition = tempsApparition[choixTemps] * multiplicateurNuit;
			}
			else  {
				tempsTempoApparition = tempsApparition[choixTemps];
			}

			//automatiquement
			if (temps >= tempsTempoApparition) {
				CreationVl(trajets[choixTrajet].trajet, trajets[choixTrajet].mEntrePoint, trajets[choixTrajet].vitesse, 0);
				choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
				choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
				choixPourcentage = UnityEngine.Random.Range(1,101);
				temps = 0;
			}
		}
	}

	void CreationVlDepart (int countTrajet) {
		if (trajets[countTrajet].departVl.Length != 0) {
			for (int i = 0; i < trajets[countTrajet].departVl.Length; i++) {
				CreationVl (trajets[countTrajet].trajet, trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
			}
		}
	}

	public void CreationVl (GameObject trajet, float metre, float vitesseFraction, int position) {
		string nom = "";
		int choixVl = 0;
		InterieurVl interieur = InterieurVl.None;

		  //voitures
		if (choixPourcentage < pCar) {
			choixVl = UnityEngine.Random.Range(1,19); //19car il y a 18 voitures + 1
			nom = "Voitures/Car_";
			interieur = InterieurVl.Oui;
			//utilitaires
		} else if (choixPourcentage < pCar + pUtilitaire) {
			choixVl = UnityEngine.Random.Range(1,14);//14 car il y a  13 utilitaires+ 1
			nom = "Utilitaires/utilitaire_";
			interieur = InterieurVl.Oui;
			//semi
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi) {
			choixVl = UnityEngine.Random.Range(1,9);//car 8 camions ne pas prendre les porte char et le double remoque
			nom = "Poids_Lourds/semi_";
			interieur = InterieurVl.Oui;

			//bus
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi + pBus) {
			choixVl = UnityEngine.Random.Range(1,4);//car 3 bus +1
			nom = "BUS/bus_";
			interieur = InterieurVl.Oui;


			//semi long  2 remorque et porte char
		} else if (choixPourcentage < pCar + pUtilitaire + pSemi+ pBus + SpSemiLong) {
			choixVl = UnityEngine.Random.Range(1,4);//car 2 porte chars et le double remoque
			nom = "Super_Poids_Lourds_Longs/semiLong_";
			interieur = InterieurVl.Oui;
			//moto
		} else if (choixPourcentage <= pCar + pUtilitaire + pSemi+ pBus + SpSemiLong + pMoto) {
			choixVl = UnityEngine.Random.Range(1,9);//9
			nom = "Motos/moto_";
			interieur = InterieurVl.Non;
		}


		GameObject instance = Instantiate(Resources.Load(""+nom+choixVl,typeof(GameObject))) as GameObject;

		//modif du non
		instance.name += trajet.name;
		//change la postion de l'object dans le scéne
		//donc transform = à l'obejct qui contient le script DebutRoute
		instance.transform.SetParent(transform);

		if (GetComponent<TrajetVl>() != null) {
			//set le parcour sur l'object
			instance.GetComponent<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponent<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponent<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponent<TrajetVl>().minTab = position;
			instance.GetComponent<TrajetVl>().mEntrePoint = metre;
			instance.GetComponent<TrajetVl>().interieur = interieur;
			instance.GetComponent<TrajetVl>().DemrageVl();
		} else {
			//set le parcour sur l'object
			instance.GetComponentInChildren<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponentInChildren<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().minTab = position;
			instance.GetComponentInChildren<TrajetVl>().mEntrePoint = metre;
			instance.GetComponentInChildren<TrajetVl>().interieur = interieur;
			instance.GetComponentInChildren<TrajetVl>().DemrageVl();
		}
	}
}


Z047.jpg
Z047.jpg (103.45 Kio) Consulté 2100 fois

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Modifier un serializable

Message par boubouk50 » 17 Mars 2022 15:42

Le souci est ailleurs, Resources.Load () ne trouve pas l'asset donc il renvoie null donc tu ne peux pas instancier un objet null.

C'est tout à fait possible puisque tu ne testes pas choixPourcentage.
Ici la somme de tes pourcentages n'est pas de 100, donc si tu as un choixPourcentage > 50 alors ça ne va pas marcher.
Affecte d'abord tes pourcentages dans l'éditeur.
Sinon, il te faut un dernier else pour ne pas continuer ou faire autre chose.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Modifier un serializable

Message par freepl » 17 Mars 2022 16:49

Cette fois ci , j'ai mis 100% de voiture ( car) sur un trajet et 100% de moto sur l'autre
Z052.jpg
Z052.jpg (111.58 Kio) Consulté 2080 fois
Mais c'est toujours le choix des % du bas qui prévaut sur le choix des %
On voit que j'ai des car, des motos.. et des utilitaires sur les deux trajets
Z053.jpg
Z053.jpg (128.14 Kio) Consulté 2080 fois

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Modifier un serializable

Message par boubouk50 » 17 Mars 2022 17:03

Oui car dans ton code, tu fais des conditions avec pCar, pBus, etc.

Code : Tout sélectionner

 if (choixPourcentage < pCar + pUtilitaire + pSemi) {
Alors qu'il faut que tu fasses les comparaisons avec ceux de trajet.

Code : Tout sélectionner

 if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi) {
D'ailleurs, pCar, pBus etc ne te sont plus utiles, tu devrais les supprimer. Comme ça, tu verras où tu as besoin d'utiliser les nouveaux pourcentages.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Modifier un serializable

Message par freepl » 23 Mars 2022 16:38

Bonjour
J'ai fait la modif dans Void CreationVl , mais unity ne connait pas trajet.pcar, trajet.putilitaire ...

Code : Tout sélectionner

Assets/SCRIPT/Route/DebutRoute.cs(165,55): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `pCar' and no extension method `pCar' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

Code : Tout sélectionner

using UnityEngine;
using System.Collections;
using System;
// donne le% de véhicules

[Serializable]
public struct Trajets {
	public GameObject trajet;
	public float vitesse;
	public float mEntrePoint;
	public DepartVl[] departVl;
	public float pCar;//
	public float pUtilitaire;//
	public float pSemi;//
	public float pBus;//
	public float pSemiLong;//
	public float pMoto;//
}

[Serializable]
public struct DepartVl {
	public int positionDepart;
	public float vitesseDepart;
}

public enum InterieurVl {
	None,
	Oui,
	Non
}

public class DebutRoute : MonoBehaviour {
	
	// script qui detruit le vl à la fin du parcours et l'instancie au départ
	// si true coché = début si false non coché = à fin
	
	/*Donc tu choisie dans l'inspector le nombre de vl que tu veux au lancement de ta maquette pour chaque trajet et le programme vas te les instancié automatiquement par rapport à la fraction et au trajet.

exemple :
ligne Vl depart 4 , car j'ai 4 voies
element 0  : 4 si je veux 4 vl affichés dès le lancement de la maquette sur cette voie
element 1  : 1 si je veux 1 vl affichés dès le lancement de la maquette sur cette voie
element 2  : 3 si je veux 3 vl affichés dès le lancement de la maquette sur cette voie
element 3  : 10 si je veux 10 vl affichés dès le lancement de la maquette sur cette voie

Pour element0 , on a mis 0. Il y aura 0 véhicuke sur le trajet à 0.0,  0.25,  0.5 et  0.75
Pour element3 , on a mis 10. Il y aura 10 véhicuke sur le trajet à 0,0.  0.1,  0.2,  0.3, ....., 0.9

Il n'y a acucun vl à 1 car il serait à la fin du trajet et donc invisible !


*/
	public Trajets[] trajets;
	private int choixTrajet;
	
	// pour multiplier le temps d'apparition des vl la nuit
	public float multiplicateurNuit = 1.0f; 
	// temps apparition entre chaque voiture choisi au hasard
	public float[] tempsApparition; // dans inspector donner le nombre de temsp puis les renseigner
	//choisis un temps aléatoire
	private int choixTemps;
	private float temps;
	
	public float pCar;
	public float pUtilitaire;
	public float pSemi;
	public float pBus;
	public float SpSemiLong;
	public float pMoto;
	
	private int choixPourcentage;
	
	private bool finStart;
	private Light jourNuit;
	
	void Start () {
		choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
		choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
		choixPourcentage = UnityEngine.Random.Range(1,101);
		
		if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto > 100)
			Debug.LogError("il y a plus de 100%");
		if (pCar + pUtilitaire + pSemi + pBus  + SpSemiLong  + pMoto < 100)
			Debug.LogError("il y a moin de 100%");
		
		for (int i = 0; i < trajets.Length; i++) {
			CreationVlDepart (i);
		}
		// 1 ° gamobject car seul dans la hierarchy.on va chercher sky Dome
		// je prends son enfant Light
		// dans Inspector on prend Light
		
		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();
		
		finStart = true;
	}
	
	// Update is called once per frame
	void Update () {
		if (finStart && Time.timeScale != 0) {
			temps += Time.fixedDeltaTime;
			
			// on multiplie le temps d'apparition si light < à 0.15
			float tempsTempoApparition = 0.0f;
			if ( jourNuit.intensity < 0.15f) {
				tempsTempoApparition = tempsApparition[choixTemps] * multiplicateurNuit;
			}
			else  {
				tempsTempoApparition = tempsApparition[choixTemps];
			}
			
			//automatiquement
			if (temps >= tempsTempoApparition) {
				CreationVl(trajets[choixTrajet].trajet, trajets[choixTrajet].mEntrePoint, trajets[choixTrajet].vitesse, 0);
				choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
				choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
				choixPourcentage = UnityEngine.Random.Range(1,101);
				temps = 0;
			}
		}
	}
	
	void CreationVlDepart (int countTrajet) {
		if (trajets[countTrajet].departVl.Length != 0) {
			for (int i = 0; i < trajets[countTrajet].departVl.Length; i++) {
				CreationVl (trajets[countTrajet].trajet, trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
			}
		}
	}
	
	public void CreationVl (GameObject trajet, float metre, float vitesseFraction, int position) {
		string nom = "";
		int choixVl = 0;
		InterieurVl interieur = InterieurVl.None;

		//voitures
		if (choixPourcentage < trajet.pCar) {
			choixVl = UnityEngine.Random.Range(1,19); //19car il y a 18 voitures + 1
			nom = "Voitures/Car_";
			interieur = InterieurVl.Oui;
			//utilitaires
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire) {
			choixVl = UnityEngine.Random.Range(1,14);//14 car il y a  13 utilitaires+ 1
			nom = "Utilitaires/utilitaire_";
			interieur = InterieurVl.Oui;
			//semi
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi) {
			choixVl = UnityEngine.Random.Range(1,9);//car 8 camions ne pas prendre les porte char et le double remoque
			nom = "Poids_Lourds/semi_";
			interieur = InterieurVl.Oui;
			
			//bus
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi + trajet.pBus) {
			choixVl = UnityEngine.Random.Range(1,4);//car 3 bus +1
			nom = "BUS/bus_";
			interieur = InterieurVl.Oui;
			
			
			//semi long  2 remorque et porte char
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi+ trajet.pBus + trajet.SpSemiLong) {
			choixVl = UnityEngine.Random.Range(1,4);//car 2 porte chars et le double remoque
			nom = "Super_Poids_Lourds_Longs/semiLong_";
			interieur = InterieurVl.Oui;
			//moto
		} else if (choixPourcentage <= trajet.pCar + trajet.pUtilitaire + trajet.pSemi+ trajet.pBus + trajet.SpSemiLong + trajet.pMoto) {
			choixVl = UnityEngine.Random.Range(1,9);//9
			nom = "Motos/moto_";
			interieur = InterieurVl.Non;
		}
		
		
		GameObject instance = Instantiate(Resources.Load(""+nom+choixVl,typeof(GameObject))) as GameObject;
		
		//modif du non
		instance.name += trajet.name;
		//change la postion de l'object dans le scéne
		//donc transform = à l'obejct qui contient le script DebutRoute
		instance.transform.SetParent(transform);
		
		if (GetComponent<TrajetVl>() != null) {
			//set le parcour sur l'object
			instance.GetComponent<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponent<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponent<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponent<TrajetVl>().minTab = position;
			instance.GetComponent<TrajetVl>().mEntrePoint = metre;
			instance.GetComponent<TrajetVl>().interieur = interieur;
			instance.GetComponent<TrajetVl>().DemrageVl();
		} else {
			//set le parcour sur l'object
			instance.GetComponentInChildren<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponentInChildren<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().minTab = position;
			instance.GetComponentInChildren<TrajetVl>().mEntrePoint = metre;
			instance.GetComponentInChildren<TrajetVl>().interieur = interieur;
			instance.GetComponentInChildren<TrajetVl>().DemrageVl();
		}
	}
}


Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Modifier un serializable

Message par boubouk50 » 23 Mars 2022 18:22

Ce n'est pas ce que dit l'erreur.
L'erreur dit que pCar n'existe pas dans la Classe GameObject. Donc tu as du définir trajet comme un GameObject.

Tu as défini une structure, pour appeler ses propriétés tu dois passer par elle.

Effectivement

Code : Tout sélectionner

public void CreationVl (GameObject trajet, float metre, float vitesseFraction, int position) {
tu dois récupérer un Trajets pas un GameObject.

Code : Tout sélectionner

public void CreationVl (Trajets trajet, float metre, float vitesseFraction, int position) {
Peut-être devrais-tu renommer ta variable *trajet* (de Trajets) en *trajObject* pour ne pas te tromper.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Modifier un serializable

Message par freepl » 24 Mars 2022 10:48

Bonjour
J'ai modifié le nom de la variable *trajet* (de Trajets) en *trajObject* et modifié le nom dans le code.

Code : Tout sélectionner

using UnityEngine;
using System.Collections;
using System;
// donne le% de véhicules

[Serializable]
public struct Trajets {
	public GameObject trajObject;
	public float vitesse;
	public float mEntrePoint;
	public DepartVl[] departVl;
	public float pCar;//
	public float pUtilitaire;//
	public float pSemi;//
	public float pBus;//
	public float pSemiLong;//
	public float pMoto;//
}

[Serializable]
public struct DepartVl {
	public int positionDepart;
	public float vitesseDepart;
}

public enum InterieurVl {
	None,
	Oui,
	Non
}

public class DebutRoute : MonoBehaviour {
	
	// script qui detruit le vl à la fin du parcours et l'instancie au départ
	// si true coché = début si false non coché = à fin
	
	/*Donc tu choisie dans l'inspector le nombre de vl que tu veux au lancement de ta maquette pour chaque trajet et le programme vas te les instancié automatiquement par rapport à la fraction et au trajet.

exemple :
ligne Vl depart 4 , car j'ai 4 voies
element 0  : 4 si je veux 4 vl affichés dès le lancement de la maquette sur cette voie
element 1  : 1 si je veux 1 vl affichés dès le lancement de la maquette sur cette voie
element 2  : 3 si je veux 3 vl affichés dès le lancement de la maquette sur cette voie
element 3  : 10 si je veux 10 vl affichés dès le lancement de la maquette sur cette voie

Pour element0 , on a mis 0. Il y aura 0 véhicuke sur le trajet à 0.0,  0.25,  0.5 et  0.75
Pour element3 , on a mis 10. Il y aura 10 véhicuke sur le trajet à 0,0.  0.1,  0.2,  0.3, ....., 0.9

Il n'y a acucun vl à 1 car il serait à la fin du trajet et donc invisible !


*/
	public Trajets[] trajets;
	private int choixTrajet;
	
	// pour multiplier le temps d'apparition des vl la nuit
	public float multiplicateurNuit = 1.0f; 
	// temps apparition entre chaque voiture choisi au hasard
	public float[] tempsApparition; // dans inspector donner le nombre de temsp puis les renseigner
	//choisis un temps aléatoire
	private int choixTemps;
	private float temps;
	
	public float pCar;
	public float pUtilitaire;
	public float pSemi;
	public float pBus;
	public float SpSemiLong;
	public float pMoto;
	
	private int choixPourcentage;
	
	private bool finStart;
	private Light jourNuit;
	
	void Start () {
		choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
		choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
		choixPourcentage = UnityEngine.Random.Range(1,101);
		
		if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto > 100)
			Debug.LogError("il y a plus de 100%");
		if (pCar + pUtilitaire + pSemi + pBus  + SpSemiLong  + pMoto < 100)
			Debug.LogError("il y a moin de 100%");
		
		for (int i = 0; i < trajets.Length; i++) {
			CreationVlDepart (i);
		}
		// 1 ° gamobject car seul dans la hierarchy.on va chercher sky Dome
		// je prends son enfant Light
		// dans Inspector on prend Light
		
		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();
		
		finStart = true;
	}
	
	// Update is called once per frame
	void Update () {
		if (finStart && Time.timeScale != 0) {
			temps += Time.fixedDeltaTime;
			
			// on multiplie le temps d'apparition si light < à 0.15
			float tempsTempoApparition = 0.0f;
			if ( jourNuit.intensity < 0.15f) {
				tempsTempoApparition = tempsApparition[choixTemps] * multiplicateurNuit;
			}
			else  {
				tempsTempoApparition = tempsApparition[choixTemps];
			}
			
			//automatiquement
			if (temps >= tempsTempoApparition) {
				CreationVl(trajets[choixTrajet].trajObject, trajets[choixTrajet].mEntrePoint, trajets[choixTrajet].vitesse, 0);
				choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
				choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
				choixPourcentage = UnityEngine.Random.Range(1,101);
				temps = 0;
			}
		}
	}
	
	void CreationVlDepart (int countTrajet) {
		if (trajets[countTrajet].departVl.Length != 0) {
			for (int i = 0; i < trajets[countTrajet].departVl.Length; i++) {
				CreationVl (trajets[countTrajet].trajObject trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
			}
		}
	}
	
	public void CreationVl (Trajets trajet, float metre, float vitesseFraction, int position) {
		string nom = "";
		int choixVl = 0;
		InterieurVl interieur = InterieurVl.None;
		
		//voitures
		if (choixPourcentage < trajet.pCar) {
			choixVl = UnityEngine.Random.Range(1,19); //19car il y a 18 voitures + 1
			nom = "Voitures/Car_";
			interieur = InterieurVl.Oui;
			//utilitaires
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire) {
			choixVl = UnityEngine.Random.Range(1,14);//14 car il y a  13 utilitaires+ 1
			nom = "Utilitaires/utilitaire_";
			interieur = InterieurVl.Oui;
			//semi
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi) {
			choixVl = UnityEngine.Random.Range(1,9);//car 8 camions ne pas prendre les porte char et le double remoque
			nom = "Poids_Lourds/semi_";
			interieur = InterieurVl.Oui;
			
			//bus
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi + trajet.pBus) {
			choixVl = UnityEngine.Random.Range(1,4);//car 3 bus +1
			nom = "BUS/bus_";
			interieur = InterieurVl.Oui;
			
			
			//semi long  2 remorque et porte char
		} else if (choixPourcentage < trajet.pCar + trajet.pUtilitaire + trajet.pSemi+ trajet.pBus + trajet.pSemiLong) {
			choixVl = UnityEngine.Random.Range(1,4);//car 2 porte chars et le double remoque
			nom = "Super_Poids_Lourds_Longs/semiLong_";
			interieur = InterieurVl.Oui;
			//moto
		} else if (choixPourcentage <= trajet.pCar + trajet.pUtilitaire + trajet.pSemi+ trajet.pBus + trajet.pSemiLong + trajet.pMoto) {
			choixVl = UnityEngine.Random.Range(1,9);//9
			nom = "Motos/moto_";
			interieur = InterieurVl.Non;
		}
		
		
		GameObject instance = Instantiate(Resources.Load(""+nom+choixVl,typeof(GameObject))) as GameObject;
		
		//modif du non
		instance.name += trajet.name;
		//change la postion de l'object dans le scéne
		//donc transform = à l'obejct qui contient le script DebutRoute
		instance.transform.SetParent(transform);
		
		if (GetComponent<TrajetVl>() != null) {
			//set le parcour sur l'object
			instance.GetComponent<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponent<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponent<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponent<TrajetVl>().minTab = position;
			instance.GetComponent<TrajetVl>().mEntrePoint = metre;
			instance.GetComponent<TrajetVl>().interieur = interieur;
			instance.GetComponent<TrajetVl>().DemrageVl();
		} else {
			//set le parcour sur l'object
			instance.GetComponentInChildren<TrajetVl>().target_Path = trajet;
			//modif la vitesse de l'object
			instance.GetComponentInChildren<TrajetVl>().kmh = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().kmhActuel = vitesseFraction;
			instance.GetComponentInChildren<TrajetVl>().minTab = position;
			instance.GetComponentInChildren<TrajetVl>().mEntrePoint = metre;
			instance.GetComponentInChildren<TrajetVl>().interieur = interieur;
			instance.GetComponentInChildren<TrajetVl>().DemrageVl();
		}
	}
}
il reste une erreur pour faire un test

Code : Tout sélectionner

Assets/SCRIPT/Route/DebutRoute.cs(126,83): error CS1525: Unexpected symbol `trajets'
là je ne vois pas où est l'erreur au caractère 83 ?
ScreenShot020.jpg
ScreenShot020.jpg (79.03 Kio) Consulté 1916 fois

Avatar de l’utilisateur
jmhoubre
Messages : 856
Inscription : 05 Oct 2019 22:05

Re: [MY-AL] Modifier un serializable

Message par jmhoubre » 24 Mars 2022 13:10

Bonjour,

ne manque-t-il pas une virgule entre CreationVl (trajets[countTrajet].trajObject et trajets[countTrajet].mEntrePoint dans

Code : Tout sélectionner

for (int i = 0; i < trajets[countTrajet].departVl.Length; i++)
{
	CreationVl (trajets[countTrajet].trajObject trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
}
?

Répondre

Revenir vers « Scripting »