using JetBrains.Annotations; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class TilesOverlap : MonoBehaviour { public bool UpdateRealTime; public bool DebugTiles; [HideInInspector] public List OverlapTiles { get; private set; } private List Vertices; private Mesh Mesh; public HashSet OverlapHashSet; private void Awake() { OverlapHashSet = new HashSet(); Vertices = new List(); Mesh = GetComponent().mesh; GetOverlap(); #if UNITY_EDITOR DebugOverlap(); #endif } private void Update() { if (UpdateRealTime) { GetOverlap(); #if UNITY_EDITOR DebugOverlap(); #endif } } private void GetOverlap() { Vertices = Mesh.vertices.ToList(); if (OverlapHashSet.Count > 0) OverlapHashSet.Clear(); for (int i = 0; i < Vertices.Count; i++) { var posV3 = transform.TransformPoint(Vertices[i]); //this accounts of negative transforms. //which i don't know if tiles are positive only. var posX = posV3.x < 0 ? posV3.x - 1 : posV3.x; var posY = posV3.z < 0 ? posV3.z - 1 : posV3.z; OverlapHashSet.Add(new Vector2((int)posX, (int)posY)); } //returns as a list for easy manipulation if need be. OverlapTiles = OverlapHashSet.ToList(); } #if UNITY_EDITOR private void DebugOverlap() { if (DebugTiles) { for (int i = 0; i < OverlapTiles.Count; i++) { Debug.Log(OverlapTiles[i]); } } } #endif }