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:

         private void ImportMeshesFromXml(XDocument xml)
         {
             var meshData = xml.Root.Elements("ImportMesh");
             foreach (var mesh in meshData)
             {
                 // We're going to create/write a file that contains our mesh data as a Wavefront Obj file
                 // The actual mesh will be imported from this Obj file

                 string name = mesh.Attribute("filename").Value;
                 string data = mesh.Value;

                 // The data is in base64 format. We need it as a raw string.
                 string raw = ImportUtils.Base64ToString(data);

                 // Save and import the asset
                 string pathToMesh = "Assets/Tiled2Unity/Meshes/" + name;
                 ImportUtils.ReadyToWrite(pathToMesh);
                 File.WriteAllText(pathToMesh, raw, Encoding.UTF8);
                 AssetDatabase.ImportAsset(pathToMesh, ImportAssetOptions.ForceSynchronousImport);
             }
         }