Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I turn a PNG or other image file into a game map/tile map?

Discussion in '2D' started by pixelduh, Apr 12, 2021.

  1. pixelduh

    pixelduh

    Joined:
    May 20, 2020
    Posts:
    1
    I don't know exactly how to articulate my question, as I'm very new to unity and game dev in general, but does there exist some sort of function that takes an image file (such as a png) and analyzes the hexadecimal color value of each pixel and assigns a material or a specific texture set to each color? I.e., is there a way to pre-plan out a game world in photoshop or paint and then import it into unity in some way to convert it to a partially textured/tiled game world/ game map?

    I have included a picture to hopefully clarify what I mean.
    Thanks!

    Pixelduh how do i do this.png
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    the simple answer: no

    the complex answer: there can be if someone codes it
     
  3. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    It is possible but probably more work than doing it a different way. All that stuff can be done with Unity's tile maps.

    If you really wanted to do this, you'd probably end up writing an editor script that would analyze the pixel data and translate it into a tilemap. But since you can just set it up with a tilemap directly, that's going to be the easier approach. There are a bunch of YouTube tutorials on how to get up and running with tilemaps.

    To have collision on some tiles but not others, the best way I've found to do it is to have multiple tilemaps. So your setup would be something like this:

    - collision tilemap - add a "TilemapCollider" component to this tilemap
    - no-collision tilemap - no TilemapCollider component

    Then you just put all your water or mountain tiles onto the collision tilemap, and your non-collision tiles into the non-collision tilemap.

    As for sound, that's not something built-in to the tilemap itself. If you want a sound playing every time your character moves, you'd have a script on the character itself that, every time it moves, calculates which tile you're on and then plays the sound accordingly. Basically you can convert the position of your character, a Vector3, into tilemap coordinates (Vector3Int) and then call tilemap.GetTile on it and see which tile you're on.