De retour sur mon projet pour récup une base de donnée mysql et jouer avec sur Unity dans un tableau.
Maintenant j'arrive à récup la liste et à l'envoyer dans mon tableau sous forme de boutons. J'ai un bouton de tri pour trier par rang. J'ai pas d'erreur et on va dire que dans l'ensemble ça marche... sauf que c'est très capricieux
Voici ce que ça donne en lançant l'appli (récup de la liste en Start) puis quand je clique 3 fois sur le bouton pour trier. En start j'ai un problème d'affichage, après le 1er clique... ce n'est pas trié. Ensuite ça tri mais ça s'affiche bizarrement et enfin ça s'affiche mais j'ai toujours la dernière de la liste qui se met au début, c'est systématique...
Quelqu'un a une idée du pourquoi du comment ?
Code : Tout sélectionner
public class Web : MonoBehaviour
{
public string nameConf ;
public string Mdp ;
public string Valide ;
public string listeConfTri ;
//public Confederations ConfUnique;
string tableauListeConf;
public GameObject BoutonNew;
public Transform BoutonTransform;
public Text BoutonText;
public GameObject BoutonParent;
public Text ListeConf;
void Start()
{
// StartCoroutine(GetListeConfTri(listeConfTri));
StartCoroutine(GetListeConf(BoutonParent.transform));
//StartCoroutine(RegisterConf(nameConf, Mdp, Valide));
}
IEnumerator GetListeConf(Transform newParent)
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost/CTDTCommunityApp/ListeConfNom.php");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
tableauListeConf = www.downloadHandler.text;
tableauListeConf = "{\"Items\":" + tableauListeConf + "}";
Debug.Log(tableauListeConf);
Confederations[] Conf = JsonHelper.FromJson<Confederations>(tableauListeConf);
int listelong = Conf.Length;
int compteur;
int indexConf = 0;
for (compteur = 1; compteur <= listelong; compteur++)
{
Instantiate(BoutonNew, newParent);
BoutonTransform = BoutonNew.transform.GetChild(0);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Nom;
BoutonTransform = BoutonNew.transform.GetChild(1);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].NbrMembre + "/30";
BoutonTransform = BoutonNew.transform.GetChild(2);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Rang;
BoutonTransform = BoutonNew.transform.GetChild(3);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Langue;
indexConf++;
}
}
}
IEnumerator RegisterConf(string nameConf2, string Mdp2, string Valide2)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("registerNameConf", nameConf2));
formData.Add(new MultipartFormDataSection("registerMdp", Mdp2));
formData.Add(new MultipartFormDataSection("registerValide", Valide2));
UnityWebRequest www = UnityWebRequest.Post("http://localhost/CTDTCommunityApp/RegisterConf.php", formData);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Fait");
}
}
IEnumerator GetListeConfTri(string listeConfTri)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("requeteTri", listeConfTri));
UnityWebRequest www = UnityWebRequest.Post("http://localhost/CTDTCommunityApp/GetListeNomTri.php", formData);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
tableauListeConf = www.downloadHandler.text;
tableauListeConf = "{\"Items\":" + tableauListeConf + "}";
}
}
public void CreationBouton(Transform newParent)
{
Confederations[] Conf = JsonHelper.FromJson<Confederations>(tableauListeConf);
int listelong = Conf.Length;
int compteur;
int indexConf = 0;
foreach (Transform child in BoutonParent.transform)
{
Destroy(child.gameObject);
}
for (compteur = 1; compteur <= listelong; compteur++)
{
Instantiate(BoutonNew, newParent);
BoutonTransform = BoutonNew.transform.GetChild(0);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Nom;
BoutonTransform = BoutonNew.transform.GetChild(1);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].NbrMembre + "/30";
BoutonTransform = BoutonNew.transform.GetChild(2);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Rang;
BoutonTransform = BoutonNew.transform.GetChild(3);
BoutonText = BoutonTransform.GetComponent<Text>();
BoutonText.text = Conf[indexConf].Langue;
indexConf++;
}
}
public void BoutonTriRang()
{
listeConfTri = "SELECT Nom, Langue, Rang, NbrMembre FROM confederations WHERE valide = 1 ORDER BY Rang DESC";
StartCoroutine(GetListeConfTri(listeConfTri));
Debug.Log (tableauListeConf);
CreationBouton(BoutonParent.transform);
}
public void BoutonRegisterConf()
{
nameConf = "ConfTest";
Mdp = "12345";
Valide = "1";
StartCoroutine(RegisterConf(nameConf, Mdp, Valide));
}
}
[Serializable]
public class Confederations
{
public string Nom;
public string Langue;
public string Rang;
public string NbrMembre;
}
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}