Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D side-scroller with Unity

Discussion in 'iOS and tvOS' started by ej2009, Feb 16, 2010.

  1. ej2009

    ej2009

    Joined:
    Mar 2, 2009
    Posts:
    353
    Hi,

    I'm looking for tutorials on how to setup/make level design in Unity for 2D side scroller game using sprites.

    If anyone has some links or maybe some general ideas please share. I've never made side scroller game before so have hard time visualizing how I should set it all up in Unity editor.

    Thanks in advance.
     
  2. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    I would make my levels in Tiled (mapeditor.org).

    The format is a bit bloated, so I have a script that converts it to a csv-like format with only the stuff I need.

    Then you can read that and make meshes from it.
     
    Last edited: Dec 1, 2010
  3. ej2009

    ej2009

    Joined:
    Mar 2, 2009
    Posts:
    353
    Thanks mate!

    It might sound stupid, but do you create those meshes in Unity Editor script and can view in design mode or create them in the runtime?
     
  4. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    I guess both could work, but I do it at runtime. For a rather big level it takes 3 to 5 seconds, which is okay.

    If I did it in the editor I would have to run the script every time I changed something in the level, and I'm too lazy for that :)

    My conversion to the csv-type format is done with php btw. It checks all the maps that have changed (modification date > the corresponding csv's modification date) and only converts the changed maps.
     
  5. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    @Smag

    Don't suppose you'd be willing to share your script? would you?

    I might have a look at the file format and see if I can't whip something together...Could be very useful I would imagine.
     
  6. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    Sure, if I can remember once I'm behind my macbook.
     
  7. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    Here it is.
    It requires this : http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

    It converts tsx files (tilesets) to json files, and tmx files (the tilesets) to something csv-like. I could probably have used a binary format to save more space, but meh :)

    Code (csharp):
    1. <?php
    2. require_once("xml2json.php");
    3. ini_set("memory_limit","512M");
    4.  
    5.  
    6. $from = 'source/directory/';
    7. $to = 'destination/directory/';
    8. $backups = 'backups_directory/';
    9.  
    10. if ($dh = opendir($from)) {
    11.     while (($file = readdir($dh)) !== false) {
    12.         $from_path = $from . $file;
    13.         $to_path = $to . $file . '.txt';
    14.         set_time_limit(300);
    15.         if(is_file($from_path)  $file[0]!='.') {
    16.             $from_date = filemtime($from_path);
    17.             $to_date = 0;
    18.             if(is_file($to_path)) {
    19.                 $to_date = filemtime($to_path);
    20.             }
    21.             if($from_date > $to_date) {
    22.                 if($to_date > 0) rename($to_path, $backups . $file .'.txt');
    23.                 $explode = explode('.', $file);
    24.                 $ext = $explode[count($explode) - 1];
    25.                 $content = file_get_contents($from_path);
    26.                 $jsonContents = xml2json::transformXmlStringToJson($content);
    27.                 if($ext == 'tmx') {
    28.                    
    29.                     $o=json_decode($jsonContents);
    30.                     $layers=$o->map->layer;
    31.                     $a="@attributes";
    32.                     $output='';
    33.                    
    34.                     $output.=$o->map->$a->width.",";
    35.                     $output.=$o->map->$a->height.",";
    36.                     $output.=$o->map->$a->tilewidth.",";
    37.                     $output.=$o->map->$a->tileheight;
    38.                     for($i=0;$i<count($layers);$i++) {
    39.                         $output.="\n";
    40.                         if(is_array($layers)) {
    41.                             $layer=$layers[$i]->data->tile;
    42.                         } else {
    43.                             $layer=$layers->data->tile;
    44.                         }
    45.                        
    46.                         for($j=0;$j<count($layer);$j++) {
    47.                            
    48.                             if($j>0) $output.=",";
    49.                             $output.=$layer[$j]->$a->gid;
    50.                         }
    51.                        
    52.                     }
    53.                    
    54.                     $handle = fopen($to_path, 'w');
    55.                     fwrite($handle, $output);
    56.                     fclose($handle);
    57.                     echo 'Converted map : '.$file.'
    58. ';
    59.                    
    60.                    
    61.                 } elseif ($ext == 'tsx') {
    62.                     $handle = fopen($to_path, 'w');
    63.                     fwrite($handle, $jsonContents);
    64.                     fclose($handle);
    65.                     echo 'Converted tileset : '.$file.'
    66. ';
    67.                 }
    68.             } else {
    69.                 echo 'Skipped file : '.$file.'
    70. ';
    71.             }
    72.         }
    73.        
    74.     }
    75.     closedir($dh);
    76. }
    77.  
    78. ?>
     
  8. AgentFoley

    AgentFoley

    Joined:
    May 14, 2010
    Posts:
    12
    Hey has anyone successfully gotten the PHP script to work? Also how do you integrate the json data into unity?
    Plz help!
     
  9. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    Actually, it doesn't convert to JSON anymore. That was my first plan, but then I changed the format to csv, but I think it still uses the json as an intermediary step because I used JSON first.

    What exactly are you having trouble with?
     
  10. AgentFoley

    AgentFoley

    Joined:
    May 14, 2010
    Posts:
    12
    I am having trouble building 2d levels with Mapeditor and getting them into unity. One way I was trying this was exporting to PNG format and drawing in all the collider's.

    Is there any way you can walk me through importing the data generated by your PHP script into unity?
     
  11. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    Well, I can't give you my code, because it has dependencies on other classes of mine, and I don't want to give away my entire codebase :)
    PNG's are a bad idea since it will increase your filesize a lot if you have many levels.

    What I do is I loop through the csv format, and construct meshes of 10x10 tiles. It's not that difficult - it's basically the same as creating sprites, but you just add a little more to each one. Figuring out the right coordinates and uv's based on the numbers in the csv can be a bit tricky though.

    Good luck!
     
  12. pepworks

    pepworks

    Joined:
    Jan 18, 2010
    Posts:
    19
    I can recommend to use sprite manager 2 (SM2) and a mixture out of
    not rendered 3D meshes (for the collision), plain textures and sprites.
    (Use an 'orthographic' camera to get this 2D arcade feeling ;) )

    "PEP the dragon" is created that way and as it uses mostly 2D artwork
    it runs fine, even on older apple devices.

    You can see a flash demo on http://www.pepworks.com/ or download
    the LITE version in the app store to see how it look and feels.

    best regards,

    pep
     
  13. gonzokawasaki

    gonzokawasaki

    Joined:
    May 6, 2009
    Posts:
    36
    i defo agree SM2 will cut out a lot of optimization work and donkey work. i am currently trying to create a side scroller for iphone and i realised early on that the draw calls would kill the whole game. i have been using SM2 for just a day now and have got great results already.




    death by pixel
    www.deathbypixel.co.uk