Files
speckle-unity/Assets/Extra/Selectable.cs
T
2021-07-08 15:54:32 +01:00

48 lines
949 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Thanks to : https://sharpcoderblog.com/blog/unity-3d-rts-style-unit-selection
public class Selectable : MonoBehaviour
{
public Bounds GetObjectBounds()
{
Bounds totalBounds = new Bounds();
var renderers = this.gameObject.GetComponents<MeshRenderer>();
foreach (var r in renderers)
{
if (totalBounds.center == Vector3.zero)
{
totalBounds = r.bounds;
}
else
{
totalBounds.Encapsulate(r.bounds);
}
}
return totalBounds;
}
void OnEnable()
{
//Add this Object to global list
if (!SelectionManager.selectables.Contains(this))
{
SelectionManager.selectables.Add(this);
}
}
void OnDisable()
{
//Remove this Object from global list
if (SelectionManager.selectables.Contains(this))
{
SelectionManager.selectables.Remove(this);
}
}
}