2017-04-03 43 views
-1

我有一個xml,我保留了一些圖像和一些exe的路徑。 我需要我的程序來讀取XML並創建儘可能多的按鈕,因爲有元素,分配每個按鈕的圖像,並給它的按鈕來運行.exe我的程序讀取XML並創建按鈕。我需要的按鈕,有圖像,當.exe文件運行.exe文件我怎樣才能在C#中運行.exe Unity

我的類讀取XML

using System.IO; 
using System.Xml.Serialization; 

public class XmlManager { 

    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; 
    } 
} 

我的類來生成按鈕,並把德圖像按鈕

using System.IO; 
using UnityEngine; 
using UnityEngine.UI; 

public class AppLogic : MonoBehaviour { 

    [SerializeField] 
    private Transform layout; 

    [SerializeField] 
    private Button buttonPrefab; 

    private Datos data; 

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

     data = xmlMng.ReadXmlTest(); 

     foreach (var juego in data.Juegos) { 
      Button newButton = Instantiate(buttonPrefab); 
      newButton.transform.SetParent(layout); 
      newButton.GetComponent<AppButton>(); 

      Sprite imageSprite = new Sprite(); 
      Texture2D SpriteTexture = Texture(path); 
      imageSprite = Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0, 0), 100.0f); 
      newButton.image.sprite = imageSprite; 


     } 
    } 

    public Texture2D Texture(string path) { 

     Texture2D Texture2D; 
     byte[] FileData; 

     if (File.Exists(path)) { 

      FileData = File.ReadAllBytes(path); 
      Texture2D = new Texture2D(1, 1); 

      if (Texture2D.LoadImage(FileData)) 
       return Texture2D; 

     } 
     return null; 
    } 
} 

我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<Datos> 
    <dato> 

    <play> 
     <ruta>D:/exe.exe</ruta> 
     <img>C:/png.png</img> 
    </play> 


    <play> 
     <ruta>D:/exe1.exe</ruta> 
     <img>C:/png1.png</img> 
    </play> 

    </dato> 
</Datos> 

我的程序時創建的按鈕放在統一的默認圖像。我認爲這是因爲它讀取所有的XML而不僅僅是一個圖像。

我希望你能理解我,我西班牙人jejeje

+0

可能的重複[如何從C#運行.exe](http://stackoverflow.com/questions/12636991/how-to-run-exe-from-c-sharp) – PJvG

+0

有許多類似的問題堆棧溢出:http://stackoverflow.com/search?q=c%23+run+exe – PJvG

回答

3

如果你想運行在C#中的.exe,你應該使用System.Diagnostics

有了它,你可以創建一個新的Process和火起來的.exe

這個例子是從MSDN文檔

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
    class MyProcess 
    { 
     public static void Main() 
     { 
      Process myProcess = new Process(); 

      try 
      { 
       myProcess.StartInfo.UseShellExecute = false; 
       // You can start any process, HelloWorld is a do-nothing example. 
       myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"; 
       myProcess.StartInfo.CreateNoWindow = true; 
       myProcess.Start(); 
       // This code assumes the process you are starting will terminate itself. 
       // Given that is is started without a window so you cannot terminate it 
       // on the desktop, it must terminate itself or you can do it programmatically 
       // from this application using the Kill method. 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
      } 
     } 
    } 
} 

Read about it here.