Advertisement
Guest User

sdegasdg

a guest
Sep 19th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.Tilemaps;
  7. using static UnityEditor.FilePathAttribute;
  8.  
  9. public class WaterTile : Tile
  10. {
  11.     public Sprite shallowWater;
  12.     public Sprite shallowOcean;
  13.     public Sprite deepOcean;
  14.     [HideInInspector]
  15.     public float altitude;
  16.     [HideInInspector]
  17.     public Tile[] neighbours = new Tile[8];
  18.  
  19.  
  20.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
  21.     {
  22.         if (altitude > 0.33f)
  23.         {
  24.             tileData.sprite = shallowWater;
  25.         }
  26.         else if (altitude < 0.24f)
  27.         {
  28.             tileData.sprite = deepOcean;
  29.         }
  30.         else
  31.         {
  32.             tileData.sprite = shallowOcean;
  33.         }
  34.  
  35.         this.neighbours[0] = GetNeighbours(position + new Vector3Int(0, 1, 0), tilemap);
  36.         this.neighbours[1] = GetNeighbours(position + new Vector3Int(1, 0, 0), tilemap);
  37.         this.neighbours[2] = GetNeighbours(position + new Vector3Int(0, -1, 0), tilemap);
  38.         this.neighbours[3] = GetNeighbours(position + new Vector3Int(-1, 0, 0), tilemap);
  39.         this.neighbours[4] = GetNeighbours(position + new Vector3Int(-1, -1, 0), tilemap);
  40.         this.neighbours[5] = GetNeighbours(position + new Vector3Int(-1, 1, 0), tilemap);
  41.         this.neighbours[6] = GetNeighbours(position + new Vector3Int(1, 1, 0), tilemap);
  42.         this.neighbours[7] = GetNeighbours(position + new Vector3Int(1, -1, 0), tilemap);
  43.     }
  44.  
  45.     private Tile GetNeighbours(Vector3Int position, ITilemap tilemap)
  46.     {
  47.         if (!tilemap.cellBounds.Contains(position))
  48.         {
  49.             return null;
  50.         }
  51.         return tilemap.GetTile(position) as Tile;
  52.     }
  53.     private bool HasWaterTile(Vector3Int position, ITilemap tilemap)
  54.     {
  55.         return tilemap.GetTile(position) == this;
  56.     }
  57.  
  58. #if UNITY_EDITOR
  59.     [MenuItem("Assets/Create/WaterTile")]
  60.     public static void CreateWaterTile()
  61.     {
  62.         string path = EditorUtility.SaveFilePanelInProject("Save Water Tile", "New Water Tile", "Asset", "Save Water Tile", "Assets/Tiles");
  63.         if (path == "")
  64.             return;
  65.         AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<WaterTile>(), path);
  66.     }
  67. #endif
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement