using UnityEditor;
using UnityEngine;
public class ZeroToolEditor
{
private static readonly RaycastHit[] Cache = new RaycastHit[10];
private static Vector3? GetHitPoint(Transform transform)
{
var count = Physics.RaycastNonAlloc(transform.position + Vector3.up, Vector3.down, Cache, 100f);
for (int i = 0; i < count; i++)
{
if (Cache[i].transform == transform)
{
continue;
}
return Cache[i].point;
}
return null;
}
[MenuItem("Zero Tools/Snap To Ground")]
public static void SnapToGround()
{
foreach (var transform in Selection.transforms)
{
var hitPoint = GetHitPoint(transform);
if (hitPoint.HasValue)
{
Undo.RecordObject(transform, "Snap To Ground");
transform.position = hitPoint.Value;
}
}
}
[MenuItem("Zero Tools/Snap To Ground Add Extents Y")]
public static void SnapBoxToGround()
{
foreach (var transform in Selection.transforms)
{
var hitPoint = GetHitPoint(transform);
if (hitPoint.HasValue)
{
Undo.RecordObject(transform, "Snap To Ground");
var pos = hitPoint.Value;
var collider = transform.GetComponent<Collider>();
if (collider)
{
pos.y += collider.bounds.extents.y;
}
transform.position = pos;
}
}
}
}
|