How To Make Custom Rule Tiles In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
If you ever worked with tilemaps in unity I’m sure you come across with the tilemap extras package and its rule tile, it is a very powerful tool that can save you a lot of time. But have you ever noticed the “create custom rule tile script” option in the create asset menu, and wondered how it works, and how do you use it? In this video I’m going to show you where custom rule tiles are useful, how they work, and how you can create your own ones. The default rule tiles in unity are really useful, but there is one thing that they can’t do. They don’t interact with other tiles other than themselves. But with custom tiles you can change that! You can make a tile that connects to a specific tile other than just itself. Or you can make a tile which will completely change to a different looking one if there’s another tile next to it. Or you could make a tile that will serve as a border to other tiles. Only your imagination is the limit here. In order to create your own custom tile you need to understand how the tile checks for its neighboring tiles. It checks for every tile next to it, and will return true or false depending on the rule you specified. If you didn’t specify a rule it will always return true, if the rule is set to “This”, it will return true if the tile at that position is the same as this tile, and the “NotThis” rule will return true if the tile at that position is anything but this tile. The script will choose that sprite where every neighboring tile rule will return true, if none of the tiles match, it will return the default sprite. Now that we understand how rule tiles work we can start programming our own tile. We’ll create a tile which will have three extra options: An option to check for any tile An option to check for specific tiles And an option to check if there’s an empty space To create the script go to the create asset menu and click on the create custom rule tile script button that I mentioned at the beginning of the video. When we open this script there will be two methods already, the first one is the neighbor class, where we will write our custom rules, and the second is the RuleMatch method, where we will, well.... match our rules. Let’s create our custom rules in the Neighbor class. This class just contains static integers with a value, this class is only there to name your rules. Rules 1 and 2 are the default “This“ and ”NotThis” if you want to overwrite these start from 1, if you want to keep the default rules start with 3 instead. In this case I want to keep the default two rules, and add 3 more rules to it. So the values of the integers will be 3, 4 and 5. After we specified our rules we can go to the RuleMatch function. This function is executed for every neighboring tile, and will return either true or false. By default, the method contains everything we need, we just need to add our custom rules to the switch statement, and return a bool value. I’ll add our rules to the switch statement, and make a separate function for every rule so my code is kinda more clean… to be honest I’m not sure about that one. Each of these functions will take the neighbor tile as parameters, because we need something to compare to. For the “This” rule we’ll override it so we can also check for other types of tiles. Let’s create an array of tiles and call it “tiles to connect”, and also make a bool to toggle if we check for these tiles or not, let’s call this variable “alwaysConnect”. In the function we’ll check if the alwaysConnect variable is enabled or not. If it’s not we’ll just return true if the tile equals this, else, we’ll return true if the tile equals this, OR if the “tilesToConnect” array contains our tile. With the System.Linq namespace we can just use .Contains to check if the array contains that tile. For the “NotThis” rule we won’t modify anything, you can skip this one, but I just included it to show you how it works. It returns true if the other tile is not equal to this tile. For the “Any” rule we’ll return true if the other tile is not empty, so it’s not equal to null. I also added an option to exclude this tile, so it will return true if it’s not empty AND it’s not this tile. For the “Specified” rule we’ll return true if our array contains our tile. Once again we can use the .Contains extension to check if the array contains that tile. And lastly, for the “Nothing” rule we’ll return true if the tile is empty, so it’s equal to null. And we’re done! One last thing we can do is give a path to where we can find our asset in the create asset menu, so it’s more organised. We’re ready to use this tile, so we can go back to Unity now. you can create your custom tile by right clicking in the project window clicking on create and navigating to your specified location. I already have two rule tiles that I want to convert into this new custom tile, but when I create a tile it’ll create a brand new tile asset. Luckily I know a way to convert your existing tiles into custom tiles… If you change your inspector mode from normal to debug mode, you can actually replace what script does this asset use. Since our custom class is extending from the rule tile class, we can just replace the default rule rile script with our custom rule tile script, and nothing will change, except the tile will now use our custom rules. So now we can specify the other tiles to connect to, set up our rules, and it’s working flawlessly. The tiles in this state are perfectly working, but... let’s be honest here… these numbers on our custom rules are not looking great, so I’m gonna show you how you can add custom icons for our custom rules. Let’s create a script called “AdvancedRuleTileEditor”, this script will extend from the “RuleTileEditor” class. This script will be the custom editor of the AdvancedRuleTile script, so let’s add the “CustomEditor” attribute before the class, and we can also add the “CanEditMultipleObjects” attribute there, it’s useful to have, but not optional. I also put this class into the UnityEditor namespace, but that’s not necessary, this will only make so you can reference this script, only if you’re using the UnityEditor namespace. In the class let’s create a few texture2D references, one for every of our custom rules, in this case I created 3 references. If we look at the default RuleTileEditor script we can find where and how the images are assigned to the editor. This method is the RuleOnGUI method, so we’ll override this function. We’ll check for our custom rules, and if it’s matching, we’ll draw a texture like in the base class. Then we’ll return from that function so the numbers won’t be drawn on top of the images. After this is done we’ll call the base function to draw the default images. That’s it, now we can go back to unity and assign the textures. Now if we choose a custom rule it will show our image instead of the numbers. That’s it for this tutorial. If you found it useful please consider liking the video and subscribing to the channel. Seeya!
Info
Channel: Vinark117
Views: 2,562
Rating: undefined out of 5
Keywords: Unity, Unity tutorial, Unity tile, Unity tiles, Unity tilemap, tilemap, tilemaps, tiles, tile, Unity tilemaps, Rule tiles, Unity rule tiles, Unity custom rule tiles, custom rule tiles, rule tile, rule tile tutorial, tile tutorial
Id: FwOxLkJTXag
Channel Id: undefined
Length: 7min 19sec (439 seconds)
Published: Thu May 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.