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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Extended Tile class

Discussion in 'Scripting' started by masterkrueger, Apr 26, 2020.

  1. masterkrueger

    masterkrueger

    Joined:
    Apr 23, 2016
    Posts:
    10
    Hi there,

    as the subject suggests i want to extend the tile class.
    My intention is to store additional information like room exits and stuff like this to the tile information.

    I created a new class inherited from the tile class like this
    Code (CSharp):
    1. public class TileExt : Tile
    2. {
    3.     private int extendedInformation;
    4.     ...
    5.     void Start() { }
    6.     void Update() { }
    7. }
    But i don't know how to use this properly when i am setting up my Tilemap.
    Where do i instantiate the new class for the tiles?

    Or is there a much better approach to achieve this?

    Thanks in advance for your help :)

    Jason
     
  2. TuckerFlynn

    TuckerFlynn

    Joined:
    May 18, 2015
    Posts:
    10
    I would recommend you start by taking a look at the 2D Extras package (https://github.com/Unity-Technologies/2d-extras). It has a lot of useful examples you can read through to figure out how to create custom tiles, and it might even have exactly what you're trying to write so you can save yourself the effort! :D

    I'm not an expert by any means, but here's a couple things I would add from my own experience...

    First, Tile doesn't inherit from the Monobehaviour class so the Start and Update functions in your code snippet aren't going to do what you're expecting. Tile inherits from the TileBase class, which is a ScriptableObject. If you want to have your tile doing something in an Update function, you'll need to have a GameObject attached to the tile with a monobehaviour script on the gameobject.

    Second, I believe when it comes to custom tiles you should be inheriting from TileBase and not Tile. If you need the same functionality of the Tile class in addition to your other needs, just write them into you own class (ie. a sprite and gameobject).

    Hope this helps!
     
    masterkrueger likes this.
  3. masterkrueger

    masterkrueger

    Joined:
    Apr 23, 2016
    Posts:
    10
    Hey TuckerFlynn, thank you very much for your answer and ideas. I will go through 2d-extras and see what its in there for me. The tips with TileBase and the background that there is no Update or Start in my new class explains my difficulties a had :) Thank you, now i get a bit further :D
     
  4. masterkrueger

    masterkrueger

    Joined:
    Apr 23, 2016
    Posts:
    10
    I have checked the Extened-2D classes and Information. That was a nice tip. Thx i got much further with my ideas :)
     
  5. JonathanCel

    JonathanCel

    Joined:
    Feb 17, 2021
    Posts:
    22
    Just to add to to the existing information.

    Once you've extended Tile or Tilebase, you'll want to convert existing tiles to your override (in this case TileOverride).

    One way of doing this is via an editor script:

    Code (CSharp):
    1.    
    2. public static void ReplaceTile( Tile inTile ){
    3.  
    4.         string path = AssetDatabase.GetAssetPath( inTile );
    5.  
    6.         TileOverride tileOver = ScriptableObject.CreateInstance<TileOverride>();
    7.  
    8.         EditorUtility.CopySerializedManagedFieldsOnly( inTile, tileOver );
    9.  
    10.         AssetDatabase.CreateAsset( tileOver, path );
    11.  
    12.     }
    Or without code, a simpler way might be:

    - enable the inspector debug mode
    - group select your tiles
    (RIP you if you saved the .assets in the same folder as the sprites lol)
    - click on the selector next to the "Script" property and select "TileOverride" or whatever yours is called
    - done.

    This vid has a great demonstration of both changing types and creating a custom inspector:

    disclosure: not my vid!

    Essentially this:
    Code (CSharp):
    1.    
    2.     public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
    3.     {
    4.  
    5.         TileOverride tileOverride = (TileOverride)target;
    6.         if ( tileOverride.sprite != null ){
    7.             Texture2D newTexture = new Texture2D( width, height );
    8.             Texture2D spritePreview = AssetPreview.GetAssetPreview( tileOverride.sprite );
    9.             EditorUtility.CopySerialized( spritePreview, newTexture );
    10.             EditorUtility.SetDirty( target );
    11.             return newTexture;
    12.         }
    13.  
    14.         return base.RenderStaticPreview(assetPath, subAssets, width, height);
    15.     }
     
  6. kisaure

    kisaure

    Joined:
    Aug 4, 2020
    Posts:
    2
    In addition to JonathanCel response, you can retrieve all your tiles in the project windows (to quick extend them all) by typing "t:Type" in the search bar.
    Note that on the last version of the Unity UI Toolkit, the Script option is grayed out.
    Here is a fix by the same author :
     
    JonathanCel likes this.