Search Unity

Reading file for input

Discussion in 'Scripting' started by Thordis, Aug 27, 2006.

  1. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    Hello, are there any input commands for reading text from a file?

    Thanks,
    ~Thordis
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    You just use .NET classes for that (System.IO namespace). Googling for something like "C# file reading" reveals lots of tutorials as well.

    Reading and writing files is disabled in the web player for security reasons.
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
  4. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    This may or may not be useful but is what I used for some testing purposes and and XML.

    Code (csharp):
    1.  
    2. import System;
    3. import System.IO;
    4. import System.Xml;
    5.  
    6. function Start(){
    7.  
    8.     var xmlFile = Application.dataPath + "/test.xml";
    9.    
    10.     if (File.Exists(xmlFile)){
    11.        
    12.        
    13.         var sr = new StreamReader(xmlFile);
    14.         var txt = sr.ReadToEnd();
    15.        
    16.         var xml = new XmlDocument();
    17.         xml.LoadXml(txt);
    18.        
    19.         Debug.Log(xml.FirstChild.ChildNodes.Count);
    20.        
    21.         for (var i=0;i<xml.FirstChild.ChildNodes.Count;i++){
    22.             Debug.Log(xml.FirstChild.ChildNodes[i].NodeType + "  " + xml.FirstChild.ChildNodes[i].OuterXml + "  " + xml.FirstChild.ChildNodes[i].Attributes.Count);
    23.         }
    24.     }
    25. }
    26.  
    And here is the XML I used for a sample:

    Code (csharp):
    1.  
    2. <test>
    3.     <some>
    4.         <someInfo att='someText'>And Some Crap</someInfo>
    5.     </some>
    6.  
    7.     <another someAtt='anAtt'>
    8.         <someInfo>And Some Crap</someInfo>
    9.     </another>
    10.  
    11.     <onemore>
    12.         <someInfo att='someText'>And Some Crap</someInfo>
    13.     </onemore>
    14. </test>
    15.  
    HTH someone.

    -- Clint
     
  5. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    Ah great, thank to both of you, I now have the objects in my game all being placed via a 'map file'.

    Thanks!
    ~Thordis
     
  6. pdsull

    pdsull

    Joined:
    Sep 16, 2007
    Posts:
    8
    Would you mind sharing a little more information on how you did this? Thanks!