mirror of
https://github.com/nothke/quality-control.git
synced 2025-09-01 08:03:43 +00:00
Added Grid Units for asset placement.
This commit is contained in:
58
Assets/Scripts/Grid Units/GridUnit.cs
Normal file
58
Assets/Scripts/Grid Units/GridUnit.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using static Grid;
|
||||
using Color = UnityEngine.Color;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[ExecuteAlways]
|
||||
[SelectionBase]
|
||||
[DisallowMultipleComponent]
|
||||
public class GridUnit : MonoBehaviour
|
||||
{
|
||||
|
||||
public int3 Size = new (1, 1, 1);
|
||||
private Vector3 SizeInMeters => new (Size.x * CellSize, Size.y * CellSize, Size.z * CellSize);
|
||||
|
||||
public float3 CenterOffset;
|
||||
|
||||
[SerializeField, HideInInspector]
|
||||
private int3 cachedSize = new (1, 1, 1);
|
||||
[SerializeField, HideInInspector]
|
||||
private float3 cachedOffset;
|
||||
|
||||
private bool isDirty => transform.hasChanged ||
|
||||
!all(cachedOffset != CenterOffset) ||
|
||||
!all(cachedSize == Size);
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (!isDirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SnapUnit(transform, Size, CenterOffset);
|
||||
|
||||
transform.hasChanged = false;
|
||||
|
||||
cachedOffset = CenterOffset;
|
||||
cachedSize = Size;
|
||||
}
|
||||
|
||||
[DrawGizmo(GizmoType.InSelectionHierarchy)]
|
||||
private static void DrawGizmo(GridUnit unit, GizmoType type)
|
||||
{
|
||||
Transform t = unit.transform;
|
||||
quaternion rotation = t.rotation;
|
||||
var position = (float3)t.position + mul(rotation, unit.CenterOffset);
|
||||
|
||||
Gizmos.color = Color.white;
|
||||
Gizmos.matrix = Matrix4x4.TRS(position, rotation, Vector3.one);
|
||||
Gizmos.DrawWireCube(Vector3.zero, unit.SizeInMeters);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user