Search Unity

Free Code - Load a map from ASCII

Discussion in 'Scripting' started by JohnnyA, Oct 15, 2011.

  1. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Just wrote a bit of code that might be useful for others. It takes a fixed width ascii file and uses it to populate a scene with prefabs. Handy for all those people doing 2D/2.5D stuff for iPhone.

    Web Player: http://www.jnamobile.com/asciimaploader/AsciiMapLoader.html

    Turns This:
    Code (csharp):
    1.  
    2.                   *    
    3.                        
    4.                        
    5.                   #    
    6.                ##      
    7.   #*#    I              
    8.          Y   #          
    9.          Y              
    10. ========================
    11.  
    Into This:
    $MapOne.png


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class AsciiMapReader : MonoBehaviour {
    7.    
    8.     public int tileWidth = 1;                                                                                    
    9.     public int tileHeight = 1;
    10.     public float xOffset = -5;                                                                                    
    11.     public float yOffset = -5;                                                                                    
    12.     public GameObject[] prefabs;
    13.     public string characters;
    14.     public string[] filePathAndName;
    15.     public bool loadFromURL;    
    16.     public GUIText errorText;
    17.    
    18.     private ArrayList mapObjects;
    19.     private int mapWidth;    
    20.     private string map;
    21.     private WWW request;
    22.    
    23.     void Start() {
    24.         // Sample loading
    25.         LoadMap(filePathAndName[0]);
    26.         StartCoroutine(WaitForSecondsThenLoadMap(7,1));
    27.         StartCoroutine(WaitForSecondsThenLoadMap(14,2));
    28.     }
    29.    
    30.     private IEnumerator WaitForSecondsThenLoadMap(int wait, int map) {
    31.         yield return new WaitForSeconds(wait);
    32.         DestroyMap(0);
    33.         LoadMap(filePathAndName[map]);        
    34.     }
    35.    
    36.     public void LoadMap(string filePathAndName) {
    37.         if (loadFromURL) {
    38.             StartCoroutine(AsyncLoadMap(filePathAndName));
    39.         } else {
    40.             string map = ReadMap(filePathAndName);
    41.             ParseMap(map);
    42.         }
    43.     }
    44.      
    45.     public void DestroyMap(float delay) {
    46.         if (mapObjects != null) {
    47.             foreach (GameObject gameObject in mapObjects) {
    48.                 GameObject.Destroy(gameObject, delay);
    49.             }
    50.         }
    51.         mapObjects = null;
    52.     }  
    53.        
    54.     public string getMappingsAsString() {
    55.         string result = "";
    56.         for (int i = 0; i < characters.Length  i < prefabs.Length; i++) {
    57.             result += characters[i] + " = " + prefabs[i].name;
    58.             if (i != prefabs.Length - 1) {
    59.                 result += ", ";
    60.             } else {
    61.                 result += ".";
    62.             }
    63.         }
    64.         if (characters.Length > prefabs.Length) {
    65.             result += " There are " + (characters.Length - prefabs.Length) + " unmapped characters.";
    66.         } else if (characters.Length < prefabs.Length) {
    67.             result += " There are " + (prefabs.Length - characters.Length) + " unmapped prefabs.";
    68.         }
    69.         return result;
    70.     }
    71.    
    72.     private void ParseMap(string map) {
    73.     float x = 0 , y = 0;       
    74.     GameObject prefab;
    75.     Object currentObject;  
    76.     mapObjects = new ArrayList();          
    77.     for (int i = 0; i < map.Length ; i++) {
    78.             prefab = getPrefabForCharacter(map[i]);
    79.             if (prefab != null) {
    80.                 currentObject = GameObject.Instantiate(prefab, new Vector3((x * tileWidth) + xOffset, (y * tileHeight) + yOffset, 1), Quaternion.identity);
    81.                     mapObjects.Add(currentObject);
    82.             }
    83.             x++;
    84.             if (x == mapWidth) {
    85.                 x = 0; y++;
    86.         }
    87.         }
    88.     }
    89.  
    90.     private GameObject getPrefabForCharacter(char c) {
    91.         for (int i = 0; i < characters.Length  i < prefabs.Length; i++) {
    92.             if (characters[i] == c) {
    93.                 return (prefabs[i]);
    94.             }
    95.         }
    96.         return null;
    97.     }
    98.    
    99.     private string ReadMap(string filePathAndName) {
    100.             StreamReader reader = new StreamReader(filePathAndName);
    101.             string fileContents = reader.ReadToEnd();
    102.             reader.Close();
    103.             mapWidth = fileContents.IndexOf("\n") + 1;
    104.             return fileContents;
    105.     }
    106.    
    107.     IEnumerator AsyncLoadMap(string url)
    108.     {
    109.         if (request == null) {
    110.             request = new WWW(url);
    111.         }
    112.         yield return request;
    113.         if (request.error== null ) {
    114.             mapWidth = request.text.IndexOf("\n") + 1;
    115.             ParseMap(request.text);
    116.         } else {
    117.             Debug.Log("Error: " + request.error);
    118.             errorText.text = "Error: " + request.error;
    119.         }
    120.         request = null;
    121.     }
    122. }
    123.  
    Specify your mapping using:
    public GameObject[] prefabs;
    public string characters;

    They are mapped in order that they appear, you can get a description of the mapping using: getMappingsAsString().

    Unmapped characters mean do nothing.

    License is MIT, so you can do with it what you will. Credits or links to www.jnamobile.com are appreciated but not required.
     
  2. Me, Corey

    Me, Corey

    Joined:
    Oct 5, 2011
    Posts:
    61
    Impressive.
     
  3. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
    Whoa.
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    One bump, just in case someone might find it useful :)
     
  5. a28306

    a28306

    Joined:
    Sep 28, 2011
    Posts:
    11
    Very useful.Mark it.
     
  6. rungy

    rungy

    Joined:
    Oct 25, 2011
    Posts:
    23
    genius !
     
  7. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Good one!