80 lines
1.8 KiB
C#
80 lines
1.8 KiB
C#
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<Vector2> OverlapTiles { get; private set; }
|
|
|
|
private List<Vector3> Vertices;
|
|
private Mesh Mesh;
|
|
|
|
public HashSet<Vector2> OverlapHashSet;
|
|
|
|
private void Awake()
|
|
{
|
|
OverlapHashSet = new HashSet<Vector2>();
|
|
Vertices = new List<Vector3>();
|
|
|
|
Mesh = GetComponent<MeshFilter>().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
|
|
} |