using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
using UnityEngine.WSA;
[ExecuteInEditMode()]
public class ShowMeshBounds : MonoBehaviour
{
public Color color = Color.green;
private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;
private bool isSelecting = false;
private Vector3 mousePosition1;
private void Start()
{
CalcPositons();
DrawBox();
}
void Update()
{
// If we press the left mouse button, save mouse location and begin selection
if (Input.GetMouseButtonDown(0))
{
isSelecting = true;
mousePosition1 = Input.mousePosition;
}
// If we let go of the left mouse button, end selection
if (Input.GetMouseButtonUp(0))
isSelecting = false;
}
void CalcPositons()
{
Bounds bounds = GetComponent<MeshFilter>().sharedMesh.bounds;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
v3BackTopRight = transform.TransformPoint(v3BackTopRight);
v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
}
void DrawBox()
{
SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);
SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);
SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);
}
void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
{
GameObject myLine = new GameObject();
myLine.name = start.ToString();
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.startColor = color;
lr.endColor = color;
lr.startWidth = 0.03f;
lr.endWidth = 0.03f;
lr.SetPosition(0, start);
lr.SetPosition(1, end);
}
private void OnGUI()
{
if (isSelecting)
{
// Create a rect from both mouse positions
var rect = DrawRectangle.GetScreenRect(mousePosition1, Input.mousePosition);
DrawRectangle.Utils.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
DrawRectangle.Utils.DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
}
}
}
Inside the method SpawnLineGenerator( Iām giving name to each gameobject:
myLine.name = start.ToString();
The result is a name for example:
(-2.1, 3.4, 4.9)
But instead I want some how to make in the Inspector a name description and the start and end coordinates of the object for example in the Inspector:
v3FrontTopLeft, v3FrontTopRight (-2.1, 3.4, 4.9) , (-1.1, 3.4, 4.9)
Next
v3FrontTopRight, v3FrontBottomRight (1,1,1) , (2,2,2)
And so on. The variables names in the DrawBox method will be the description of each vector3 and the coordinates of it.