Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     public BitmapFont(string pathPNG, string pathXML, GameObject gameObject)
     {
         this.gameObject = gameObject;
         sprites = new Dictionary();
         yoffsets_xadvances = new Dictionary();
         rects = new Dictionary();

         Texture2D texture = Resources.Load(pathPNG);
         float height = texture.height;
         TextAsset xml = Resources.Load(pathXML);

         XmlDocument test = new XmlDocument();
         test.Load(new StringReader(xml.text));

         //test.LoadXml(new StringReader(xml.text).ReadToEnd());
         //string[] keys = new string[] {"x","y","width","height","yoffset","xadvance","letter"};

         foreach (XmlNode node in test.DocumentElement.ChildNodes)
         {
             XmlAttributeCollection collection = node.Attributes;
             Rect rect = new Rect(float.Parse(collection.Item(0).Value), height - float.Parse(collection.Item(1).Value) - float.Parse(collection.Item(3).Value), float.Parse(collection.Item(2).Value), float.Parse(collection.Item(3).Value));
             rects.Add(collection.Item(6).Value, rect);
             sprites.Add(collection.Item(6).Value, Sprite.Create(texture, rect, Vector2.zero));
             yoffsets_xadvances.Add(collection.Item(6).Value, new Vector2(float.Parse(collection.Item(4).Value), float.Parse(collection.Item(5).Value)));
         }
     }