There's more...

We'll learn ways to implement the LoadMap function by using the .map file format as an example:

  1. 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); 
    } 
}
  1. 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; 
  1. Read the header of the file containing its height and width:
line = strmRdr.ReadLine();// non-important line line = strmRdr.ReadLine();// height ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.