using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices.WindowsRuntime; using UnityEngine; using UnityEngine.UI; public class WallDistance : MonoBehaviour { private List lineRenderer = new List(); public Text[] distanceText; public float offset = -0.21f; private bool needToDistanceCheck; private List meshFilters = new List(); private ParentObject parentPbject; public Vector3 minX, maxX, minZ, maxZ; private Vector3 center; private LayerMask layerMaskToIgnore; private void Start() { lineRenderer = BuildingRoom.Instance.GetLineRenderes(); distanceText = BuildingRoom.Instance.GetDistanceText(); parentPbject = GetComponent(); Collider collider = GetComponent(); if (collider) meshFilters.Add(collider); else { meshFilters = new List(GetComponentsInChildren()); } } void Update() { if (!needToDistanceCheck) return; CastRay(); } public void SetMinMaxMesh() { MeshFilter[] childMeshFilters = GetComponentsInChildren(); center = transform.position; minX = Vector3.positiveInfinity; maxX = Vector3.negativeInfinity; minZ = Vector3.positiveInfinity; maxZ = Vector3.negativeInfinity; foreach (MeshFilter childMeshFilter in childMeshFilters) { Mesh childMesh = childMeshFilter.mesh; Vector3[] vertices = childMesh.vertices; foreach (Vector3 vertex in vertices) { Vector3 worldVertex = childMeshFilter.transform.TransformPoint(vertex); minX = Vector3.Min(minX, worldVertex); maxX = Vector3.Max(maxX, worldVertex); minZ = Vector3.Min(minZ, worldVertex); maxZ = Vector3.Max(maxZ, worldVertex); } } minX -= center; maxX -= center; minZ -= center; maxZ -= center; } void CastRay() { if (meshFilters.Count == 0) { return; } Bounds fullBounds = meshFilters[0].bounds; for (int i = 1; i < meshFilters.Count; i++) { Bounds bounds = meshFilters[i].bounds; Vector3 min = Vector3.Min(bounds.min, fullBounds.min); Vector3 max = Vector3.Max(bounds.max, fullBounds.max); fullBounds.SetMinMax(min, max); } Vector3 startRight = fullBounds.center + Vector3.Scale(fullBounds.extents, Vector3.right); Vector3 startLeft = fullBounds.center + Vector3.Scale(fullBounds.extents, Vector3.left); Vector3 startForward = fullBounds.center + Vector3.Scale(fullBounds.extents, Vector3.forward); Vector3 startBack = fullBounds.center + Vector3.Scale(fullBounds.extents, Vector3.back); List linePoints = new List(); parentPbject.distanceToWall[0] = CastRayAndDrawLine(startRight, Vector3.right, 0, linePoints); parentPbject.distanceToWall[1] = CastRayAndDrawLine(startLeft, Vector3.left, 1, linePoints); parentPbject.distanceToWall[2] = CastRayAndDrawLine(startForward, Vector3.forward, 2, linePoints); parentPbject.distanceToWall[3] = CastRayAndDrawLine(startBack, Vector3.back, 3, linePoints); } private float CastRayAndDrawLine(Vector3 startPoint, Vector3 direction, int textIndex, List linePoints) { float distance = 0; linePoints = new List(); startPoint.y = 0.5f; Ray raycast = new Ray(startPoint, direction); RaycastHit hit; int wallLayerMask = 1 << LayerMask.NameToLayer("Wall"); if (Physics.Raycast(raycast, out hit, Mathf.Infinity, wallLayerMask)) { if (hit.collider.CompareTag("Wall")) { distance = hit.distance; if (distance > 0.05f) { //39.37 float inch = distance * 39.37f; int feet = (int)(inch / 12); int remainingInches = (int)(inch % 12); distanceText[textIndex].text = $"{feet} ft {remainingInches} inch"; Vector3 textWorldPosition = (startPoint + hit.point) / 2f; Vector3 offsetVector = (hit.point - startPoint).normalized * offset; Vector3 screenPos = Camera.main.WorldToScreenPoint(textWorldPosition + offsetVector); distanceText[textIndex].transform.position = screenPos; linePoints.Add(startPoint); linePoints.Add(hit.point); lineRenderer[textIndex].positionCount = linePoints.Count; lineRenderer[textIndex].SetPositions(linePoints.ToArray()); } else { distanceText[textIndex].text = string.Empty; lineRenderer[textIndex].positionCount = 0; } } } else { distanceText[textIndex].text = string.Empty; lineRenderer[textIndex].positionCount = 0; } return distance; } public void EnabeleDistanceCheck(bool enable) { needToDistanceCheck = enable; if (!needToDistanceCheck) { DisableComponents(); } } private void DisableComponents() { for (int i = 0; i < distanceText.Length; i++) { distanceText[i].text = string.Empty; lineRenderer[i].positionCount = 0; } } }