Problème sélection ComboBox

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
Waskira
Messages : 7
Inscription : 28 Jan 2015 17:42

Problème sélection ComboBox

Message par Waskira » 16 Avr 2015 17:12

Bonjour à tous !
Je m'excuse par avance pour ce pavé ! :oops:

Je créé une ComboBox et chaque sélection dans une combo box correspond au nom d'un matériel dans un fichier XML. Il y a : -Un fichier XML qui récupère le nom d'un équipement.
-Un fichier portant le nom de l'équipement avec ses propres info dedans.
Je voudrais que, lorque qu'un équipement est sélectionné dans la CB, effectuer une recherche du fichier XML du même nom et en fonction du contenu, créer des GUI.
Mon problème est que je n'arrive pas à démarrer... (Ou coder cette recherche ? En passant par quoi ?)

Un peu de code pourrait aussi aider tout de même !

Ce script créé la combobox et la complète en fonction du nombre de Matériel enregistré dans le fichier ListeEquipement.xml

Code : Tout sélectionner

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class ComboBoxTest : MonoBehaviour
{
GUIContent[] comboBoxList;
private ComboBox comboBoxControl;
private GUIStyle listStyle = new GUIStyle();
private string contenu;
private int i = 0, a = 0;
public CreerFaderEtText AppelFonctionFader;

private void Start()
{
XmlDocument XmlDoc = new XmlDocument ();
XmlDoc.Load (@"C:\\Users\\dupuis\\Desktop\\XMLfiles\\ListeEquipement.xml");
XmlElement Racine = XmlDoc.DocumentElement;
XmlNodeList listEquipement = XmlDoc.GetElementsByTagName("Equipement");

foreach (XmlElement node in listEquipement)
{
a++;
}

comboBoxList = new GUIContent[a];

foreach (XmlElement node in listEquipement)
{
contenu = node.InnerText;
comboBoxList [i++] = new GUIContent (contenu);
}

listStyle.normal.textColor = Color.white; 
listStyle.onHover.background =
listStyle.hover.background = new Texture2D(2, 2);
listStyle.padding.left =
listStyle.padding.right =
listStyle.padding.top =
listStyle.padding.bottom = 4;
comboBoxControl = new ComboBox(new Rect(1220, 35, 100, 20), comboBoxList[0], comboBoxList, "button", "box", listStyle);
}

private void OnGUI () 
{
comboBoxControl.Show();
GUI.Label (new Rect (1220, 10, 100, 20), "Equipement : ");
}
}

Celui-ci est la gestion de la ComboBox

Code : Tout sélectionner

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class ComboBox
{
private static bool forceToUnShow = false; 
private static int useControlID = -1;
private bool isClickedComboButton = false;
private int selectedItemIndex = 0;
private Rect rect;
private GUIContent buttonContent;
private GUIContent[] listContent;
private string buttonStyle;
private string boxStyle;
private GUIStyle listStyle;
//private GameObject GO = new GameObject();

public ComboBox( Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle )
{
this.rect = rect;
this.buttonContent = buttonContent;
this.listContent = listContent;
this.buttonStyle = "button";
this.boxStyle = "box";
this.listStyle = listStyle;
}

public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle, string boxStyle, GUIStyle listStyle)
{
this.rect = rect;
this.buttonContent = buttonContent;
this.listContent = listContent;
this.buttonStyle = buttonStyle;
this.boxStyle = boxStyle;
this.listStyle = listStyle;
}

public int Show()
{
if( forceToUnShow )
{
forceToUnShow = false;
isClickedComboButton = false;
}

bool done = false;
int controlID = GUIUtility.GetControlID( FocusType.Passive ); 

switch( Event.current.GetTypeForControl(controlID))
{
case EventType.mouseUp:
{
if( isClickedComboButton )
{
done = true;
}
}
break;
} 
if( GUI.Button( rect, buttonContent, buttonStyle ) )
{
if( useControlID == -1 )
{
useControlID = controlID;
isClickedComboButton = false;
}

if( useControlID != controlID )
{
forceToUnShow = true;
useControlID = controlID;
}
isClickedComboButton = true;
}

if (isClickedComboButton)
{
Rect listRect = new Rect (rect.x, rect.y + listStyle.CalcHeight (listContent [0], 1.0f),
rect.width, listStyle.CalcHeight (listContent [0], 1.0f) * listContent.Length);
GUI.Box (listRect, "", boxStyle);
int newSelectedItemIndex = GUI.SelectionGrid (listRect, selectedItemIndex, listContent, 1, listStyle);

if (newSelectedItemIndex != selectedItemIndex)
{
selectedItemIndex = newSelectedItemIndex;
buttonContent = listContent[selectedItemIndex];
}
}

if (done)
{
isClickedComboButton = false;
}
return selectedItemIndex;
}

public int SelectedItemIndex
{
get
{
return selectedItemIndex;
}

set
{
selectedItemIndex = value;
}
}
}

Je voudrais appeler ma fonction createFader() lorsque je clique sur un équipement pour générer le nombre de fader qui varie en fonction de chaque équipement.

Code : Tout sélectionner

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class CreerFaderEtText : MonoBehaviour
{ 
private GameObject Fader, PourcentageFader,TextNOMunivers, NOMunivers;
private string EtiquetteUnivers;
private int nbuniv, AxeX;
private CreerXML RecupINT;
private float Pourc;
public string[] Contenu;

void Start()
{
//empty
}

void Update () 
{
for (int i=0; i < nbuniv; i++)
{
Pourc = GameObject.Find ("slider" + i).GetComponent<Slider> ().value;
GameObject.Find("Text"+i).GetComponent<Text> ().text = "" + Math.Round (Pourc, 1) + " %";
}
}

public void OnLevelWasLoaded()
{
createFader();
}

public void AppelText(int i,int AxeX, int AxeY)
{
PourcentageFader = (GameObject)Instantiate (Resources.Load ("PourcentageFader"));
PourcentageFader.transform.parent = GameObject.Find ("Panel").transform;
PourcentageFader.transform.localPosition = new Vector3 (AxeX + 70,AxeY, 30);
PourcentageFader.name = "Text" + i;
}

public void AppelSlider(int i,int AxeX, int AxeY)
{
Fader = (GameObject)Instantiate (Resources.Load ("PrefabFader"));
Fader.transform.parent = GameObject.Find ("Panel").transform;
Fader.transform.localPosition = new Vector3 (AxeX + 70,AxeY, 30);
Fader.name = "slider"+i;
}

public void createFader()
{
RecupINT = GetComponentInChildren<CreerXML> ();
nbuniv = PlayerPrefs.GetInt ("");
AxeX = -600;
for (int i = 0; i < nbuniv; i++)
{
AxeX = AxeX + 70;
AppelSlider (i, AxeX, -40);
AppelText (i, AxeX, -140);
}
}
}
J'ai pensé passer par quelque chose de ce style mais je ne suis pas sûr du tout de mon coup et je ne sais pas où l'insérer :

Code : Tout sélectionner

XmlDocument XmlDoc = new XmlDocument ();
XmlDoc.Load(@"C:\\Users\\dupuis\\Desktop\\XMLfiles\\" + comboBoxControl + ".xml");
XmlElement Racine = XmlDoc.DocumentElement;
XmlNodeList listUnivers = XmlDoc.GetElementsByTagName("Univers");
foreach (XmlElement node in listUnivers)
{
AppelFonctionFader.createFader();
}
Je peux vous apporter des précisions si jamais il y en a besoin, j'ai cherché dans la doc unity mais je j'ai pas trouvé mon bonheur (peut être AddComponent ?).
Merci d'avance !

Cordialement.

Répondre

Revenir vers « (C#) CSharp »