We'll learn ways to implement the LoadMap function by using the .map file format as an example:
- Define the function and create a StreamReader object for reading the file:
private void LoadMap(string filename) { string path = Application.dataPath + "/" + mapsDir + "/" + filename; try { StreamReader strmRdr = new StreamReader(path); using (strmRdr) { // next steps in here } } catch (Exception e) { Debug.LogException(e); } }
- Declare and initialize the necessary variables:
int j = 0; int i = 0; int id = 0; string line; Vector3 position = Vector3.zero; Vector3 scale = Vector3.zero;
- Read the header of the file containing its height and width:
line = strmRdr.ReadLine();// non-important line line = strmRdr.ReadLine();// height ...