Search Unity

I made a script to help with Tilemaps using Custom Physics Shapes.

Discussion in '2D' started by Pixelith, Feb 12, 2020.

  1. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    580
    I made a Reddit post about my issues with the Custom Physics Shapes option in conjunction with Tilemap Collider component.

    The tldr: I wanted to set tiles that didn't have a Custom Physics Shape to have no collision at all, so I made a script here.

    So I prefer very specific collision. I also like to do the minimum in work required. So my sprite sheet doesn't have the backgrounds and grounds separate. No problem, just use Custom Physics Shapes. You can set the slopes to be slopes, you don't have to spend hours in Photoshop to separate everything. Perfect. The issue here is even if you don't define a Custom Physics Shape you will get a generated collider. You can negate this by going into your tile folder and setting all non ground tiles to Collider Type: None. If you have 1000 tiles though, this gets tedious. So I wanted an option to say "Hey, if this doesn't have a Custom Physics Shape, automatically set it to Collider Type: None." Unity doesn't come with this feature. So I made a very super simple script that just needs an import and opening a window. The only requirement on your end is setting up the Custom Physics Shapes. I find this to be a great solution which will allow you to keep one sprite sheet, one Tilemap, and minimal headache. No multiple Tilemaps with different layers just to build your level. Easy and streamline.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    If you are creating Tiles by dragging and dropping your Spritesheets onto a Tile Palette, you could try the following script:

    Code (CSharp):
    1. using UnityEditor.Tilemaps;
    2. using UnityEngine;
    3. using UnityEngine.Tilemaps;
    4.  
    5. public class CreateTilesWithColliderNone
    6. {
    7.     [CreateTileFromPalette]
    8.     public static TileBase CreateTileWithColliderNone(Sprite sprite)
    9.     {
    10.         var tile = TileUtility.DefaultTile(sprite) as Tile;
    11.         if (tile.sprite == null || tile.sprite.GetPhysicsShapeCount() == 0)
    12.         {
    13.             tile.colliderType = Tile.ColliderType.None;
    14.         }
    15.         return tile;
    16.     }
    17. }
    18.  
    With the script, you can set that as the "Tile Palette Create Tile Method" under Preferences for creating new Tiles:

    upload_2020-2-13_14-33-45.png
     
  3. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    580
    That's actually really useful in that I didn't know I could create custom import settings that easily. Normally I would create them this way, but when purchasing a tile set from the asset store they are normally already created. Thankfully my script works for what I need, but it only works after tile creation (not too much of a big deal) so I might try to incorporate this into it. Thanks for another approach!
     
  4. dxdesjardins

    dxdesjardins

    Joined:
    Aug 4, 2021
    Posts:
    5
    If you are reading this make sure that the Tilemap Texture2D Asset has "GENERATE PHYSICS SHAPE" disabled to get this to work. I got the above script to work.
     
    Last edited: Apr 6, 2022