C # Unity, foreach of prefabs

1

Good morning. I have a function that reads data from an xml that is this:

private string xmlPath;

    public XmlManager(string xmlPath) {
        this.xmlPath = xmlPath;
}
 public Datos ReadXmlTest() {
        XmlSerializer serializer = new XmlSerializer(typeof(Datos));
        StreamReader reader = new StreamReader(xmlPath);
        Datos data = (Datos)serializer.Deserialize(reader);
        reader.Close();

        return data;
    }

What I do with that is to get some routes of some photos and save all the routes in data.

Then I have this class

using System.Diagnostics;
using UnityEngine;

public class AppButton : MonoBehaviour {

    private Juego game;

    public void Init(Juego game) {
        this.game = game;

    }

    private void RunApp() {
        Process.Start(game.ruta);
    }
}

and then I have this class where the foreach is

private Datos data;
    public GameObject Button;

    void Awake() {
        string path = "C:/Users/datos.xml";
        XmlManager xmlMng = new XmlManager(path);

        data = xmlMng.ReadXmlTest();

        foreach (var juego in data.Juegos) {
            GameObject newButton = (GameObject)Instantiate(Button);
            newButton.GetComponent<AppButton>();
            //
            //

        }
    }

In the last class where they are // I have to Call the AppButton Init with a game but I do not know how to do it. And apart from that, how can I make a button with the image that exists in the xml routes for each element that goes on in the xml?

    
asked by JuanPerez 30.03.2017 в 17:30
source

1 answer

1

Take it as a pseudocode, but more or less I think that is what you are looking for:

    //..
    foreach (var juego in data.Juegos) {

        GameObject newButton = (GameObject)Instantiate(Button);

        //newButton.GetComponent<AppButton>();
        //
        //

        string FilePath <- segun entiendo usted tiene en Datos las rutas,
                           pues la mete aqui la ruta si esta cambia en
                           cada iterecion tiene que cambiarla tambien
                           para que cambie la ruta de la imagen

        Sprite imageSprite = new Sprite();

        //Usamos el metodo para crear la textura
        Texture2D SpriteTexture = CTextura(FilePath); 

        //creamos es sprite
        imageSprite = Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height),new Vector2(0,0),100.0f);

        //newButton.GetComponent<AppButton>().image.sprite = imageSprite;

        newButton.image.sprite = imageSprite;//<- añadimos la imagen/sprite
        newButton.GetComponent<AppButton>().Init(Juego game)// <- tiene pasarle el parametro del tipo Juego


    }
}


public Texture2D CTextura(string Path) {

     Texture2D Texura2D;
     byte[] FileData;

     if (File.Exists(Path)){

       FileData = File.ReadAllBytes(Path);
       Texura2D = new Texture2D(2, 2);  

       if (Texura2D.LoadImage(FileData))           
         return Textura2D;                 
     }  
     return null;                     
}

You may be interested in learning about Resources.Load

    
answered by 30.03.2017 / 18:41
source