Search Unity

Displaying text from MS-Excel file?

Discussion in 'Scripting' started by aswinindra, Apr 22, 2008.

  1. aswinindra

    aswinindra

    Joined:
    Oct 24, 2007
    Posts:
    38
    Hello Again,
    I hope you are not boring with me.
    I have these object attributes in MS Excel.
    I want to display this attribute (text, say : building name, height) when I click particular mesh in Unity.

    I am success with displaying via GUI Text, I haven't found how to access text from external file, particularly text from spreadsheet (or database).

    Anybody with same experience?
    Thank for any kind comment.
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That seems like overkill to me. Excel can easily save files as CSV (comma-separated values) which are trivial to read in. Save this file as a text file in your project folder, and it becomes a Text asset which can be accessed via a script. I've never used TextAssets myself, but I think the data comes in as a string, which you can then split up into separate data bits. For example:
    Code (csharp):
    1.  
    2. var myText : String= "name,height\nname2,height2"; //your CSV would look something like this
    3.  
    4. var lines : String[] = myText.Split("\n"[0]);
    5. for (l=0;l<lines.length;l++) {
    6. var values : String[] = lines[l].Split(","[0]);
    7. if (values.length<2) { //replace 2 with however many values per line you expect
    8. print("Error - not enough values in this line.");
    9. continue;
    10. }
    11. var name : String = values[0];
    12. var height : String = values[1];
    13. //if you have more values to read in, read them in here
    14.  
    15. //do whatever with your data here
    16. }
    17.  
    Hope that helps.
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Ah. I didn't realise it was a one-time export solution being sought for.
     
  5. aswinindra

    aswinindra

    Joined:
    Oct 24, 2007
    Posts:
    38
    Hi All,
    Thank for your kind reply. I will try to use text-based file instead.

    Really appreciated.